Home > Software engineering >  make RadioGroup fill the screen's width while making the radiobuttons equidistant to each other
make RadioGroup fill the screen's width while making the radiobuttons equidistant to each other

Time:10-31

How do I make RadioGroup fill the screen's width while being making the radiobuttons equidistant to each other?

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

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hi3" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hi2" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hi0"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hi"/>
        </RadioGroup>

CodePudding user response:

Try this :
 <RadioGroup
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="4"
            android:visibility="visible">

            <RadioButton
                android:text="hi1"
                android:layout_width="0dp"
                android:layout_height="match_parent"

                android:layout_weight="1" />

                <RadioButton
                    android:text="hi1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />

                <RadioButton
                    android:text="hi1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />

                <RadioButton
                    android:text="hi1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />


        </RadioGroup>

CodePudding user response:

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

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/daily_weekly_button_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal">


<RadioButton
    android:id="@ id/radio0"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:background="@drawable/radio_flat_selector"
    android:button="@android:color/transparent"
    android:checked="true"
    android:gravity="center"
    android:text="Daily"
    android:textColor="#000000" />

<RadioButton
    android:id="@ id/radio1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:background="@drawable/radio_flat_selector"
    android:button="@android:color/transparent"
    android:gravity="center"
    android:text="Weekly"
    android:textColor="#000000" />

<RadioButton
    android:id="@ id/radio2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:background="@drawable/radio_flat_selector"
    android:button="@android:color/transparent"
    android:gravity="center"
    android:text="Weekly"
    android:textColor="#000000" />

</RadioGroup>

//radio_flat_selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/round_bg" android:state_checked="true" 
/>
<item android:drawable="@drawable/round_bg" />
</selector>
//round bg
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid
    android:color="#E7ECF6"/>

<size
    android:width="120dp"
    android:height="120dp"/>
</shape>
  • Related