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 ?
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);
}
Actually there is a simple way.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With