Showing posts with label Android code sample: ExpandableListView. Show all posts
Showing posts with label Android code sample: ExpandableListView. Show all posts

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);
 }

}


Monday, September 15, 2014

Create groupIndicator for ExpandableListView example

Modify from last exercise "ExpandableListView example", to create groupIndicator.


Copy drawables to /res/drawable/ folder. In my exercise, btn_check_buttonless_on.png and btn_check_buttonless_off.png copied from Android SDK folder: /Android/sdk/platforms/android-20/data/res/drawable-mdpi/

Create /res/drawable/groupindicator.xml, to define our groupIndicator.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_check_buttonless_off"
       android:state_expanded="true" />
   <item android:drawable="@drawable/btn_check_buttonless_on"/>
</selector>

Modify /res/layout/activity_main.xml to specify android:groupIndicator of ExpandableListView.
<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidexpandablelistview.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <ExpandableListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:groupIndicator="@drawable/groupindicator" />

</LinearLayout>



Next:
- Place groupIndicator of ExpandableListView on right side


Sunday, September 14, 2014

ExpandableListView example

Example of using ExpandableListView:


Create /res/layout/group_layout.xml, to define the layout of list group:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#A0A0A0"
    android:orientation="horizontal" >
 
    <ImageView
        android:id="@+id/groupimg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    <TextView
        android:id="@+id/group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textStyle="bold" />
 
</LinearLayout>

Create /res/layout/item_layout.xml to define layout of items:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dip"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/item"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="italic" />
 
</LinearLayout>

/res/layout/activity_main.xml
<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidexpandablelistview.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <ExpandableListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Create MyExpandableListAdapter.java
package com.example.androidexpandablelistview;

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

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

 private Context context;
 private List<String> listGroup;
 private HashMap<String, List<String>> listChild;

 public MyExpandableListAdapter(Context context, List<String> listGroup,
   HashMap<String, List<String>> listChild) {
  this.context = context;
  this.listGroup = listGroup;
  this.listChild = listChild;
 }

 @Override
 public int getGroupCount() {
  return listGroup.size();
 }

 @Override
 public int getChildrenCount(int groupPosition) {
  return listChild.get(listGroup.get(groupPosition)).size();
 }

 @Override
 public Object getGroup(int groupPosition) {
  return listGroup.get(groupPosition);
 }

 @Override
 public Object getChild(int groupPosition, int childPosition) {
  return listChild.get(listGroup.get(groupPosition)).get(childPosition);
 }

 @Override
 public long getGroupId(int groupPosition) {
  return groupPosition;
 }

 @Override
 public long getChildId(int groupPosition, int childPosition) {
  return childPosition;
 }

 @Override
 public boolean hasStableIds() {
  return false;
 }

 @Override
 public View getGroupView(int groupPosition, boolean isExpanded,
   View convertView, ViewGroup parent) {
  if (convertView == null) {
   LayoutInflater infalInflater = (LayoutInflater) context
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   convertView = infalInflater.inflate(R.layout.group_layout, null);
  }

  String textGroup = (String) getGroup(groupPosition);

  TextView textViewGroup = (TextView) convertView
    .findViewById(R.id.group);
  textViewGroup.setText(textGroup);

  return convertView;
 }

 @Override
 public View getChildView(int groupPosition, int childPosition,
   boolean isLastChild, View convertView, ViewGroup parent) {
  if (convertView == null) {
   LayoutInflater infalInflater = (LayoutInflater) context
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   convertView = infalInflater.inflate(R.layout.item_layout, null);
  }

  TextView textViewItem = (TextView) convertView.findViewById(R.id.item);

  String text = (String) getChild(groupPosition, childPosition);

  textViewItem.setText(text);
  return convertView;
 }

 @Override
 public boolean isChildSelectable(int groupPosition, int childPosition) {
  // TODO Auto-generated method stub
  return false;
 }

}

MainActivity.java
package com.example.androidexpandablelistview;

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

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;

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);
 }

 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);
 }

}

download filesDownload the files.

Next:
- Create groupIndicator for ExpandableListView example
Place groupIndicator of ExpandableListView on right side