Search code examples
androidxmlradio-buttonalignmentandroid-linearlayout

LinearLayout with TextView on the left and RadioButton on the right?


Good afternoon,

I am trying to do an alignment like this one :

https://i.sstatic.net/h183p.png

To do it I tried the following code :

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="0sp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dark background"
        android:textAppearance="@style/TextAppearance.AppCompat.Title" />

    <RadioButton android:id="@+id/radio_darkbackground"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked"
        android:layout_gravity="right"
        android:gravity="right"/>
</LinearLayout>

As you can see, I put the layout_gravity and gravity on "right" but it does not work and look like this :

https://i.sstatic.net/nLbBj.png


Solution

  • change your code to this:

    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Dark background"
                android:textAppearance="@style/TextAppearance.AppCompat.Title" />
    
            <RadioButton android:id="@+id/radio_darkbackground"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:onClick="onRadioButtonClicked"
                         android:layout_gravity="right"/>
        </FrameLayout>