1

I'm Developing an android app in which the login activity contains radio button for the field department and years of experience. And I've designed my login activity in Relative layout.So now i wanted to RadioGroup the radiobuttons without changing the alignment. Please Help me ! Thanks in Advance !

`

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/reg_view"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText2"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="59dp"
    android:ems="10"
    android:hint="@string/firstname"
    android:inputType="textPersonName"
    android:text="@string/edit_name" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/editText1"
    android:ems="10"
    android:hint="@string/lastname"
    android:inputType="textPersonName"
    android:text="@string/edit_name" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/editText1"
    android:layout_alignLeft="@+id/editText1"
    android:layout_marginBottom="35dp"
    android:text="@string/name_view"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView4"
    android:layout_alignRight="@+id/textView1"
    android:text="@string/teamleader_radio" />
<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView4"
    android:layout_below="@+id/textView2"
    android:checked="true"
    android:text="@string/manager_radio" />
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/textView5"
    android:text="@string/Registration_proceed" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioButton1"
    android:layout_centerHorizontal="true"
    android:text="@string/years_view"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<RadioButton
    android:id="@+id/radioButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView4"
    android:layout_below="@+id/textView4"
    android:text="@string/years_check" />

<RadioButton
    android:id="@+id/radioButton4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/radioButton3"
    android:layout_alignBottom="@+id/radioButton3"
    android:layout_centerHorizontal="true"
    android:text="@string/years1_check" />

<RadioButton
    android:id="@+id/radioButton5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/radioButton4"
    android:layout_alignBottom="@+id/radioButton4"
    android:layout_alignParentRight="true"
    android:text="@string/years3_check" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioButton3"
    android:layout_toLeftOf="@+id/radioButton4"
    android:text="@string/dept_view"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioButton1"
    android:layout_below="@+id/editText2"
    android:text="@string/desig_view"
    android:textAppearance="?android:attr/textAppearanceMedium" />

`

Steve
  • 117
  • 2
  • 3
  • 11
  • if you use radio button except group then you will manage this manually so always try to use in side radio button group. – Yogendra Feb 27 '14 at 07:03

1 Answers1

4

You can use the <RadioGroup> to group the Radio buttons like this;

<RadioGroup 
    android:id="@+id/radioGroup"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/Red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/red"
        android:textColor="@android:color/holo_red_dark" 
        android:onClick="onRadioButtonClicked" />

    <RadioButton
        android:id="@+id/Green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/green"
        android:textColor="@android:color/holo_green_dark"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/Blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/blue"
        android:textColor="@android:color/holo_blue_dark"
        android:onClick="onRadioButtonClicked"/>

</RadioGroup>
Lachlan Goodhew-Cook
  • 1,101
  • 17
  • 31
Pankaj
  • 2,115
  • 2
  • 19
  • 39
  • AndroidGeek: Thanks for your code.But I made the same changes in my xml the thing is alignment is getting changed in the graphical layout. I've also assigned the orientation as horizontal what should I do for that? – Steve Feb 28 '14 at 05:54
  • you need to handle other widget's alignment if you are using RelativeLayout as root ,so to avoid that you can use LinearLayout in place of that and set the orientation to horizontal – Pankaj Feb 28 '14 at 06:17