Stanislav Demianets
Stanislav Demianets

Reputation: 43

android how to align radio-buttons inside radio group?

I have something like a table in my activity( I used vertical + horizontal linear layouts to make this table), in this table I have buttons, edit texts, and radio buttons. I need to align radio-buttons inside radiogroup correspondingly to all table. But currently radio-buttons looks like shifted to left.

I played with gravity of each radio button, with gravity of radio-group and it didn't help.

Here is the code of 2 last horizontal linear layouts. I need to align radio-buttons to be centered like editText.

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="6">

            <ImageView
                android:id="@+id/imageQuest"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <EditText
                android:id="@+id/quest1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:digits="0123456789-"
                android:ems="10"
                android:inputType="number"
                android:maxLength="3"
                android:hint="0"
                android:gravity="center" />

            <EditText
                android:id="@+id/quest2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:digits="0123456789-"
                android:ems="10"
                android:inputType="number"
                android:maxLength="3"
                android:hint="0"
                android:gravity="center" />

            <EditText
                android:id="@+id/quest3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:digits="0123456789-"
                android:ems="10"
                android:inputType="number"
                android:maxLength="3"
                android:hint="0"
                android:gravity="center" />

            <EditText
                android:id="@+id/quest4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:digits="0123456789-"
                android:ems="10"
                android:inputType="number"
                android:maxLength="3"
                android:hint="0"
                android:gravity="center" />

            <EditText
                android:id="@+id/quest5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:digits="0123456789-"
                android:ems="10"
                android:inputType="number"
                android:maxLength="3"
                android:hint="0"
                android:gravity="center" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="6">

            <ImageView
                android:id="@+id/imageLongestWay"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <RadioGroup
                android:id="@+id/RadioGroup"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:orientation="horizontal"
                android:weightSum="5">

                <RadioButton
                    android:id="@+id/radioButton1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

                <RadioButton
                    android:id="@+id/radioButton2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

                <RadioButton
                    android:id="@+id/radioButton3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

                <RadioButton
                    android:id="@+id/radioButton4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

                <RadioButton
                    android:id="@+id/radioButton5"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />
            </RadioGroup>


        </LinearLayout>

Here is screenshot: Radio-buttons are not aligned inside radio-group

Upvotes: 0

Views: 3288

Answers (2)

Stanislav Demianets
Stanislav Demianets

Reputation: 43

Thanks S.B.K for advise, we need to use custom radio button image. And use "android:drawableTop="

so 1) download 2 images from internet. One for checked radio, one for unchecked. 2) Put them to drawable folder in your project 3) create custom_radio_button.xml in drawable folder

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

4) change your radiobutton parameters to

android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@android:color/transparent"
android:drawableTop="@drawable/custom_radio_button"/>

Upvotes: 0

sandeep kolhal
sandeep kolhal

Reputation: 423

You need to create custom RadioButton in drawable:

<RadioButton
      android:id="@+id/radioButton1"
      android:layout_width="0dp"
      android:layout_height="50dip"
      android:layout_weight="1"
      android:layout_gravity="center_horizontal"
      android:button="@android:color/transparent"
      android:drawableLeft="@drawable/custom_radio_button"/>

custom_radio_button.xml

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

Upvotes: 1

Related Questions