1

I need to use custom radio buttons with background image, and space them evenly horizontally, I can achieve the spacing but cannot position the background image in the center of each button. My layout is as below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="58dp">


    <RadioGroup
        android:id="@+id/group_bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="52dp"

        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/v4_btn_trip"
            style="@style/BottomTabBarButtonStyle"
            android:button="@drawable/v4_tabbar_radio_trip" />

        <RadioButton
            android:id="@+id/v4_btn_agenda"
            style="@style/BottomTabBarButtonStyle"
            android:button="@drawable/v4_tabbar_radio_agenda" />

        <RadioButton
                android:id="@+id/v4_btn_favorite"
                style="@style/BottomTabBarButtonStyle"
            android:button="@drawable/v4_tabbar_radio_favorite" />

        <RadioButton
                android:id="@+id/v4_btn_settings"
                style="@style/BottomTabBarButtonStyle"
            android:button="@drawable/v4_tabbar_radio_settings" />

    </RadioGroup>


</RelativeLayout>

The style is per below:

<style name="BottomTabBarButtonStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:layout_weight">1</item>

I already set the gravity to center_horizontal.

I tried to replace android:button with android:background but that just stretched the image.

One of the selector files (v4_tabbar_radio_trip)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_bottom_tab_trip_active" android:state_checked="true" android:state_pressed="true" />
    <item android:drawable="@drawable/ic_bottom_tab_trip_active" android:state_pressed="true" />
    <item android:drawable="@drawable/ic_bottom_tab_trip_active" android:state_checked="true" />
    <item android:drawable="@drawable/ic_bottom_tab_trip" />
</selector>

This is its current state. Update: The expected output is that the background image (the two dots) is in the middle of the greyed area (which is the radio button), not aligned to the left. Similarly for other icons

enter image description here

EyeQ Tech
  • 7,198
  • 18
  • 72
  • 126

4 Answers4

0

try to write

    style="@style/BottomTabBarButtonStyle" 

after the

    android:button="@drawable/v4_tabbar_radio_settings" 

in layout xml file.

Manoj ahirwar
  • 1,062
  • 1
  • 10
  • 24
0

You can try this layout xml.

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

    <RadioGroup
        android:id="@+id/group_bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:orientation="horizontal" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <RadioButton
            android:id="@+id/v4_btn_trip"
            android:layout_gravity="center"
            android:button="@drawable/v4_tabbar_radio_trip" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <RadioButton
            android:id="@+id/v4_btn_agenda"
            android:layout_gravity="center"
            android:button="@drawable/v4_tabbar_radio_agenda" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <RadioButton
            android:id="@+id/v4_btn_favorite"
            android:layout_gravity="center"
            android:button="@drawable/v4_tabbar_radio_favorite" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <RadioButton
            android:id="@+id/v4_btn_settings"
            android:layout_gravity="center"
            android:button="@drawable/v4_tabbar_radio_settings" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </RadioGroup>

</LinearLayout>

I know this is not good way of handling the things but it can do a trick for you and it is feasible for all screens.

Try it. Hope this helps you.

A.R.
  • 2,631
  • 1
  • 19
  • 22
  • @AR Nice trick, thanks. I suggest you edit your answer by putting layout_width=0dp since it use the weight already. That's what I did and everything shows up alright. – EyeQ Tech Mar 10 '15 at 02:19
0

Try out this!!

<LinearLayout
            android:id="@+id/ll_radio_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:weightSum="2" >

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

                <RadioButton
                    android:id="@+id/radioPreapaid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:button="@android:drawable/btn_radio" />

                <RadioButton
                    android:id="@+id/radIoPostpaid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:gravity="center_horizontal" />
            </RadioGroup>
        </LinearLayout>
Srishti Roy
  • 576
  • 5
  • 17
0

You are centering your button, but the drawable is always left-aligned.

The solution appears to be create a new radio group. Here is an example.

Community
  • 1
  • 1
DevJem
  • 656
  • 1
  • 13
  • 21