Search code examples
androidxmlbuttonandroid-scrollview

How to align button to right in Scrollview?


I want the first button aligned to the left as it is and the other button below it to be aligned to the right. I've tried a lot of stuff but nothing seems to work. Both buttons are in a scroll view. Below is my XML Activity.

The background the buttons are using is an XML Drawable file with a shape.

            <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".pg2">

    <ScrollView
        android:layout_width="395dp"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="243.288590604dp"
                android:layout_height="100dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:contentDescription="@string/logo"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.151"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.035"
                app:srcCompat="@drawable/frag11" />

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:fontFamily="@font/open_sans_semibold"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:text="@string/basic_rules_of_html"
                android:textColor="#000000"
                android:textSize="34sp" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:text="@string/str1"
                android:textColor="#0A538F"
                android:textSize="30sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:background="#DADADA"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="@string/str3"
                android:textColor="#000000"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:text="@string/str2"
                android:textColor="#0A538F"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:text="@string/str4"
                android:textColor="#0A538F"
                android:textSize="30sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:background="#DADADA"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="@string/str5"
                android:textColor="#000000"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/textView8"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:text="@string/str6"
                android:textColor="#0A538F"
                android:textSize="30sp" />

            <Button
                android:id="@+id/button2"
                android:layout_width="135dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="20dp"
                android:background="@drawable/button_fill"
                android:fontFamily="@font/open_sans_semibold"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:text="@string/cont_"
                android:textAlignment="textStart"
                android:textColor="#FFFFFF"
                android:textSize="18sp" />

            <Button
                android:id="@+id/button"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:background="@drawable/code"
                android:text="@string/code"
                android:textAlignment="center"
                android:textColor="#FFFFFF"
                android:textSize="42sp"/>

            <TextView
                android:id="@+id/textView6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>

. . . . . . . . . . I would appreciate any help anyone can provide, I've tried a lot of stuff and nothing seems to work.


Solution

  • Please check below layout, I've updated your layout. I've just added a Framlayout as a parent of two Button.

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorWhite">
    
        <ScrollView
            android:layout_width="395dp"
            android:layout_height="match_parent"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginBottom="8dp"
                android:orientation="vertical">
    
                <ImageView
                    android:id="@+id/imageView4"
                    android:layout_width="243.288590604dp"
                    android:layout_height="100dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginBottom="8dp"
                    android:contentDescription="@string/logo"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.151"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.035"
                    app:srcCompat="@drawable/frag11" />
    
                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:fontFamily="@font/open_sans_semibold"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp"
                    android:text="@string/basic_rules_of_html"
                    android:textColor="#000000"
                    android:textSize="34sp" />
    
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp"
                    android:text="@string/str1"
                    android:textColor="#0A538F"
                    android:textSize="30sp"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="10dp"
                    android:background="#DADADA"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:text="@string/str3"
                    android:textColor="#000000"
                    android:textSize="30sp" />
    
                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp"
                    android:text="@string/str2"
                    android:textColor="#0A538F"
                    android:textSize="30sp" />
    
                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp"
                    android:text="@string/str4"
                    android:textColor="#0A538F"
                    android:textSize="30sp"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/textView7"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="10dp"
                    android:background="#DADADA"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:text="@string/str5"
                    android:textColor="#000000"
                    android:textSize="30sp" />
    
                <TextView
                    android:id="@+id/textView8"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp"
                    android:text="@string/str6"
                    android:textColor="#0A538F"
                    android:textSize="30sp" />
    
                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <Button
                        android:id="@+id/button2"
                        android:layout_width="135dp"
                        android:layout_height="50dp"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="20dp"
                        android:layout_marginRight="20dp"
                        android:layout_marginBottom="20dp"
                        android:background="@drawable/button_fill"
                        android:fontFamily="@font/open_sans_semibold"
                        android:paddingLeft="20dp"
                        android:layout_gravity="start|center_vertical"
                        android:paddingRight="20dp"
                        android:text="@string/cont_"
                        android:textAlignment="textStart"
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />
    
                    <Button
                        android:id="@+id/button"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="20dp"
                        android:layout_marginRight="20dp"
                        android:layout_gravity="end"
                        android:layout_marginBottom="20dp"
                        android:background="@drawable/code"
                        android:text="@string/code"
                        android:textAlignment="center"
                        android:textColor="#FFFFFF"
                        android:textSize="42sp" />
    
                </FrameLayout>
    
    
                <TextView
                    android:id="@+id/textView6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
    
            </LinearLayout>
        </ScrollView>
    
    </android.support.constraint.ConstraintLayout>