Android Expandable ListView

CLICK HERE TO DOWNLOAD THIS PROJECT

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    >
<ExpandableListView
    android:id="@+id/exp_list"
    android:layout_height="match_parent"
   android:layout_width="match_parent"
  android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
    android:divider="#A4C739"
    android:dividerHeight="0.5dp"
     ></ExpandableListView>
    </RelativeLayout>

parent_layout.xml

<?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="match_parent"
    android:orientation="vertical"
   >
   <TextView
    android:id="@+id/parent_txt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:textColor="#A4C739"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    />
 
    </LinearLayout>

child_layout.xml

<?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="wrap_content"
   android:orientation="vertical"
     >
    <TextView
        android:id="@+id/child_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        />

    </LinearLayout>

MainActivity.java

package com.easyway2in;

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

import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;


public class MainActivity extends Activity {
HashMap<String, List<String>> Movies_category;
List<String> Movies_list;
ExpandableListView Exp_list;
MoviesAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
        Movies_category = DataProvider.getInfo();
        Movies_list = new ArrayList<String>(Movies_category.keySet());
        adapter = new MoviesAdapter(this, Movies_category, Movies_list);
        Exp_list.setAdapter(adapter);
    }
}

MoviesAdapter.java

package com.easyway2in;

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

import android.content.Context;
import android.graphics.Typeface;
import android.util.MonthDisplayHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MoviesAdapter extends BaseExpandableListAdapter{
private Context ctx;
private HashMap<String, List<String>> Movies_Category;
private List<String> Movies_List;
public MoviesAdapter(Context ctx, HashMap<String, List<String>> Movies_Category, List<String> Movies_List )
{
this.ctx = ctx;
this.Movies_Category = Movies_Category;
this.Movies_List = Movies_List;
}

@Override
public Object getChild(int parent, int child) {
return Movies_Category.get(Movies_List.get(parent)).get(child);
}

@Override
public long getChildId(int parent, int child) {
// TODO Auto-generated method stub
return child;
}

@Override
public View getChildView(int parent, int child, boolean lastChild, View convertview,
ViewGroup parentview) 
{
String child_title =  (String) getChild(parent, child);
if(convertview == null)
{
LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   convertview = inflator.inflate(R.layout.child_layout, parentview,false);
}
TextView child_textview = (TextView) convertview.findViewById(R.id.child_txt);
child_textview.setText(child_title);
return convertview;
}

@Override
public int getChildrenCount(int arg0) {
return Movies_Category.get(Movies_List.get(arg0)).size();
}

@Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return Movies_List.get(arg0);
}

@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return Movies_List.size();
}

@Override
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public View getGroupView(int parent, boolean isExpanded, View convertview, ViewGroup parentview) {
// TODO Auto-generated method stub
String group_title = (String) getGroup(parent);
if(convertview == null)
{
LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   convertview = inflator.inflate(R.layout.parent_layout, parentview,false);
}
TextView parent_textview = (TextView) convertview.findViewById(R.id.parent_txt);
parent_textview.setTypeface(null, Typeface.BOLD);
parent_textview.setText(group_title);
return convertview;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}

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

}

DataProvider.java

package com.easyway2in;

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

public class DataProvider {
public static HashMap<String, List<String>> getInfo()
{
HashMap<String, List<String>> MoviesDetails = new HashMap<String, List<String>>();
List<String> Action_Movies = new ArrayList<String>();
Action_Movies.add("300 Rise of an Empire");
Action_Movies.add("Robocop");
Action_Movies.add("The Hunger Games");
Action_Movies.add("The Expendables 3");
Action_Movies.add("Guardian of the Galaxy");
List<String> Romntic_Movies = new ArrayList<String>();
Romntic_Movies.add("Mean Girls");
Romntic_Movies.add("Failure To Launch");
Romntic_Movies.add("The House Bunny");
Romntic_Movies.add("Ghost of Girlfriends Past");
Romntic_Movies.add("The Devil Wears Prada");
List<String> Horror_Movies= new ArrayList<String>();
Horror_Movies.add("The Conjuring");
Horror_Movies.add("Drag Me to Hell");
Horror_Movies.add("Sinister");
Horror_Movies.add("Sleepy Hollow");
Horror_Movies.add("Eden lake");
List<String> Comedy_Movies = new ArrayList<String>();
Comedy_Movies.add("Ride Along");
Comedy_Movies.add("That Awkward Moment");
Comedy_Movies.add("Wish I Was Here");
Comedy_Movies.add("About last Night");
Comedy_Movies.add("This is the End");
MoviesDetails.put("Action Movies", Action_Movies);
MoviesDetails.put("Romantic Movies", Romntic_Movies);
MoviesDetails.put("Horror Movies", Horror_Movies);
MoviesDetails.put("Comedy Movies", Comedy_Movies);
return MoviesDetails;
}

}