简体   繁体   中英

highlight selected item on listview

before someone marked this question as duplicate, I must say I already know that this question is already asked several times, but every time I tried I always failed.

so I have a listview of date on the left and I want to show data on the right, I want to make a listview that can show which item is selected so I know which date that I picked. So far I've tried this;

on listview I've added android:choiceMode="singleChoice" ;

<ListView
        android:id="@+id/listTglRMPasien"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnObatAlkes"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" 
        android:choiceMode="singleChoice">
</ListView>

on item of listview I've added android:background="@drawable/list_selector" ;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/txtListRekamMedikTanggal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/emptyString"
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:background="@drawable/list_selector"/>

    <TextView
        android:id="@+id/txtListRekamMedikTabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/emptyString"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:visibility="gone" />

</LinearLayout>

and on list_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_launcher" android:state_activated="false"/>
    <item android:drawable="@drawable/ic_launcher" android:state_pressed="false"/>
    <item android:drawable="@drawable/blue" android:state_pressed="true"/>
    <item android:drawable="@drawable/blue" android:state_activated="true"/>
</selector>

note that ic_launcher and blue here are png file, it's supposed to be "@drawable/listitem_pressed" and "@drawable/solid_white", I find this answer from here but somehow it got an error, it says No resource found that matches the given name and I think something is missing so I changed it to png file

anyway, when I tried to run this, it always show ic_launcher whether I select it or not, it never show blue .

apparently it happens because my API level is too low, I changed it from API level 8 to API level 14.

this is a good answer about how to change min. API level for your Android app in Eclipse

Here is what I did on my ListView. Create your own theme on res/values folder, you can name it for example mytheme.xml. Here what I added on mytheme.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="mytheme" parent="android:Theme.Holo.Light">
   <item name="android:textColor">#fff</item>
</style>
</resources>

Then in your Manifest add this android:theme="@style/mytheme" inside the activity of your ListView Class.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM