Tuesday, September 16, 2014

Place groupIndicator of ExpandableListView on right side

To place groupIndicator of ExpandableListView on right side, we can call setIndicatorBounds() for Android devices before Jelly Bean, or call setIndicatorBoundsRelative() for devices of Jelly Bean or higher.

Run on Nexus 7 of Android 4.4.4:



On Nexus One running Android 2.3.6



Modify MainActivity.java of last example "Create groupIndicator for ExpandableListView example", override onWindowFocusChanged() method.

package com.example.androidexpandablelistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.annotation.TargetApi;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

 ExpandableListView expandableListView;
 MyExpandableListAdapter myExpandableListAdapter;
 List<String> groupList;
 HashMap<String, List<String>> childMap;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  init();
  expandableListView = (ExpandableListView) findViewById(R.id.mylist);
  myExpandableListAdapter = new MyExpandableListAdapter(this, groupList, childMap);
  expandableListView.setAdapter(myExpandableListAdapter);

 }

 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
 @Override
 public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);
  
  Drawable drawable_groupIndicator = 
   getResources().getDrawable(R.drawable.groupindicator);
  int drawable_width = drawable_groupIndicator.getMinimumWidth();
  
  if(android.os.Build.VERSION.SDK_INT < 
   android.os.Build.VERSION_CODES.JELLY_BEAN_MR2){
   expandableListView.setIndicatorBounds(
    expandableListView.getWidth()-drawable_width, 
    expandableListView.getWidth());
  }else{
   expandableListView.setIndicatorBoundsRelative(
    expandableListView.getWidth()-drawable_width, 
    expandableListView.getWidth());
  }
  
  Toast.makeText(MainActivity.this, 
   "drawable_width: " + drawable_width +"\n" +
   "expandableListView.getWidth()" + expandableListView.getWidth(), 
   Toast.LENGTH_LONG).show();
 }

 private void init() {
  groupList = new ArrayList<String>();
  childMap = new HashMap<String, List<String>>();

  List<String> groupList0 = new ArrayList<String>();
  groupList0.add("groupList0 - 1");
  groupList0.add("groupList0 - 2");
  groupList0.add("groupList0 - 3");
  
  List<String> groupList1 = new ArrayList<String>();
  groupList1.add("groupList1 - 1");
  groupList1.add("groupList1 - 2");
  groupList1.add("groupList1 - 3");

  List<String> groupList2 = new ArrayList<String>();
  groupList2.add("groupList2 - 1");
  groupList2.add("groupList2 - 2");
  groupList2.add("groupList2 - 3");

  List<String> groupList3 = new ArrayList<String>();
  groupList3.add("groupList3 - 1");
  groupList3.add("groupList3 - 2");
  groupList3.add("groupList3 - 3");

  groupList.add("Group List 0");
  groupList.add("Group List 1");
  groupList.add("Group List 2");
  groupList.add("Group List 3");

  childMap.put(groupList.get(0), groupList0);
  childMap.put(groupList.get(1), groupList1);
  childMap.put(groupList.get(2), groupList2);
  childMap.put(groupList.get(3), groupList3);
 }

}


1 comment:

Unknown said...

Thank you for your example. But I'm just wondering why don't you use XML layout and specify appropriate value dimensions (large, normal, etc.)?

BR
Dima