2

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>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
rengineer
  • 349
  • 3
  • 19

5 Answers5

1

You can set android:layout_weight=1 parametr to TextView

Pratik Satani
  • 1,115
  • 1
  • 9
  • 25
Dmitriy Puchkov
  • 1,530
  • 17
  • 41
1

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

1

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">
Pratik Satani
  • 1,115
  • 1
  • 9
  • 25
  • If I were to make the identical button and textview on the left(on each side now), would I need to make a separate LinearLayout and combine it? – rengineer Oct 17 '19 at 08:04
1

Use this android:layout_gravity as (left,right) you can refer this link What is the difference between gravity and layout_gravity in Android?

1

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841