Deepshikha Puri, the young Indian Entrepreneur heading the mobile development trade from years to successive extent, has worked with numerous clients and many tremendous brands in this industry of mobile encompassing in India and overseas maintaining promising work relationships with each of them with an impression to manage it's whole thing.

Sunday, February 11, 2018

Expandablelistview With Searchview In Android

Video Demo:


In this tutorial, I am creating a demo of expandable listview with search bar. Expandable listview is used to show the data according to particular category. You can expand or collapse the data of particular group. To search the data I am using the TextWatcher. DOWNLOAD SOURCE CODE FROM BELOW
android expandablelistview search filter example

activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/rl_header"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="#067b16"
        android:gravity="center"
        android:textColor="#ffffff">
    </TextView>
    <RelativeLayout
        android:id="@+id/rl_search"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_below="@+id/rl_header"
        android:background="#EFEFF4">
        <EditText
            android:id="@+id/et_search"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_centerInParent="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/drawable_search"
            android:drawableLeft="@android:drawable/ic_menu_search"
            android:drawablePadding="10dp"
            android:hint="Search"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" />
    </RelativeLayout>
    <ExpandableListView
        android:id="@+id/ev_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/rl_search">
    </ExpandableListView>
</RelativeLayout>
MainActivity.java:
 package com.deepshikha.expandablelistviewsearch;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ListView;

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

/**
 * Created by deepshikha Puri on 16/1/18.
 */

public class MainActivity extends AppCompatActivity {
    ExpandableListView ev_list;
    List> lv_country = new ArrayList<>();
    CustomAdapter customAdapter;
    EditText et_search;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        init();

    }

    private void init() {
        ev_list = (ExpandableListView) findViewById(R.id.ev_list);
        et_search = (EditText) findViewById(R.id.et_search);

        fn_arraylist();
        customAdapter = new CustomAdapter(lv_country, getApplicationContext());
        ev_list.setAdapter(customAdapter);

        ev_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true; // This way the expander cannot be collapsed
            }
        });

        for (int i = 0; i < customAdapter.getGroupCount(); i++) {
            ev_list.expandGroup(i);
        }


        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                if (et_search.getText().toString().length() == 0) {
                    customAdapter = new CustomAdapter(lv_country, getApplicationContext());
                    ev_list.setAdapter(customAdapter);

                    for (int i = 0; i < customAdapter.getGroupCount(); i++) {
                        ev_list.expandGroup(i);
                    }
                } else {

                    List> lv_search = new ArrayList<>();

                    for (int i = 0; i < lv_country.size(); i++) {
                        List> lv_searchstate = new ArrayList<>();

                        List> lv_state = (List>) lv_country.get(i).get("State");
                        for (int j = 0; j < lv_state.size(); j++) {
                            if (lv_state.get(j).get("Name").toString().toLowerCase().contains(et_search.getText().toString())) {
                                lv_searchstate.add(lv_state.get(j));
                            }
                        }

                        if (lv_searchstate.size() != 0) {
                            HashMap hashMap_search = new HashMap<>();
                            hashMap_search.put("Name", lv_country.get(i).get("Name").toString());
                            hashMap_search.put("State", lv_searchstate);

                            lv_search.add(hashMap_search);
                        }
                    }


                    customAdapter = new CustomAdapter(lv_search, getApplicationContext());
                    ev_list.setAdapter(customAdapter);

                    for (int i = 0; i < customAdapter.getGroupCount(); i++) {
                        ev_list.expandGroup(i);
                    }


                }

            }
        });


    }

    private void fn_arraylist() {
        /*Afghanistan*/
        HashMap hashMap_country = new HashMap<>();
        hashMap_country.put("Name", "Afghanistan");

        List> lv_state = new ArrayList<>();
        HashMap hashMap_state = new HashMap<>();


        hashMap_state.put("Name", "Kabul");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Kandahar");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Herat");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Mazar-i-Sharif");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Kunduz");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Jalalabad");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Lashkar Gah");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Taluqan");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Puli Khumri");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Khost");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Ghazni");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Sheberghan");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Sari Pul ");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_country.put("State", lv_state);

        lv_country.add(hashMap_country);

        /*India*/
        hashMap_country = new HashMap<>();
        hashMap_country.put("Name", "India");

        lv_state = new ArrayList<>();
        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Arunachal Pradesh");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Assam");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Bihar");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Chandigarh");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Chhattisgarh");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Goa");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Gujarat");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Haryana");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Himachal Pradesh");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Jammu and Kashmir");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Jharkhand");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Maharashtra");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Odisha");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Punjab");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Rajasthan");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Sikkim");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Uttarakhand");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "West Bengal");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();

        hashMap_country.put("State", lv_state);
        lv_country.add(hashMap_country);

        /*America*/
        hashMap_country = new HashMap<>();
        hashMap_country.put("Name", "America");

        lv_state = new ArrayList<>();
        hashMap_state = new HashMap<>();

        hashMap_state.put("Name", "California");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Texas");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Hawaii");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Florida");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Alabama");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Alaska");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Pennsylvania");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "New Jersey");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Minnesota");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Georgia");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Colorado");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "North Carolina");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "Indiana");
        lv_state.add(hashMap_state);

        hashMap_state = new HashMap<>();
        hashMap_state.put("Name", "New Mexico");
        lv_state.add(hashMap_state);

        hashMap_country.put("State", lv_state);
        lv_country.add(hashMap_country);

    }

}

adapter_child.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_child"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="Child"
        android:textColor="#000000"
        android:textSize="15dp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="#000000">

    </View>

</RelativeLayout>
 adapter_header.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="50dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_headername"
        android:layout_width="match_parent"
        android:textSize="15dp"
        android:layout_height="match_parent"
        android:background="#067b16"
        android:gravity="center_vertical"
        android:padding="10dp"
        android:text="Header"
        android:textColor="#FFFFFF" />

</LinearLayout>

CustomAdapter.java:

package com.deepshikha.expandablelistviewsearch;

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

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

/**
 * Created by deepshikha Puri on 16/1/18.
 */

public class CustomAdapter extends BaseExpandableListAdapter {
    List> lv_data;
    Context context;

    public CustomAdapter(List> lv_data, Context context) {
        this.lv_data = lv_data;
        this.context = context;
    }

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

    @Override
    public int getChildrenCount(int i) {

        List> lv_state = (List>) lv_data.get(i).get("State");
        return lv_state.size();
    }

    @Override
    public Object getGroup(int i) {
        return lv_data.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {


        List> lv_state = (List>) lv_data.get(i).get("State");
        return lv_state.get(i1);
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

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

    @Override
    public View getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup) {

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.adapter_header, null);
        }

        TextView tv_header = (TextView) convertView.findViewById(R.id.tv_headername);
        tv_header.setText(lv_data.get(i).get("Name").toString());
        return convertView;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View convertView, ViewGroup viewGroup) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.adapter_child, null);
        }

        TextView tv_chid = (TextView) convertView.findViewById(R.id.tv_child);

        List> lv_state = (List>) lv_data.get(i).get("State");
        tv_chid.setText(lv_state.get(i1).get("Name").toString());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}

DOWNLOAD SOURCE CODE FROM HERE

3 comments: