Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expandable list view move group icon indicator to right

As regards to expandable list view, the group icon indicator is positioned to the left side, can I move the indicator and positioned it to the right? Thanks.

EDITED: Since I don't want to extend the view, I got this workaround of getting the width dynamically. Just sharing my solution.

Display newDisplay = getWindowManager().getDefaultDisplay(); 
int width = newDisplay.getWidth();
newListView.setIndicatorBounds(width-50, width);
like image 386
iamtheexp01 Avatar asked Apr 27 '11 06:04

iamtheexp01


3 Answers

setIndicatorBounds(int, int) does not work properly for Android 4.3. They introduced a new method setIndicatorBoundsRelative(int, int) which works ok for 4.3.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
       mExpandableListView.setIndicatorBounds(myLeft, myRight);
    } else {
       mExpandableListView.setIndicatorBoundsRelative(myLeft, myRight);
    }
}
like image 165
luben Avatar answered Sep 21 '22 13:09

luben


All XML solution! I found a better way of tackling this issue. This does not require you to mess with display metrics, no need to programmatically hack it.

This is what you need to do:

1) set layout direction to right-to-left

<ExpandableListView
...
android:layoutDirection="rtl" />

2) make sure your list item layout includes this (yes, you'll need custom layout):

android:gravity="left" //to adjust the text to align to the left again
android:layoutDirection="ltr"  //OR this line, based on your layout

And you are good to go!

like image 43
goodKode Avatar answered Sep 24 '22 13:09

goodKode


The best way to do this is below but not for all Android Version So use 2nd or third method specified below

ViewTreeObserver vto = mExpandableListView.getViewTreeObserver();

    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
                 mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth());
        }
    });

or this:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth());
} 

will move that indicator to right of list view even on device and on tab also.

or you can do this in a postdelayed thread.

 (new Handler()).post(new Runnable() {

        @Override
        public void run() {
              mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth());
        }
    });
like image 45
varun bhardwaj Avatar answered Sep 23 '22 13:09

varun bhardwaj