Search code examples
androidlistviewandroid-fragmentsandroid-tabhost

Listview item got highlighted on click (not wanted)


I've a TabHost with items and I want that they are only highlighted on click.

I had it before and I changed nothing on the ListViews itself but on the TabHost (and I think that shouldn't matter..)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="at.htl3r.appmosphere.MainActivity$PlaceholderFragment" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <ListView
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </ListView>

                <ListView
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </ListView>

                <ListView
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </ListView>

                <ListView
                    android:id="@+id/tab4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </ListView>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

I read some posts how to disable it, but I don't want to disable it complete because I'll handle a click action

I tried adding choiceMode="none" and a selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/darkgreen_light" android:state_pressed="true"/>
    <item android:drawable="@android:color/transparent" android:state_activated="true"/>
    <item android:drawable="@android:color/transparent" android:state_checked="true"/>
    <item android:drawable="@android:color/transparent" android:state_selected="true"/>
    <item android:drawable="@android:color/transparent"/>

</selector>

UPDATE

better choice: ViewPagerIndicator


Solution

  • You should keep your listviews' choice mode to single choice (to be able to detect item selectsions / taps), and override the list selector to be transparent (@null works for older versions of android but not 16+):

    <style name="YourTheme.ListView" parent="@android:style/Widget.ListView">
        <item name="android:listSelector">@drawable/listSelector</item>
        <item name="android:choiceMode">singleChoice</item>
    </style>
    
    <?xml version="1.0" encoding="utf-8"?>
    

    where your listSelector.xml could look like this:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@color/darkgreen_light" android:state_pressed="true"/> <!-- pressed -->
        <item android:drawable="@color/darkgreen_light" android:state_focused="true"/> <!-- focused -->
        <item android:drawable="@android:color/transparent"/> <!-- default -->
    
    </selector>
    

    Update:

    If you don't want any visual feedback on item click, just change the @drawable/listSelector in the listview's style to @android:color/transparent.