pippo
pippo

Reputation: 824

Android - custom listview and highlight

I've tried a lot to get things working, but without any sort of success.

I have implemented a custom listview that displays 3 textviews, added the custom adapter of my hands and things work correctly, I've done the registerForContextMenu(View of listview) and when I press items displayed, it shows a perfect blue highlight around my item, and when I press it long the same happens and then it shows me the menu. Okay. Then I added a button inside my custom listview displaying one color if certain things happen, displaying another one viceversa. After I modified my custom adapter to include my button and setting the change-color logic, if I long press my items I have no more highlight around, but the context menu is preserved.

Why is this happening?

I tried a lot searching on Stack and Google, but every solution I found was not working with my app. I tried also to insert a custom selector, it works fine when I exclude the button from my listview design, I suppose the button is the culprit, but I can't find out a way to resolve this problem. I suppose is something related to the "background" - "drawable" of the button, but I am here to ask you some help.

Thanks in advance.

Some code if it can interest you:

listviewdesign.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

>
<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="5dp"
    android:layout_centerVertical="true"
    android:gravity="center"
    />
<TextView
    android:id="@+id/text2"
    android:singleLine="false"
    android:maxEms="8"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dp"
    android:layout_toLeftOf="@+id/text3"
    android:layout_toRightOf="@+id/text1"
    android:layout_centerVertical="true"
    android:gravity="center"
    />
<TextView
    android:id="@+id/text3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="5dp"
    android:layout_toLeftOf="@+id/btn1"
    />


<Button
    android:id="@+id/btn1"
    android:layout_width="10px"
    android:layout_height="10px"
    android:layout_alignParentRight="true"
    android:layout_marginTop="20dp"


    />
 </RelativeLayout>

mainactivitylayout.xml:

<LinearLayout ...>
<ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        (when I add selector: android:listSelector="@xml/myselector")
        android:clickable="false"
        />
    </LinearLayout>

And my selector for completeness:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
class="class of project">
<item android:state_pressed="true"
    android:drawable="@drawable/pressed"/>
<item android:state_focused="true"
    android:drawable="@drawable/focused"/>
<item android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@drawable/normal"/>
<item android:drawable="@drawable/normal"/>
</selector>

Where those drawable items are correctly setup such as:

<resources>
<drawable name="pressed">#FF00A5FF</drawable>
<drawable name="focused">#FF0000FF</drawable>
<drawable name="normal">#00000000</drawable>
</resources>

Thanks!

Upvotes: 0

Views: 167

Answers (1)

Ajith Pandian
Ajith Pandian

Reputation: 1342

You need to place "myselector.xml" in drawable folder.

    android:listSelector="@xml/myselector" 

this line should be

    android:listSelector="@drawable/myselector"

Upvotes: 1

Related Questions