Android: how to change the position of ExpandableListView Indicator?
Asked Answered
J

5

9

I want to change the position of the default arrow that appears in the Group view of the ExpandableListView. I want it to be to the right instead of being to the left.

how can this be done ?

Jetport answered 27/2, 2011 at 10:37 Comment(0)
P
9

Use setIndicatorBounds:

Sets the drawing bounds for the indicators (at minimum, the group indicator is affected by this; the child indicator is affected by this if the child indicator bounds are set to inherit).

Premeditation answered 6/2, 2012 at 8:22 Comment(0)
A
29

Actually there is a simple way.

  1. Edit your XML related to the group view like this:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="35dp"
    android:orientation="horizontal" >
    
    <TextView
        android:id="@+id/videos_explist_groupname"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/videos_group_indicator"
        android:gravity="center"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textAppearance="?android:attr/textAppearanceListItem" />
    
    <ImageView
        android:id="@+id/videos_group_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/videos_chevron_collapsed" />
    
    </RelativeLayout>
    

    See how the ImageView is on the right of the TextView.

  2. Edit your ExpandableListAdapter as follows:

    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) {
        View v;
        if (convertView == null) {
            v = newGroupView(isExpanded, parent);
        } else {
            v = convertView;
        }
        bindView(v, mGroupData.get(groupPosition), mGroupFrom, mGroupTo);
        ((ImageView) v.findViewById(R.id.videos_group_indicator))
            .setImageResource(isExpanded?R.drawable.videos_chevron_expanded:R.drawable.videos_chevron_collapsed);
        return v;
    }
    

    As you can see you can easily set the drawable corresponding to the group state.

I know the question was asked a long time ago, but this may help somebody.

Akin answered 31/1, 2012 at 23:12 Comment(4)
What do you put in the indicator tag inside the expandablelistview one?Hereditary
android:groupIndicator="@null"Akin
@NagarjunaReddy: I'm sorry, what?Akin
@Bicou see this #15501913Perfusion
P
9

Use setIndicatorBounds:

Sets the drawing bounds for the indicators (at minimum, the group indicator is affected by this; the child indicator is affected by this if the child indicator bounds are set to inherit).

Premeditation answered 6/2, 2012 at 8:22 Comment(0)
N
6

Try this

expList = getExpandableListView();
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
//this code for adjusting the group indicator into right side of the view
expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));

And the GetDipsFromPixel is here

public int GetDipsFromPixel(float pixels)
{
   // Get the screen's density scale
   final float scale = getResources().getDisplayMetrics().density;
   // Convert the dps to pixels, based on density scale
   return (int) (pixels * scale + 0.5f);
}
Needlework answered 28/11, 2012 at 12:21 Comment(0)
G
0

enter image description here

if you want to change the position indicator from the left to right side you need to add android:layoutDirection= "rtl". but the position of list change to the right side.

the example code is look like this

<ExpandableListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    android:layoutDirection="rtl"
    />
Galvani answered 14/7, 2021 at 5:26 Comment(0)
G
-3

There is no direct way to do this.

mIndicatorLeft and mIndicatorRight are the two fields in ExpandableListView class that handle the location of the indicator icon and they are private fields. Unfortunately the position of indicator is not set in a neatly defined Layout as expected, but is determined on dispatchDraw() in the ExpandableListView. A rectangle is drawn using the top/bottom and the mIndicatorRight and mIndicatorLeft values, the indicator icon is drawn in this Rect. You must change the position of this Rect obj named 'indicatorRect' to move your indicator icon.

To get an idea of things you can see this example http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html Change the value of textView padding to 0 in getGenericView() in the example, you will see that the indicator will overlap the text.

Your only option is to get the source code of ExpandableListView and set these mIndicatorRight and mIndicatorLeft values yourself by setting the mIndicatorRight as item.getRight() and mIndicatorLeft as item.getRight() - 'your icon size'.

Glossematics answered 27/2, 2011 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.