I have Radio Group which appears left aligned on screen.
How can i horizontally center the radio group.
The TextView itself appears properly center-aligned horizontally.
....
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Test"
/>
<RadioGroup
android:id="@+id/radioGroupMode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal" >
<RadioButton
android:id="@+id/radioTestMode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:tag="OPTION_1"
android:text="Option1"
/>
<RadioButton
android:id="@+id/radioLearnMode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="Option_2"
android:text="Option 2"
/>
</RadioGroup>
</LinearLayout>
....
In radio group layout you mentioned layout width like match_parent
,if you mention your parent layout like match parent you cannot use gravity properly.So you need to change width like wrap_content
.
<RadioGroup
android:id="@+id/radioGroupMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal" >
<RadioButton
android:id="@+id/radioTestMode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:tag="OPTION_1"
android:text="Option1"
/>
<RadioButton
android:id="@+id/radioLearnMode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="Option_2"
android:text="Option 2"
/>
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Test" />
<RadioGroup
android:id="@+id/radioGroupMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal">
<RadioButton
android:id="@+id/radioTestMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:tag="OPTION_1"
android:text="Option1"
/>
<RadioButton
android:id="@+id/radioLearnMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="Option_2"
android:text="Option 2"
/>
</RadioGroup>
</LinearLayout>
Problem is with your RadioButton. Kindly change your layout_width to "wrap_content"
Try this :
<RadioGroup
android:id="@+id/account_options_sex"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:orientation="horizontal"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
app:layout_constraintTop_toBottomOf="@+id/account_title_pf_sex">
<RadioButton
android:id="@+id/radio_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitStart"
android:text="Feminino" />
<RadioButton
android:id="@+id/radio_masculine"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitStart"
android:text="Masculino" />
<RadioButton
android:id="@+id/radio_unkown"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitStart"
android:text="Indefinido" />
</RadioGroup>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With