Start Button from Right to Left Instead of Left to Right

Viewed 179

I am trying to make it so that the Button in the LinearLayout starts from the right side and to set the Button 5dp from the right edge of the LinearLayout, with the TextView showing to the left of the button. Currently, it only allows to set the button from the "left" edge of the linear layout. I've tried all sorts of ways but nothing seems to work.

<LinearLayout
    android:id="@+id/remote_button_layout"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:layout_marginTop="30dp"
    android:background="@drawable/btn_remote_button"
    android:orientation="horizontal">

    <TextView
        android:text="Example Text"
        android:textSize="10sp"
        android:fontFamily="@font/mmedium"
        android:textAllCaps="false"
        android:layout_width="125dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@android:color/secondary_text_light"/>

    <Button
        android:id="@+id/btn_remote_button"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="40dp"
        android:layout_height="match_parent"
        android:layout_marginEnd="5dp"
        android:background="@drawable/btn_remote_button"
        android:drawableStart="@drawable/logo"
        app:layout_constraintHorizontal_bias="1.0" />

</LinearLayout>
5 Answers

You can set android:layout_weight=1 parametr to TextView

Use layoutGravity tag in your xml as (right, left)

You have to add android:gravity="right" in Layout. So all the inner widgets will be set from right to left manner. Like below.

<LinearLayout
                android:id="@+id/remote_button_layout"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginTop="30dp"
                android:gravity="right
                android:background="@drawable/btn_remote_button"
                android:orientation="horizontal">

In your case you can just change add the android:gravity="right" attribute in your LinearLayout.

  <LinearLayout
      android:gravity="right"

Before:
enter image description here After:
enter image description here

You can also consider to do something different using a MaterialButton adding the text and the icon.

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        app:icon="@drawable/..."
        app:iconGravity="end"
        android:text="@string/..."/>

enter image description here

Related