How to place two views on opposite side of LinearLayout

1.3k views Asked by At

How can I place the ImageButton on the right side of the screen ?

How it is right now

I think it has to do with "layout_gravity" property, but I can't manage to figure out how to do it. Here is the XML file that creates each line :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/list_generated_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="20dp"
        android:layout_gravity="left|center_vertical" />

    <ImageButton
        android:id="@+id/list_generated_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/ic_refresh"/>

</LinearLayout>
2

There are 2 answers

0
Vikas On BEST ANSWER

Just add one line in your Text View

android:layout_weight="1"

Then all will be done as you wanted

0
Pawan Soni On

use this

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/list_generated_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_weight="1"
        android:textSize="20dp"
        android:layout_gravity="left|center_vertical" />

    <ImageButton
        android:id="@+id/list_generated_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/ic_refresh"/>

</LinearLayout>