Question

WARNING: The XML in this question is wrong, read the answer before you confuse yourself!


I have been banging my head on the wall for a while now. The following posts have shed light on the subject, but failed to solve my issue: Android ListView State List not showing default item background and ListView item background via custom selector

The proper nine-patch background shows perfectly when I select the list item, but I can not get the default nine-patch background to show initially. It seems to me that I need to set the default item background somehow, but I can't figure out how to do so.

List View:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/shopListHeader"
    />
  <ListView
    android:id="@+id/shopList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:dividerHeight="0px"
    android:divider="#FFFFFFFF"
    android:listSelector="@drawable/shop_list_selector"
    />
</LinearLayout>

Selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
  android:visible="true">

    <!-- the list items are enabled and being pressed -->
    <item
        android:state_pressed="true"
        android:drawable="@drawable/shop_list_item_pressed" />

    <item
        android:state_selected="true"
        android:textColor="#FFFFFFFF" />
</selector>

Background:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
  android:visible="true">

    <item
      android:state_selected="true"
      android:drawable="@android:color/transparent" />

    <item
        android:drawable="@drawable/shop_list_item" />
</selector>

As you can see, I have dumbed down the examples from the references.

You may also notice that the Background selector isn't being referenced anywhere. I started tossing it in random places (if the app compiled the addition either had no effect or cause a forced close)

I have also made an attempt to stop the color of the text from changing to black and grey when an item is selected but not pressed (can be done by scrolling through the list). Since my background will be black in the center, the text becomes partially invisible when selected. That addition (the last item node in the Selector) does nothing, as far as I can tell.

Does anyone have any thoughts on getting this ridiculously time consuming functionality working?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top