How to right align widget in horizontal linear layout Android? How to right align widget in horizontal linear layout Android? android android

How to right align widget in horizontal linear layout Android?


Try to add empty View inside horizontal LinearLayout before element that you want to see right, e.g.:

<LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content" >    <View        android:layout_width="0dp"        android:layout_height="0dp"        android:layout_weight="1" />                    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>


Do not change the gravity of the LinearLayout to "right" if you don't want everything to be to the right.

Try:

  1. Change TextView's width to fill_parent
  2. Change TextView's gravity to right

Code:

    <TextView               android:text="TextView"               android:id="@+id/textView1"              android:layout_width="fill_parent"              android:layout_height="wrap_content"                 android:gravity="right">    </TextView>


As a supplement to alcsan's answer, you can use Space since API 14 (Android 4.0 ICE_CREAM_SANDWICH), document here.

Space is a lightweight View subclass that may be used to create gaps between components in general purpose layouts.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"     android:orientation="horizontal" >    <Space        android:layout_width="0dp"        android:layout_height="0dp"        android:layout_weight="1" />    <TextView        android:layout_width="wrap_content"         android:layout_height="wrap_content"        android:text="TextView"        android:gravity="right" /></LinearLayout>

For apps that supporting API levels under 14, there is an android.support.v4.widget.Space since Android Support Library r22.1.0.