1

I am writing an application where I need to draw two radio button in equal horizontal space. That I achieved by set android:layout_weight="1" property for both of radio button.

Now I need to display custom drawable button in centre and need to place text inside of that drawable, like this:

enter image description here

Here behaviour of both draw drawable and text is dynamic.

Is there any way to achieve both things in centre.

CoDe
  • 11,056
  • 14
  • 90
  • 197

1 Answers1

1

Give a try to the following code to achieve the desired output.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="horizontal">

   <LinearLayout
       android:orientation="horizontal"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:gravity="center">

       <RadioButton
           android:id="@+id/radioButton"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="@drawable/rd_bg"
           android:button="@null"
           android:gravity="center"
           android:text="15"/>
   </LinearLayout>

   <LinearLayout
       android:orientation="horizontal"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:gravity="center">

       <RadioButton
           android:id="@+id/radioButton2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:button="@null"
           android:background="@drawable/rd_bg"
           android:gravity="center"
           android:text="30"/>
   </LinearLayout>
</LinearLayout>

Below is the code for drawable duration_selector

<?xml version="1.0" encoding="utf-8"?>

 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="true"
      android:state_focused="true">
    <shape android:shape="oval">
        <solid android:color="@color/login_bg"/>
        <size
            android:width="50dp"
            android:height="50dp"/>
    </shape>
</item>

<item
    android:state_checked="false"
    android:state_focused="true">
    <shape android:shape="oval">
        <solid android:color="@color/login_bg"/>
        <size
            android:width="50dp"
            android:height="50dp"/>
    </shape>
</item>

<item
    android:state_checked="true">
    <shape android:shape="oval">
        <solid android:color="@color/login_bg"/>
        <size
            android:width="50dp"
            android:height="50dp"/>
    </shape>
</item>

<item
    android:state_checked="false">
    <shape android:shape="oval">
        <solid android:color="@android:color/transparent"/>
        <stroke android:color="@color/login_bg"
                android:width="1dp"/>
        <size
            android:width="50dp"
            android:height="50dp"/>
    </shape>
</item>

@color/login_bg is the color code which you would like to use.

miteshpithadiya
  • 214
  • 1
  • 10
  • hi..thanks for your answer. But since I need to place button in equal horizontal space. So then bg image "duration_selector' get stretch and text get aligned at left. Any other suggestion ! – CoDe Nov 03 '14 at 08:42
  • @Shubh You can check out the solution http://stackoverflow.com/a/19163987/1839336 . It might help you. – GrIsHu Nov 03 '14 at 08:59
  • @miteshpithadiya but in this it not satisfy radio group property to select single radio button .any other suggestion! – CoDe Nov 03 '14 at 09:07
  • @GrIsHu thanks, but this solution also have same issue, image get stretch as try to make it equal horizontal space, ie. using android:layout_weight="1" ...any other solution? – CoDe Nov 03 '14 at 09:12
  • hi thanks for reply. Since my resquirement to get both thing in centre so it will not fit. But any way I found other way with Toggle button, It require to maintain state of individual child. Thanks for ur work :). – CoDe Nov 04 '14 at 05:55