How to align Radio button at the center of the screen
Asked Answered
W

12

16

I am using Radio buttons as tab in my application.

I have loaded images for it, but they are aligning towards left side. how to make them align at center.

enter image description here

This is how my XML file looks

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/tabhost" 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <FrameLayout android:id="@android:id/tabcontent"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:layout_weight="1" 
      android:padding="20dip" 
      android:background="#fff"/>

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


      <RadioButton android:id="@+id/allcontacts" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center" 
      android:layout_weight="1"/>

      <RadioButton
          android:id="@+id/categories"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_weight="1"/>

      <RadioButton android:id="@+id/favourites" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center" 
      android:layout_weight="1" />

    </RadioGroup>

    <TabWidget android:id="@android:id/tabs"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:layout_weight="0" android:visibility="gone" />
  </LinearLayout>
</TabHost>
Weisshorn answered 5/5, 2012 at 17:15 Comment(4)
Just curious, why aren't you using TabWidget?Distefano
I want tabs to come at the bottom of screen. If i use tab widget, tabs looks above so..Weisshorn
Never thought of that, great idea. I'm glad I asked. Another trick to stuff into my notes. :)Distefano
I was having a similar problem and solved in a different way. Check: #36433550Dolt
D
14

Try android:gravity="center" in the xml for each RadioButton.

I see you're wrap_content, so theoretically that shouldn't do anything, but it looks like the icons are evenly divided, maybe the RadioGroup divides the space evenly so using android:gravity will have an effect.

Worth a shot I guess.

EDIT

Try this:

  <RadioButton android:id="@+id/allcontacts" 
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:marginLeft="20dp" 
  android:gravity="center"
  android:layout_weight="1"/>

You will have to play with the android:marginLeft="20dp" to get it properly centered, but that'll let you do what you're looking to do. I copied your layout and tested this time, so I know it works! :)

Distefano answered 5/5, 2012 at 18:3 Comment(3)
Got it, check my re-revised answer. Use what I propsed previously and add android:marginLeft="#dp" with # being whatever is necessary to move the image to the center.Distefano
Yes Barak, It worked... Thanks for your help... Hav gr8 day :)Weisshorn
Barak, I think specify layout_marginLeft is too absolutely, did you agree me?Gibbet
G
26

I had similar issue before, I figure it out, so I try to explain it for you.

runtime screenshot

above is my app screenshot, as you can see, I also did the same, I had a menu align screen bottom, they're a RadioGroup and three RadionButton inside it, I want each menu icon align center for RadionButton, the shadow image(9-patch) shown together when Button check event trigger.
At first, I used android:button attribute refers to a drawable selector, the selector has two drawable state between check and uncheck, but I cannot align the menu icon center and make the shadow image fill up by RadionButton, they looks like it :

enter image description here

I tried included android:layout_gravity=center and android:gravity=center with the RadionButton, but it also didn't got effect. after that, my workmate told me the android:button attribute is refers to foreground rather than background, then I use android:background instead of android:button and it worked, below is my code :

<RadioGroup android:layout_width="match_parent" android:layout_height="60dp"
        android:orientation="horizontal" android:background="@drawable/menu_bg">

    <RadioButton android:layout_width="0dp" android:layout_height="match_parent"
                 android:layout_weight="1" android:button="@null" android:checked="true"
                 android:background="@drawable/menu_item_selector" />

    <RadioButton android:layout_width="0dp" android:layout_height="match_parent"
                 android:layout_weight="1" android:button="@null"
                 android:background="@drawable/menu_item_selector" />

    <RadioButton android:layout_width="0dp" android:layout_height="match_parent"
                 android:layout_weight="1" android:button="@null"
                 android:background="@drawable/menu_item_selector" />

</RadioGroup>

the menu_item_selector is important thing because it had gravity setting :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/menu_item_layer_list" android:state_checked="true" />
    <item android:drawable="@drawable/menu_item_layer_list" android:state_pressed="true" />
    <item>
        <bitmap android:src="@drawable/menu_item_off" android:gravity="center" />
    </item>
</selector>

cause I had two images to draw when user turn Button state is checked, the shadow background image and icon active state image, so I used layer-list to implement it, the active icon also had gravity setting, below is menu_item_layer_list code :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/menu_bg_pressed" />
    <item>
        <bitmap android:src="@drawable/menu_item_on" android:gravity="center" />
    </item>
</layer-list>

I don't know that solution was perfect or not, because I always implement it as a view and draw myself. But I think Android SDK supplied more convenient component let's finish work with xml configuration, we should be learn and use it.

Gibbet answered 27/7, 2013 at 8:44 Comment(2)
best solution by far! especially the android:gravity="center" thingy in the <item /> tags for your selector was a real new thing for me! thumbs up!Bowfin
android:button="@null" is main here!Eldrida
I
20

You can use empty views to fill upp the space evenly between the radio buttons. In this case I have 3 radio buttons that vill look centered, nice and tidy.

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

    <View
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="" />

    <View
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:checked="true"
        android:gravity="center"
        android:text="" />

    <View
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="" />

    <View
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</RadioGroup>
Impudence answered 3/8, 2013 at 17:33 Comment(3)
Nice solution! But I used instead of TextView a View and for the most right and left view android:layout_weight="0.5".Keifer
Very nice solution! Works for landscape and portrait.Saw
ohhohhoooohooooo,, tq very muchCris
D
14

Try android:gravity="center" in the xml for each RadioButton.

I see you're wrap_content, so theoretically that shouldn't do anything, but it looks like the icons are evenly divided, maybe the RadioGroup divides the space evenly so using android:gravity will have an effect.

