androidxmlandroid-layoutjustify

Android; How to Right Justify Buttons while other widgets stay Left Justified


I'm new to Android and my app is finally working however I am having trouble with the layout.

The xml below is stripped down but where I marked it <repeats> I have 4 horizontal layouts.

Each with different widgets Spinner, SeekBar and RadioButtons all seperated by a divider.

Each you pick a setting and press the Set button to the right.

My issue, though all widget are neatly Left justified the vertical alignment of the buttons is all over the place.

If i just play with widget margins it will not fit right with different Android devices.

How can I get the Buttons vertically lined up to the right without moving all else from the left?

           GETTING                              WANT
 -------------------------------   -------------------------------- 
 | <Spinner>  <button>          |  | <Spinner>           <button> |      
 | < -------- Divider --------> |  | < -------- Divider --------> |
 | <RadioButton >  <button>     |  | <RadioButton>       <button> |
 | < -------- Divider --------> |  | < -------- Divider --------> |
 | <SeekBar>  <button>          |  | <SeekBar>           <button> |
 | < -------- Divider --------> |  | < -------- Divider --------> |
 | <RadioButton>       <button> |  | <RadioButton>       <button> |
 -------------------------------   --------------------------------

Here is my related code:

content_main.xml 
 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        tools:scrollY="?android:attr/scrollbarSize">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical">

<repeats>   <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <Spinner
                    android:id="@+id/dynamic_TZ_spinner"
                    android:layout_width="223dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/dp20"
                    android:layout_marginRight="@dimen/dp20" />
                <Button
                    android:id="@+id/TZ_btnSubmit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/set" />
            </LinearLayout>
            <View
                android:id="@+id/divider3"
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:layout_marginTop="@dimen/dp10"
                android:background="?android:attr/listDivider"
                android:visibility="visible" />
....            

            </LinearLayout>
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

Solution

  • Try this :-

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        tools:scrollY="?android:attr/scrollbarSize">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical">
    
     <repeats>   <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <Spinner
                    android:id="@+id/dynamic_TZ_spinner"
                    android:layout_width="223dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginLeft="@dimen/dp20"
                    android:layout_marginRight="@dimen/dp20" />
                <Button
                    android:id="@+id/TZ_btnSubmit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:text="@string/set" />
            </RelativeLayout>
            <View
                android:id="@+id/divider3"
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:layout_marginTop="@dimen/dp10"
                android:background="?android:attr/listDivider"
                android:visibility="visible" />
    ....            
    
            </LinearLayout>
    </RelativeLayout>