0

I'd like to see the highlight when I click the item in ListView but It doesn't work. I googled so long time to fix this but nothing helped.

I didn't have any issue when I created the list in Android native. Here is the XML of layout that worked on Android native project.

<ListView
            android:id="@+id/menuList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/img_logo_arkema"
            android:layout_below="@+id/img_logo_paladin"
            android:background="#ffffffff"
            android:choiceMode="singleChoice" />

But This doesn't work when I create same ListView in Xamarin.Android project. Here is the XML of layout that doesn't work on Xamarin.Android project.

<ListView
        android:id="@+id/menuList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/colorTopDivider"
        android:dividerHeight="@dimen/right_menu_divider_size"
        android:choiceMode="singleChoice" />

I am developing the Xamarin.Android in Visual Studio 2015.

thanks for your help!

Passionate.C
  • 233
  • 3
  • 20

2 Answers2

2

I have tried for several hours and finally fixed it myself.

The problem was that I set the color to background of list item layout. When I removed the background attribute in list item layout, it worked!

Here is more detailed solution.

highlighting the selected item in the listview in android

Community
  • 1
  • 1
Passionate.C
  • 233
  • 3
  • 20
0

Yeah basically your idea is correct Passionate.C. But you can also make it easier with selector, you define a selector in xml (inside the drawble folder)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@android:drawable/list_selector_background" />      
</selector>

Then in your listView you call setSelector() and pass your own selector. This is a more customizable solution because you can also easily control other status of your listView item's

Maurizio Ricci
  • 462
  • 1
  • 4
  • 11
  • 1
    I agree, Maurizio. It's really more customisable solution. But I don't wanted to create new resource for basic highlight. Thanks. – Passionate.C Apr 28 '17 at 07:49