Saturday, August 30, 2008

accessing @android:id in the java code

In the notepad v1 tutorial, you set up a list with two views in it - one with the id "@android:id/list" and one with "@android:id/empty".

These are special id's used under a ListActivity. The first defines the view to use for the list and the second the view to use when there is no data in the list.

I was wanting to obtain a handle to the list view to set the background image. I was unable to find how to access it given that the id is not defined in the autogenerated R.java. After a bit of hunting, I found that it is defined under android.R.id.list.

The code I used to set the image was:


View myView = findViewById(android.R.id.list);
myView.setBackgroundResource(R.drawable.icon);


A better way to do this however is to use the xml file to define the background image:


<ListView android:id="@android:id/list" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/icon"></ListView>

No comments: