0

I'm trying to add a selector to my expandable listview

I have a background, text color and and image that I need to change state when the item is selected

here's my layout for the group item

<?xml version="1.0" encoding="utf-8"?>
<com.example.views.CheckedRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@color/selector_menu_item_bg"
    android:minHeight="48dp"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/ic_menu"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="15dp"
        android:background="@color/selector_menu_item_pic" />

    <com.example.views.CheckedTextView
        android:id="@+id/txt_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/ic_menu"
        android:text="Dashboard"
        android:textColor="@color/selector_menu_item_txt"
        android:textSize="16dp" />

    <ImageView
        android:id="@+id/ic_drop_down_arrow"
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_play_down" />

</com.example.views.CheckedRelativeLayout>

here are my selectors :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/light_grey"/>
    <item android:state_selected="true" android:drawable="@color/light_grey"/>
    <item android:state_checked="true" android:drawable="@color/light_grey"/>
    <item android:state_activated="true" android:drawable="@color/light_grey"/>
    <item android:drawable="@color/white" />
</selector>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/turquoise"/>
    <item android:state_selected="true" android:color="@color/turquoise"/>
    <item android:state_checked="true" android:color="@color/turquoise"/>
    <item android:state_activated="true" android:color="@color/turquoise"/>
    <item android:color="@color/blue_grey"/>
</selector>

and for the imageView I use this:

private StateListDrawable getStatesForPageId(PageId pageId) {
        StateListDrawable states = new StateListDrawable();
        switch (pageId) {
        case HOME:
            states.addState(
                    new int[] { android.R.attr.state_pressed },
                    context.getResources().getDrawable(
                            R.drawable.ic_menu_dashboard_active));
            states.addState(
                    new int[] { android.R.attr.state_activated },
                    context.getResources().getDrawable(
                            R.drawable.ic_menu_dashboard_active));
            states.addState(
                    new int[] { android.R.attr.state_checked },
                    context.getResources().getDrawable(
                            R.drawable.ic_menu_dashboard_active));
            states.addState(
                    new int[] {},
                    context.getResources().getDrawable(
                            R.drawable.ic_menu_dashboard));
            break;

        case PAGE_1:
            states.addState(
                    new int[] { android.R.attr.state_pressed },
                    context.getResources().getDrawable(
                            R.drawable.ic_menu_transfer_active));
            states.addState(
                    new int[] { android.R.attr.state_activated },
                    context.getResources().getDrawable(
                            R.drawable.ic_menu_transfer_active));
            states.addState(
                    new int[] { android.R.attr.state_checked },
                    context.getResources().getDrawable(
                            R.drawable.ic_menu_transfer_active));
            states.addState(
                    new int[] {},
                    context.getResources().getDrawable(
                            R.drawable.ic_menu_transfer));
            break; 
 }
}

The pressed state works however it doesn't keep the state when pressed.

For the moment I use

listMenu.setItemChecked(selectedMenuItem, true);

but with no luck, on android 4.4 I have the background that stays highlighted but not the textview nor the imageView. On api 8, not even the background is hightlighted.

I've had a look at this https://github.com/theomega/ActivatedStateDemo However I couldn't get it to work with an expandable listview

Mike Bryant
  • 2,455
  • 9
  • 39
  • 60
  • I faced the same problem, I higlighted the selected expListview using custom code. If there is a standard answer, It would be good. – amalBit Mar 22 '14 at 08:32
  • Would you be able to post your solution? – Mike Bryant Mar 22 '14 at 08:50
  • Y not. Let me try and do that for you. – amalBit Mar 22 '14 at 10:05
  • possible duplicate of [Selector Expandable List View - Api 8+](http://stackoverflow.com/questions/22424407/selector-expandable-list-view-api-8) – user Mar 23 '14 at 06:46
  • It is a duplicate however I still haven't found an answer and generally on here if you don't help get within a day of posting the question it gets pushed to the bottom and is never answered.. – Mike Bryant Mar 23 '14 at 10:17
  • That isn't a reason to post the exact question again. You either post a new bounty(when the current one finishes) or you edit the question to improve it. I've recommended something on the other question, did you considered what I said or you just expect code? – user Mar 23 '14 at 11:59
  • I did have a look at the question that you recommended, I read too quickly, it's actually turned out to be very helpful, If you could answer the question in the other thread with the code that I posted as an answer to help future readers. Then I'll accept your answer (delete mine) and give you the bounty, you've earned it, Thanks for your help I appreciate it! – Mike Bryant Mar 23 '14 at 18:19

0 Answers0