Android Lists: ListActivity and ListView III – Basic Layout and Formatting

Continued from: Android Lists: ListActivity and ListView II – Custom Adapter and List Item View

When I left off, we ended up with a ListActivity that displayed this:

Now, it’s a good start, but it would be even better if it was more readable.  There is plenty of empty space on the screen right now, so we might as well use it.  For starters, we can space out the ticker symbol and the quote, as well as adjust the font size to make everything easier to read.
If we pop open the res/layout/stock_quote_list_item.xml file, we can see that it’s quite plain right now, and it specifies very minimal layout specific attributes.  The LinearLayout that the list item is currently using makes it so that all elements in the LinearLayout container are displayed one after another in a single row (or column, depending on orientation).  Instead, we can switch the list item to use RelativeLayout, which allows us to specify the layout of the elements in the RelativeLayout container in relation to the parent element, which is the actual RelativeLayout container in this case.

Enough talk, let’s see what the modification of LinearLayout to RelativeLayout will do for our Stock Viewer application.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/ticker_symbol"
        android:layout_alignParentLeft="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/ticker_price"
        android:layout_alignParentRight="true"  />
</RelativeLayout>

You’ll notice I also added an additional attribute to each TextView called android:layout_alignParentLeft or android:layout_alignParentRight.  These two particular attributes are specific to the RelativeLayout, and you’ll notice if you’d just add these attributes to the TextViews without changing the layout to a RelativeLayout, you won’t see any effect.  When set to true, these attributes make the left or right edge of the TextView match the left or right edge of the RelativeLayout container.  Let’s take a look at what this has accomplished.

Hey, we’re making some progress.  The two values are now spaced apart and it’s definitely easier to read than it was in the beginning.  Let’s continue our layout and formatting journey by changing the color of the ticker symbol to white, making the font size larger, and adding some space between the list item separator.  Add the following four attribute values to the ticker symbol TextView:

android:textColor="@android:color/white"
android:textSize="20sp"
android:paddingTop="4dp"
android:paddingBottom="4dp"

If you’re relatively new to Android, you might be wondering what in the heck “dp” and “sp” are and why aren’t we using something like “px” to specify font size and padding sizes.  As you probably know, Android devices come in all shapes and sizes, which means the displays on these devices are not equal. One of the major differences between displays is the pixel density, meaning displays have a variable number pixels per inch.  So, 160 pixels (160px) on one screen might be equal to an inch, where on another screen 160 pixels might only be equal to half of an inch due to an increased pixel density.

To deal the pixel density variations between screens, Android defines a density-independent pixel (dp) display unit that takes the device’s display density into consideration when calculating the size.  The scale-independent pixel (sp) display unit is similar to the density-independent pixel except it scales the size by the user’s font size preference as well.  While Android will allow you to use the pixel (px) display unit, it is suggested that all layout values use density-independent pixels and all font values use scale-independent pixels.

Now that that explanation is out of the way, this is what we were able to accomplish with those four attribute values:

Uh-oh! Now that we’ve increased the size of the ticker symbol font, the quote price value looks funky because it is being pushed to the top of the RelativeLayout container.  Android makes it easy to center items within a RelativeLayout by setting the android:layout_centerVertical attribute to true.

That’s more like it.  While this isn’t going to be the next big Android app, this is surely progressing along nicely from where were previously left off.  The two text values in each list item are now distinct and separated from one another, which makes for a better application experience.

Final Code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ticker_symbol"
        android:layout_alignParentLeft="true"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/ticker_price"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"  />
</RelativeLayout>

Tags: , , ,

This entry was posted on Saturday, April 9th, 2011 at 12:53 pm and is filed under Android. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

6 Responses to “Android Lists: ListActivity and ListView III – Basic Layout and Formatting”

venki June 8th, 2011 at 9:45 am

will you please provide the code for stock ticker which will take the yahoo finance service please i am really need of that

Austin Rasmussen June 8th, 2011 at 8:25 pm

Hi venki,

Could you provide the URL or description of the Yahoo! Finance API you’re talking about? The only one I was able to find was one that sent back a CSV file. Is this the one you are referring to?

Graham June 15th, 2011 at 4:33 am

Hi, Neat blog, but I am a beginner and would be grateful if you could show how the classes finally fit together. Presumably the first activity is; src/com/austinrasmussen/stockviewer/StockList.java.
If so, how has it been modified to call the other classes, or have I missed something? I am still trying to convert my mindset from VB logic to android…

Austin Rasmussen June 15th, 2011 at 7:50 am

Hi Graham,

Thanks and great question.

This particular article is only discussing the layout and formatting of the stock ticker list item. The previous article (ListActivity and ListView II – Custom Adapter and List Item View) shows how to create the custom adapter, which is where most of the code is.

You are correct in saying that StockList.java is main entry point. This is also the place where the static list of StockQuote objects are created.

You can see that on line 23 of StockList.java, a new instance of StockListAdapter is created, which is essentially the data source of the ListView.

Let me know if this isn’t what you were looking for, and I can try to explain it in more detail!

PANTELIS SEISOGLOU February 22nd, 2012 at 8:05 am

Great tutorials !!! I just found them and I will study them. I hope that I will find the solution to my problem : xml parsing after calling a web service.

One more thing if you can help : I’ve built an android app that will call a URL with coords from GPS every 15 minutes. It’s not clear to me how to make it a background service and how to repeatedly let it do that call for a predefined interval of time (e.g. for 9:00 am to 18:00 pm)

Thank you in advance !!! Wait to hear from you !!!

Austin Rasmussen February 22nd, 2012 at 6:52 pm

Fantastic question, Pantelis!

I’ve been wanting to getting around to setting up a background service process, and I unfortunately don’t have a blog entry up about that yet.

I do plan to get to that relatively soon.

Leave a Reply

You must be logged in to post a comment.