How to change ExpandableListView group indicator position?
Asked Answered
S

3

10

I want to change the ExpandableListView group indicator to right with padding.

I used custom adapter to load data to ExpandableListView.

This is my ExpandableListView xml.

<ExpandableListView
    android:id="@+id/Ex_offers"
    android:layout_width="250dp"
    android:layout_height="400dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:childDivider="#00000000"
    android:groupIndicator="@drawable/settings_selector"
    android:listSelector="@android:color/transparent">

</ExpandableListView>

This is GroupView xml

 <TextView
        android:id="@+id/lblListHeadertwo"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/transperant_bar"
        android:fontFamily="Lato"
        android:paddingLeft="15dp"
        android:textColor="#daac56"
        android:textStyle="bold"
        android:textSize="17sp"
        android:paddingTop="8dp"  />

This is ChildView xml

 <TextView
        android:id="@+id/text_offers"
        android:layout_width="300dp"
        android:layout_height="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/transperant_bar"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:textColor="#daac56"
        android:textSize="14sp"/>

    <ImageView
        android:id="@+id/img"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_marginLeft="12dp"/>

This is the output image, I want to set margin to this indicator.

enter image description here

Swirly answered 20/3, 2014 at 9:26 Comment(0)
M
23

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.

public int GetPixelFromDips(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);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels; 
    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        explvList.setIndicatorBounds(width-GetPixelFromDips(35), width-GetPixelFromDips(5));
    } else { 
        explvList.setIndicatorBoundsRelative(width-GetPixelFromDips(35), width-GetPixelFromDips(5));
    }
}
Miran answered 20/3, 2014 at 9:33 Comment(5)
Yes I found this from google. what is this "(myLeft, myRight)"?Swirly
that is a integer value where you want to setMiran
eg: explvList.setIndicatorBounds(width-GetDipsFromPixel(35), width-GetDipsFromPixel(5)); Here width means device width.Miran
why did not accept my edit? then others can't find this GetDipsFromPixel method?Swirly
this made my standard arrows dissapear, Emulator device:Nexus 4 API 22 Android 5.1. MinSdkVersion 16, TargetSdVersion 22.Grievous
N
1

If you want to move indicator to right Just create a dimen in res folder. It will be like that:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="my_value">350dp</dimen>
</resources>

Then in the <ExpandableListView/> add like that:

<ExpandableListView
        ...
        android:indicatorLeft="@dimen/my_value"
        .../>

You can change the dp according to device settings

Nardone answered 13/10, 2020 at 7:8 Comment(0)
P
0
private void setGroupIndicatorToRight() {
    /* Get the screen width */
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        expandableList.setIndicatorBounds(width - getDipsFromPixel(35), width - getDipsFromPixel(5));
    }
    // Convert pixel to dip
    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 + 250.5f);
    }
Pam answered 15/3, 2016 at 9:40 Comment(1)
setGroupIndicatorToRight(); call this method after setting adapter of exp listPam

© 2022 - 2024 — McMap. All rights reserved.