D. Math D. Math - 6 months ago 27
Android Question

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

Good afternoon,

I am trying to do an alignment like this one :

http://i.imgur.com/ArAEiZC.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 :

http://i.imgur.com/JDzwEJI.png

Answer

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>