Reputation: 1669
I want to change the location of the group Indicator of an expandable List view , from left to right, but the group Indicator disappears .
Code From Main Activity:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
mExpandableListView.setIndicatorBounds(width - getDipsFromPixel(35), width
- getDipsFromPixel(5));
} else {
mExpandableListView.setIndicatorBoundsRelative(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 + 0.5f);
}
code from Group Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/tv_menu_background_color"
android:paddingLeft="32dp"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@color/list_item_title"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
Code from activity_main:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/drawer_layout">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- Expandable list view -->
<ExpandableListView
android:id="@+id/list_slideMenu"
android:layout_width="320dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
Thanks!
Upvotes: 0
Views: 275
Reputation: 44118
Seems to me that you're fetching the screen's width and using it to set the indicator's bounds. However, your ExpandableListView
's width is 320dp. I believe you should calculate the width like this:
int width = mExpandableListView.getWidth();
Also all of this should definitely not be called in onWindowFocusChanged
, as it can be called multiple times.
Instead you should put this code into onCreate
(after setContentView
):
mExpandableListView = findViewById(R.id.elv);
mExpandableListView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
mExpandableListView.removeOnLayoutChangeListener(this);
int width = mExpandableListView.getWidth();
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
mExpandableListView.setIndicatorBounds(width - getDipsFromPixel(35), width
- getDipsFromPixel(5));
} else {
mExpandableListView.setIndicatorBoundsRelative(width - getDipsFromPixel(35), width
- getDipsFromPixel(5));
}
}
});
This adds a listener to the ELV, to wait until it's measured, and then set the indicator.
Upvotes: 1