Well, here are some things you should know about:
- The background color that you set in your XML file refers to the activity, not to the ListItems that you are trying to define.
- Each list item has its own layout file, which must be transferred or inflated if you use a complex layout for the list item.
I will try to explain this with a sample code:
**** Let's start with the ListItems ** layout : save it in your res/layout folder of your Android project, say ** list_black_text.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center_vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/list_content" android:textColor="#000000" android:gravity="center" android:text="sample" android:layout_margin="4dip" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
Well, a simple layout with TextView , to be precise. You must have an identifier assigned by TextView in order to use it.
Now the screen / activity / main layout will come to you, as I said that you define the background on the screen with the android:background attribute. I see that you also defined a TextView, and I suspect that you are trying to define there a content / list item that is not needed at all.
Here's the edited layout:
<?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" android:background="#FFFFFF"> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
And finally, most importantly, install the adapter.
setListAdapter(new ArrayAdapter<String>( this, R.layout.list_black_text, R.id.list_content, listItems));
Pay attention to the layout resource that we pass to the adapter R.layout.list_black_text , and R.id.list_content , which we declared the ID TextView. I also changed the ArrayAdapter to a String type, as it is generic.
Hope this all explains. Mark my answer if you agree.
A useless but quick way to quickly fix
You can also do this with a quick fix if you don't want to go ahead with defining a complex layout, etc.
While the adapter instance declares an inner class for this, here is a sample code:
ArrayAdapter<String> adapter=new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems){ @Override public View getView(int position, View convertView, ViewGroup parent) { View view =super.getView(position, convertView, parent); TextView textView=(TextView) view.findViewById(android.R.id.text1); textView.setTextColor(Color.BLUE); return view; } }; setListAdapter(adapter);