Skip to main content

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

}






Comments

  1. Thanks a lot, sir i tried it on Android Studio but i get some errors on (exp_list) appeared on red color

    Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
    Movies_category = DataProvider.getInfo();
    Movies_list = new ArrayList(Movies_category.keySet());
    adapter = new MoviesAdapter(this, Movies_category, Movies_list);
    Exp_list.setAdapter(adapter);

    will you plz help me out

    ReplyDelete
  2. Hi i'm have an issue with the following (R.layout.child_layout) child_layout is highlighted red:
    LayoutInflater Inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = Inflater.inflate(R.layout.child_layout, parentView,false);

    ReplyDelete

Post a Comment

Popular posts from this blog

Android MySQL Database Operations

In this post we are going to learn about how to connect to MySQL Database from your Android Application and perform database operations. Here we create an android app that contain some login Activity through which the user can retrieve information from database and a registration Activity through which the user can add information into the database.  First you need to have the following components installed in your development machine.  1. Database : Here we use the MySQL database. 2. Web Server : Here we use the Apache Web Server. 3. Server side Scripting Language :   Here we use PHP for server side scripting. 4. Android Development environment : You must install android sdk and android studio.   I recommend you to download and install WAMPSERVER. The wamp server installer contains the following components. Apache Server Application MySQL Database PHP/phpMyAdmin First we have to create the database and table in MySQL. You can use the...

Schedule Android Background Job Using Firebase JobDispatcher

From Android version 8.0 (Oreo) on wards there are limitations in executing background services and there are some limitations in receiving certain broadcasts. The android team implement all these restrictions to improve app performance and device battery life. If your app target API 26 then you have to consider some other mechanisms to do your background job.  Fortunately there is JobScheduler API that solve the problem regarding the background job limitations in newer versions of android. Google suggests Android App developers to use the JobScheduler API for their background job execution rather than using a background service.  But the problem is that the android framework JobScheduler API is available from android API version 21(Lollipop) and above. If your app support start from Lollipop and above then you can go for the framework JobScheduler API for your background job. If your App support start from android API lower than 21 then you can use the Firebase ...

"please correct the errors on this form" adsense error simple solution

Many of the bloggers now facing the problem with their adsense widgets. Whenever try to add the new  adsense link unit using the blogger widgets, it shows the error " please correct the errors on this form " as shown bellow. In blogger you can add the adsense units in two ways. First one is adding the adsense using the blogger widgets(Most of the beginners doing this) and the second method is to obtain the adsense code from the adsense login page and place it into the targeted position on the blog. To improve the revenue from adsense you have to place the appropriate ad unit into the right position. The adsense link units are very important for increasing the adsense revenue. So due to this error many of the bloggers are unable to place the link units.  Here is the solution for the problem. 1. Login into your adsense and blogger account. 2. Now make sure that only two adsense widgets (units) are present on your blog, this is because Google allows onl...