ListView List Style - Left Alignment and Right Alignment

I am trying to make the ListView string look like this:

| Text-Text-Text <ImageButton> | 

When you click the image button on the right edge. How can i do this? Here is the current layout code I'm using. What am I doing wrong?

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layercontainer" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#699"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="left"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="YO HOW SI IT GOESSDA" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="right"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/trash" /> </LinearLayout> </LinearLayout> 

My code currently produces the following: grrr

+6
android android-layout android-listview android-xml
source share
2 answers

Step # 1: use the RelativeLayout base.

Step # 2: Put your ImageButton as having android:layout_alignParentRight="true"

Step # 3: Enter a TextView value of android:layout_alignParentLeft="true" , android:layout_toLeftOf="..." (where ... is the identifier of your ImageButton ) and, possibly, some other RelativeLayout.LayoutParams value for vertical alignment

+16
source share

The following code snippet shows an example of creating a ListView row with some text (horizontal alignment to the left, via android:layout_alignParentLeft="true" ) and an icon (horizontal alignment through android:layout_alignParentRight="true" ) and all vertically ( android:layout_centerVertical="true" ).

It looks like this (YMMV, depending on global styles):

Rendering of code example

There is also a comment icon that can be placed to the left of the largest icon; remove the XML comment tags to include.

Here is the XML layout:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:descendantFocusability="blocksDescendants" android:padding="6dp"> <!-- Note: android:descendantFocusability="blocksDescendants" set to ensure that OnItemClickListener works by ensuring constituent controls do not take focus --> <TextView android:id="@+id/lbl_list_item_row_text" android:layout_height="wrap_content" android:layout_width="fill_parent" android:lines="1" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="My List Item Text" /> <!-- This can be uncommented to add another button <ImageButton android:id="@+id/btn_additional_icon" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/icon_additional_icon" android:layout_centerVertical="true" android:layout_width="wrap_content" android:padding="3dp" android:background="@null" android:src="@drawable/icon_additional_icon" /> --> <ImageButton android:id="@+id/icon_additional_icon" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@null" android:padding="3dp" android:src="@drawable/icon_right_aligned_icon" /> </RelativeLayout> 
+2
source share

All Articles