Expandable list view move group icon indicator to right
According to ExpandableListView
's source code, the only way to move an indicator to the right side is to change its bounds using ExpandableListView.setIndicatorBounds()
method. You can calculate bound in onSizeChanged()
method, for example.
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!
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);
}
}
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());
}
});