Worth a shot I guess.

EDIT

Try this:

  <RadioButton android:id="@+id/allcontacts" 
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:marginLeft="20dp" 
  android:gravity="center"
  android:layout_weight="1"/>

You will have to play with the android:marginLeft="20dp" to get it properly centered, but that'll let you do what you're looking to do. I copied your layout and tested this time, so I know it works! :)

Distefano answered 5/5, 2012 at 18:3 Comment(3)
Got it, check my re-revised answer. Use what I propsed previously and add android:marginLeft="#dp" with # being whatever is necessary to move the image to the center.Distefano
Yes Barak, It worked... Thanks for your help... Hav gr8 day :)Weisshorn
Barak, I think specify layout_marginLeft is too absolutely, did you agree me?Gibbet
C
4

You could put your RadioButton inside FrameLayout and center them:

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

    <FrameLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content" 
         android:layout_weight="1">

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"

            android:button="@drawable/rb_temp1"
            android:checked="true"
            android:gravity="center" />
    </FrameLayout>

    <FrameLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content" 
         android:layout_weight="1">

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"

            android:button="@drawable/rb_temp1"
            android:gravity="center" />
    </FrameLayout>

</RadioGroup>
Chordate answered 12/7, 2013 at 4:18 Comment(2)
but then how you will access property of radio group to select only single child.Dethrone
To reiterate @Shubh 's problem, if you go with this method of centering radiobuttons, the default functionality which handles allowing only one radiobutton to be selected at a time will NOT work. You will have to manually implement handling of allowing one button to be checked at a time. So, I would recommend finding another way of centering radiobuttons if this default functionality matters to you.Radiochemistry
B
3

Put android:gravity="center" on RadioGroup Tag. This Works.

Bogeyman answered 23/9, 2014 at 17:57 Comment(0)
R
2

I tried all of the above and it doesn't work.

I had found a better solution which i would like to share. Set your button to @null ; then add the drawable to android:drawableTop or any other type of position. This will enable us to set the drawables to other position of the radioButton. add some padding or drawablePaddingso that it will not stick to any side of the button/text.

android:background="@drawable/background_paint"
android:drawableTop="@drawable/shape"
android:button="@null"
Rang answered 15/10, 2013 at 8:59 Comment(2)
You were right! Just i added a specific height + android:minHeight + android:paddingTop! = Center Image It looks nice! Thank you!Len
this was exactly what I was looking forNessie
D
1

Try adding an android:layout_gravity="center_horizontal"on your RadioGroup.

Delectable answered 5/5, 2012 at 17:26 Comment(0)
T
1

in my case, my radio button contents were aligned right. I let them aligned center by set android:background be some value, such as @null or some color. below were my layout

<LinearLayout
    android:id="@+id/tab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <RadioGroup
        android:paddingTop="5dp"
        android:id="@+id/radioGroupAsTab"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center_vertical|center_horizontal"
        android:background="@color/v3_header_bg"
        android:fadingEdge="none"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rbDiagnose"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:checked="true"
            android:drawableTop="@drawable/v3_tab_diagnose"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/tabCaption_health_maintenance"
            android:textColor="@color/radio_astab_changecolor"
            android:textSize="10sp"
            android:textStyle="bold" />

        <RadioButton
            android:id="@+id/rbHistory"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:drawableTop="@drawable/v3_tab_history"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/tabCaption_history"
            android:textColor="@color/radio_astab_changecolor"
            android:textSize="10sp"
            android:textStyle="bold" />

        <RadioButton
            android:id="@+id/rbChart"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:drawableTop="@drawable/v3_tab_chart"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/tabCaption_graph"
            android:textColor="@color/radio_astab_changecolor"
            android:textSize="10sp"
            android:textStyle="bold" />

        <RadioButton
            android:id="@+id/rbCyclopedia"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:drawableTop="@drawable/v3_tab_encyclopedia"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/tabCaption_encyclopedia"
            android:textColor="@color/radio_astab_changecolor"
            android:textSize="10sp"
            android:textStyle="bold" />

        <RadioButton
            android:id="@+id/rbSetting"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:drawableTop="@drawable/v3_tab_setting"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/tabCaption_info"
            android:textColor="@color/radio_astab_changecolor"
            android:textSize="10sp"
            android:textStyle="bold" />
    </RadioGroup>
</LinearLayout>
Terisateriyaki answered 20/1, 2014 at 3:18 Comment(0)
O
1
 <RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >
       <RadioButton
        android:id="@+id/rb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="One"/>

        <RadioButton
        android:id="@+id/rb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Two"/>
 </RadioGroup>
Organon answered 27/4, 2015 at 6:23 Comment(1)
I think this is the better solution out of all of them. Setting the width to wrap_content or to a specific consistent width allows the RadioButtons to be only as wide as they need to be and for the RadioGroup to center the entire set of RadioButtons. Great job!Radiochemistry
S
0

i used this line in my RadioGroup and it is working fine!

android:gravity="center|left" 
Stack answered 10/9, 2013 at 15:40 Comment(0)
C
0

It was in the opposite side in my case and I just put android:paddingLeft="0dp" and it worked so you should try android:paddingRight="0dp" for radiobuttons

Crocket answered 31/7, 2015 at 12:55 Comment(0)
W
0

You can try this:

<RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/like"
            android:checked="true"
            android:layout_weight="1"
            android:gravity="right"
            android:layout_gravity="center"
            android:background="@drawable/like_selecter"
            android:button="@android:color/transparent"
            android:drawableLeft="@drawable/ic_baseline_thumb_up_select"
            android:paddingLeft="10dp"/>
Windowshop answered 16/8, 2022 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.