Today I am going to cover Lists. Lists can be made several ways, today I will show you how to make a static text based list. After we are displaying the list I will then show you how to send the text from the list to a Toast (popup) when the item is clicked.
Lets start with the XML.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fulllist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
This sets up a basic ListView widget to hold the list data and display it to the screen.
Now we want to make an Activity like normal but for this example your class should extend ListActivity. We will then create a list of movie monsters.
So speaking of… Here is the list of monsters.
static final String[] MONSTERS = new String[] { "Zombie", "Vampire", "Warewulf", "Blob" };
Now for the basic onCreate method:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
Now we need to make an adapter to hold the monsters. After the super call add:
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MONSTERS));
android.R.layout.simple_list_item_1 is a default xml view for displaying a list. This can be customized for a variety of list displays.
Now lets make a ListView object that links to the XML widget:
ListView lv = getListView(); // Grabs the ListView widget that is tied to the Activity. lv.setTextFilterEnabled(true); // Enable the Text Filter. Typing when this view has focus will filter the children to match the users input
Now we are going to create the click listener and the onItemClick event
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// Do Stuff Here
}
});
Lastly we will add the Toast call, put this in the onItemClick stub:
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),Toast.LENGTH_SHORT).show();
Here is the whole Class:
public class Lister extends ListActivity {
static final String[] MONSTERS = new String[] { "Zombie", "Vampire", "Warewulf", "Blob" };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MONSTERS));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),Toast.LENGTH_SHORT).show();
}
});
}
}
Related Posts
2 Comments to “Adventures In Android”
Post comment
popular posts
- Adventures In Android (2)
- Custom Android Buttons (0)
- My Own Tutorials... (0)
- The AndroidManifest.xml file.... (Updated) (0)
Archives
- September 2011 (1)
- May 2011 (4)
- March 2011 (1)
- January 2011 (3)
- December 2010 (1)


iPaulPro says:
Hey Wayne, some suggestions:
- It’s actually best to put your XML Drawable selector files in the /drawable directory, and the scaled graphics in the -hdpi, -mdpi, etc.
- Check out the draw9patch tool (part of the sdk/tools). It allows you to specify stretchable regions on a .png image. This allows you to create smaller graphics, that stretch to all densities and sizes. This is the best method for Buttons. You can see an example of these in the sdk (sdk/platforms/android-8/data/res/drawable-hdpi/btn_default_normal.9).
- For something like a button, it’s also more efficient to use the /res/values/styles.xml file, rather than inline styling (similar to embedded vs external CSS). Email me if you’d like more details on this.
Hope that helps!
Annetta Chain says:
This design is spectacular! You obviously know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!