Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9178475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:33:37+00:00 2026-06-17T17:33:37+00:00

I have a layout where I have an expandable list in a fragment on

  • 0

I have a layout where I have an expandable list in a fragment on the left and a details fragment on the right. This all works fine.

Now I would like to indicate what item on the left is having it’s details shown on the right and here I’m having a problem.

In a normal list view I have achieved this by setting the choice mode for the list view to single and then using a state drawable based on the “activated” state. When I click the item, the background is set to my selected color and stays that way until I select another item on the list.

I tried to apply this to my expandable listview and it was/is a dismal failure. There were no errors, but the selected item did not maintain it’s color state. I’m not sure if I’m not setting the choice mode properly for it (I’ve tried it in the layout file as well as programatically, it doesn’t seem to make a difference), or pointing it to the wrong thing (not sure how that could be, but…)

Any help/pointers appreciated (even if it’s in a completely different direction).

Most current failure:

expandable listview code

private void fillData(String group, String child) {
    ExpandableListView lv;
    mGroupsCursor = mDbHelper.fetchGroup(group);
    getActivity().startManagingCursor(mGroupsCursor);
    mGroupsCursor.moveToFirst();
    lv = (ExpandableListView) getActivity().findViewById(R.id.explist);
    lv.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);        

    mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
            R.layout.explistlayout,
            R.layout.explistlayout,
            new String[] { "_id" },
            new int[] { android.R.id.text1 },
            new String[] { child },
            new int[] { android.R.id.text1 });
    lv.setAdapter(mAdapter);
    registerForContextMenu(lv);

    lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
        @Override 
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)  
        { 
            mRowId  = id;
            EventDisplayFragment eventdisplay = new EventDisplayFragment();
            getFragmentManager().beginTransaction().replace(R.id.rightpane, eventdisplay).commit();
            return true; 
        } 
        }); 
}

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

    public MyExpandableListAdapter(Cursor cursor, Context context,
            int groupLayout, int childLayout, String[] groupFrom,
            int[] groupTo, String[] childrenFrom, int[] childrenTo) {
        super(context, cursor, groupLayout, groupFrom, groupTo,
                childLayout, childrenFrom, childrenTo);
    }
    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        Cursor childCursor = mDbHelper.fetchChildren(mGroup, groupCursor
                .getString(groupCursor
                        .getColumnIndex(AttendanceDB.EVENT_ROWID)));
        getActivity().startManagingCursor(childCursor);
        childCursor.moveToFirst();
        return childCursor;
    }
}

item_selector.xml

<?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/green" />
    <item 
     android:state_selected="true" 
     android:drawable="@color/blue" />
    <item 
     android:state_focused="true" 
     android:drawable="@color/violet" />
    <item 
     android:state_activated="true" 
     android:drawable="@color/blue" />
</selector>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:33:38+00:00Added an answer on June 17, 2026 at 5:33 pm

    Do the following on ExpandableListView:

    Step 1. Set choice mode to single (Can be done in xml as android:choiceMode = “singleChoice”)

    Step 2. Use a selector xml as background (android:listSelector = “@drawable/selector_list_item”)

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"
       android:exitFadeDuration="@android:integer/config_mediumAnimTime">
    
       <item android:drawable="@android:color/holo_blue_bright" android:state_pressed="true"/>
       <item android:drawable="@android:color/holo_blue_light" android:state_selected="true"/>
       <item android:drawable="@android:color/holo_blue_dark" android:state_activated="true"/>
    
    </selector>
    

    Step 3. Call expandableListView.setItemChecked(index,true) in onChildClick() callback.

    index is a 0 based index of the child item calculated as follows

    Group 1 [index = 0]

    • Child item 1
    • Child item 2
    • Child item 3 [index = 3]

    Group 2 [index = 4]

    • Child item 1
    • Child item 2
    • Child item 3 [index = 7]

    Group 3 [index = 8]

    • Child item 1
    • Child item 2
    • Child item 3 [index = 11]

    If we have list headers as well, they would also account for index values.

    Here is a working example:

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        ...
    
        int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
        parent.setItemChecked(index, true);
    
    
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Layout (using TableLayout, etc...) which works well. It seems like this:
I have a layout like this in which I want to display a label
I have a layout which looks like this: <LinearLayout> <ImageView> android:width=wrap_content android:height:wrap_content ... </ImageView>
I have a layout with two children one after another, like this: |<view1><text1> |
I have a layout setup which can be view here: http://jsfiddle.net/Pn3ts/ It all works
I have layout like this At the top i have listView ,below that i
Hello i have a simple expandable list activity that was working fine until i
I have layout like this: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=wrap_content android:background=@drawable/bgr> <RelativeLayout android:layout_width=match_parent android:layout_height=wrap_content android:layout_centerVertical=true
I have this code for an expandable list, I want to have a checkbox
I current have an expandable list that I'd like to be able to search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.