Question

I've followed Samsung Developer's tutorial on using fragments.

My listview menu has a white background (blue when activated) and white text, as opposed to the listview menu in the tutorial which is darker. I'm thinking it's because I'm using the Holo Light theme. How do I make my listview menu more readable by for example changing the font color in the listview?

Edit: When I change theme to Holo (Dark), the menu looks good. However, I want it to have black text color on white background.

I have tried to create a layout file called simple_list_item_activated_1.xml but that didn't make any difference. I also tried changing simple_list_item_activated_1 to simple_list_item_activated_2 and simple_list_item and so on but that also either didn't make any difference and in some cases made the app force quit when I ran it.

FragmentA.java:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter(getActivity().
    getApplicationContext(), android.R.layout.simple_list_item_activated_1, tutorialList));
}

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
Was it helpful?

Solution

From ArrayAdapter Overview:

By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource. (...) To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

Depending on your needs you can provide custom TextView defined in XML file or more complex layout and use it by overriding getView() method. First option might be enough for you, just create custom textview with black text and white background.

UPDATE

Sample code

/res/drawable/row_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <color android:color="#0CC" />
    </item>
    <item>
        <color android:color="#FFF" />
    </item>
</selector>

/res/layout/sample_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:gravity="center_vertical"
      android:background="@drawable/row_background"
      android:textColor="#000"
      android:minHeight="?android:attr/listPreferredItemHeight"
      android:clickable="true" />

And FragmentA.java

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter(
        getActivity().getApplicationContext(),
        R.layout.sample_row,
        tutorialList)
    );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top