M.Bill
M.Bill

Reputation: 37

how to get image icon to align right in a Linear Layout?

I know there are lots of threads with more or less same topic but none of them covers my situation:

I have a little garbage can for the delete button but it always disappears off the screen if the line of the text is too long and if its too short it's attached to the line and I just wanted to be on the right hand side of the app like the the Add_item button but since it is in a Linear Layout it does not work

ativity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.owner.test24.MainActivity">

<TextView
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/header"
    android:gravity="center_vertical"
    android:textColor="@color/accent"
    android:textSize="24sp" />
 <ImageButton
    android:id="@+id/add_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:layout_alignEnd="@+id/header"
    android:layout_alignRight="@+id/header"
    android:src="@drawable/add_item_button"
    android:contentDescription="@string/add_item"/>
<ListView
    android:id="@+id/todo_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/header"/>

 </RelativeLayout>

to_do_item_layout.xml

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

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"/>

<ImageButton
    android:id="@+id/btnDel"
    android:src="@drawable/ic_delete_button"
    android:background="@android:color/transparent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Delete"
    />

if someone has solution please let me know.

Upvotes: 0

Views: 2765

Answers (4)

Payal
Payal

Reputation: 913

Use Weight:

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

<CheckBox
android:id="@+id/checkBox"
android:layout_width="0dip"
android:layout_height="wrap_content"
amdroid: layout_weight="0.2"
/>

 <ImageButton
android:id="@+id/btnDel"
android:src="@drawable/ic_delete_button"
android:background="@android:color/transparent"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Delete"
amdroid: layout_weight="0.2"
/>

Upvotes: 0

shahid17june
shahid17june

Reputation: 1577

use it, it works on every screen size

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

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_weight="0.03"/>

    <TextView
        android:id="@+id/item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hhhhh"
        android:gravity="center_horizontal"
        android:textSize="20sp"
        android:layout_weight="0.87"/>

    <ImageButton
        android:id="@+id/btnDel"
        android:src="@drawable/delete_icon"
        android:background="@android:color/transparent"
        android:layout_width="wrap_content"
        android:layout_gravity="end|center_vertical"
        android:layout_height="wrap_content"
        android:text="Delete"
        android:layout_weight="0.1" />
    </LinearLayout>

Upvotes: 2

Md Sufi Khan
Md Sufi Khan

Reputation: 1761

Try below one

<TextView
    android:id="@+id/item"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="20sp"/>

Hopes it will solve your problem. :)

Upvotes: 2

Lubomir Babev
Lubomir Babev

Reputation: 1908

Add layout_gravity tag

android:layout_gravity="right"

Upvotes: 0

Related Questions