Align text with RadioGroup / RadioButton

I would like to have text next to RadioGroup, which is a kind of shortcut for it. So there are two buttons in RadioGroup with text for them, and currently I can only get the vertices, but that looks pretty awkward. I can try to tag a TextView in a string, but this is not ideal or simple. RadioGroup and TextView live inside RelativeLayout, I tried android:layout_alignBaselinefor TextView with RadioGroup and one of RadioButtons, but RadioGroup doesn't seem to have a baseline, and I can't get the baseline for RadioButton from TextView.

+3
source share
3 answers

I had the same problem these days and I finally solved it. I tried every combination that came to my mind, and in the end, I understand that the trick is to put android: layout_weight with the same value in TextViews. This aligns them only to the center of RadioButtons.

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dip"
        android:layout_marginLeft="15dip"
        android:orientation="horizontal" >

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:button="@drawable/radio_button"
                    android:paddingRight="15dip"
                    />
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:button="@drawable/radio_button"
                    android:paddingRight="15dip"
                    />
        </RadioGroup>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="@string/cop_radiobutton_label"
                    android:layout_weight="1"
                    android:gravity="center_vertical"/>

            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="@string/thief_radiobutton_label"
                    android:layout_weight="1"
                    android:gravity="center_vertical"/>
        </LinearLayout>
    </LinearLayout>
+2
source
<LinearLayout
           android:id="@+id/linearLayout5" 
           android:layout_width= "fill_parent"
           android:layout_height= "50dip"
           android:orientation="horizontal"
           android:layout_gravity="center"
           android:background="@drawable/squareback">

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

               <RadioButton
                   android:id="@+id/meleeRadio"
                   android:layout_width="150dip"
                   android:layout_height="40dip"
                   android:checked="false"
                   android:text="Melee Attack" android:textColor="@color/black" android:gravity="center" android:layout_gravity="center" android:layout_margin="5dip"/>

           <RadioButton
               android:id="@+id/rangeRadio"
               android:layout_width="150dip"
               android:layout_height="40dip"
               android:text="Range Attack" android:textColor="@color/black" android:gravity="center" android:layout_gravity="center" android:layout_margin="5dip" android:checked="false"/>

           </RadioGroup>



        </LinearLayout> 

To align them, I placed them in a LinearLayout, for which I set the size. Then I have android: layout_gravity = "center", which aligns the center for the elements. Therefore, even if the button and the text do not have the same size, they look good imo.

+1
source

: RadioGroup, 3- RadioButtons. , android: layout_gravity textView . :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Experience Level:"
        android:layout_gravity="center"/>

   <RadioGroup
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Novice"/>
       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Intermidiate"/>
       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Advanced"/>
   </RadioGroup>
</LinearLayout>
0

All Articles