Lately I have been working on adding some great new features to wpToGo, including improving the UI for the comment moderation area. I discovered that the Gravatar API was extremely easy to implement and thought it’d be great to have in my wpToGo’s comment moderation ListView.
Turns out that working with Android Layouts can be fairly tricky these days as there’s now multiple devices in the market that have varying screen sizes. Let’s make a ListView layout that will stretch its elements to fit on any Android device and still look snazzy.
First, let’s take a look at the ListView layout code, nothing too surprising here:
list_view.xml
<ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:textColor="#444444" android:divider="@drawable/list_divider" android:dividerHeight="1px" android:cacheColorHint="#00000000"> </ListView>
The only thing to note there is that I created my own divider gradient named “list_divider” which is an xml file that you throw in your drawable folder. This really isn’t that hard, you just specify the colors for the left, center and right side:
list_divider.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <gradient android:startColor="#999999" android:centerColor="#555555" android:endColor="#999999" android:height="1px" android:angle="0" /> </shape> </item> </layer-list>
So now we have our ListView ready to go, next is the layout for each row in the list. This is also done through a layout xml file that you specify later in your code. I wanted each row to have the Gravatar on the left, with all of the other content on the right so that it mirrored how comments appear on most WordPress blogs. Here’s the layout to get that to work:
row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="4px"> <ImageView android:id="@+id/avatar" android:layout_width="48px" android:layout_height="48px"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="4px"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1" android:gravity="center_vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left" android:textStyle="bold" android:singleLine="true" android:ellipsize="end" android:textColor="#444444" android:padding="0px"/> <TextView android:id="@+id/email_url" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left" android:singleLine="true" android:ellipsize="end" android:padding="0px" android:textSize="10px"/> <TextView android:id="@+id/postTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left" android:singleLine="true" android:ellipsize="end" android:padding="0px" android:textSize="10px"/> <TextView android:id="@+id/comment" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="false" android:textColor="#666666" android:maxLines="5" android:ellipsize="end"/> <TextView android:id="@+id/status" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true"/> </LinearLayout> </LinearLayout> </LinearLayout>
With all of that implemented and doing some background work to load the content, you’ll have a nice looking ListView row comment layout:
And there you have it, a nice looking comment ListView in Android.
24 responses to “Working with the Android ListView”
good article
LikeLike
How do you make these list items clickable ?
I’ve tried setting a onItemClickListener on the listview but it is never called when i’m clicking on the row in the emulator.
Any ideas on this?
LikeLike
It should work with something like this:
LikeLike
Thanks for the useful post especially for beginners like me.I could like to have a background image on listview with Back and Home buttons on the top left and right corners respectively on the screen.what i did is
but i couldn’t make my Home button to place on the right top of the screen .i could appreciate your valuable suggest .moreover i could like to change the default color of the selected list item.
LikeLike
@Ganesh,
You can try implementing your app in RealtiveLayout,
that way you can place the buttons on topleft and topright corners in a single row….
then let the list occupy the rest of the screen….
– Sudhaker
LikeLike
Hie,
Can you show the java code of how to bind the row to the list adapter
LikeLike
All of this code ended up in WordPress for Android. Take a look at the moderateCommentsTab.java source to see how the data was bound:
http://android.trac.wordpress.org/browser/trunk/src/org/wordpress/android/moderateCommentsTab.java
LikeLike
Thanks a lot ….. but it has been removed ….. could you give me another link for it our send it to my ……. mohamed000eg@yahoo.com
LikeLike
Thanks a lot for this! I had no idea you could make a gradient with XML.
LikeLike
What’s place is place in the class file?
LikeLike
How would the listview work with the data from database?
I’m really stuck!
LikeLike
How do you tell the listview to load the row.xml file?
LikeLike
How can you load the row.xml file into listview??
LikeLike
Check out this post which covers binding an adapter to the ListView:
http://android.amberfog.com/?p=296
LikeLike
hi – does this work with the htc touch? i was able to do an update with stock settings – but it won’t let me upload photos….
LikeLike
@varun
public class StaticDemo extends ListActivity {
TextView selection;
String[] items={“lorem”, “ipsum”, “dolor”, “sit”, “amet”,
“consectetuer”, “adipiscing”, “elit”, “morbi”, “vel”,
“ligula”, “vitae”, “arcu”, “aliquet”, “mollis”,
“etiam”, “vel”, “erat”, “placerat”, “ante”,
“porttitor”, “sodales”, “pellentesque”, “augue”,
“purus”};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter(this,
R.layout.row, R.id.label,
items));
selection=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView parent, View v,
int position, long id) {
selection.setText(items[position]);
}
}
LikeLike
this code does not work with me … please could you help?
LikeLike
[…] Working with the Android ListView December 200915 comments 3 […]
LikeLike
Hello wonderful guide/article!
It would be great though if you could also show us the “background work” to make it fully function. I am very new to android and would like to know how to connect row.xml to the listview and also what to enter in the .java file
LikeLike
Hi,
I don’t know how to make the list view to understand that row.xml is responsible for the item layout.
Can you help me?
LikeLike
<ListView android:id="@android:id/list"
I had to use
android:id="@+id/list
Thanks
LikeLike
This is nice post!! its help me lot and the following link also helped me to complete my task.
http://mindstick.com/Articles/802aac4e-b9ef-499e-a20b-f0d0fd56ae11/?Using%20ListView%20in%20Android%20Application
Thanks!!
LikeLike
[…] Working with the Android ListView | Around the HillDec 18, 2009 … Let’s make a ListView layout that will stretch its elements to fit on any Android device and still look snazzy. First, let’s take a look at the ListView … […]
LikeLike
i didnt understand how to connect the row.xml with list_divider.xml with this article
http://android.amberfog.com/?p=296
can you help me please? do you have video of it?
LikeLike