Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Saturday, 14 May 2016

SubMenuNavigationDrawer Sample

Hello guys if you want create a submenu navigation drawer then follow this blog

actvity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txttitle"
            android:layout_width="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#fff"
            android:layout_marginLeft="5dp"
            android:text="DrawerSample"/>

        </LinearLayout>
    </android.support.v7.widget.Toolbar>


ActivityMain.java


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;


import com.samset.expandablelistviewnavigationdrawer.adapter.ExpandableListDrawerAdapter;
import com.samset.expandablelistviewnavigationdrawer.fragments.HomeFragment;

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

public class MainActivity extends ActionBarActivity {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    public ExpandableListView mExpandableListView;
    private ExpandableListDrawerAdapter mExpandableListAdapter;
    private Map<String, List<String>> mExpandableListData;
    private String selectedItem;
    private LinearLayout linearLayout;
    private Toolbar toolbar;

    private HashMap<String, List<String>> listDataChild;
    private List<String> listDataHeader;

    View listHeaderView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.tool);


        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mExpandableListView = (ExpandableListView) findViewById(R.id.navList);
        //

       
LayoutInflater inflater = getLayoutInflater();
        listHeaderView = inflater.inflate(R.layout.nav_header, null, false);
        linearLayout = (LinearLayout) listHeaderView.findViewById(R.id.upper_linear_layout);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                beginTransction(new HomeFragment());
                mDrawerLayout.closeDrawer(GravityCompat.START);
                toolbar.setTitle("MyDrawerSample");
            }
        });
        setToolbar();

        prepareListData();
        addDrawerItems();
        setupDrawer();


        beginTransction(new HomeFragment());


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);  // OPEN DRAWER
               
return true;

        }
        return super.onOptionsItemSelected(item);
    }
    // setting toolbar
   
private void setToolbar() {
        if (toolbar != null) {
            setSupportActionBar(toolbar);

            final ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayHomeAsUpEnabled(true);
                actionBar.setDisplayShowHomeEnabled(true);
                actionBar.setDisplayShowTitleEnabled(true);
                actionBar.setDisplayUseLogoEnabled(false);
                actionBar.setHomeButtonEnabled(true);
            }
        }
    }
    private void prepareListData() {

        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        listDataHeader = Arrays.asList(getResources().getStringArray(R.array.nav_drawer_items));

        List<String> home = Arrays.asList(getResources().getStringArray(R.array.home));

        List<String> menlist = Arrays.asList(getResources().getStringArray(R.array.men));

        List<String> womenlist =Arrays.asList(getResources().getStringArray(R.array.women));

        List<String> childlist = Arrays.asList(getResources().getStringArray(R.array.child));

        List<String> electronicslist = Arrays.asList(getResources().getStringArray(R.array.electronics));

        List<String> homedecorelist = Arrays.asList(getResources().getStringArray(R.array.homedecore));

        List<String> computerlist = Arrays.asList(getResources().getStringArray(R.array.computeraccess));

        List<String> personallist = Arrays.asList(getResources().getStringArray(R.array.personal));

        List<String> gaminglist = Arrays.asList(getResources().getStringArray(R.array.gaming));

        List<String> healthlist = Arrays.asList(getResources().getStringArray(R.array.health));



        listDataChild.put(listDataHeader.get(0), home);
        listDataChild.put(listDataHeader.get(1), menlist);
        listDataChild.put(listDataHeader.get(2), womenlist);
        listDataChild.put(listDataHeader.get(3), childlist);
        listDataChild.put(listDataHeader.get(4), electronicslist);
        listDataChild.put(listDataHeader.get(5), homedecorelist);
        listDataChild.put(listDataHeader.get(6), computerlist);
        listDataChild.put(listDataHeader.get(7), personallist);
        listDataChild.put(listDataHeader.get(8), gaminglist);
        listDataChild.put(listDataHeader.get(9), healthlist);


        mExpandableListView.addHeaderView(listHeaderView);
        mExpandableListData = listDataChild;
        // mExpandableListTitle = new ArrayList(listDataChild.keySet());
       
Log.v("Main", " Key set " + listDataChild.keySet());
    }



    private void addDrawerItems() {
        mExpandableListAdapter = new ExpandableListDrawerAdapter(this, listDataHeader, mExpandableListData);
        mExpandableListView.setAdapter(mExpandableListAdapter);

        mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                Log.e("Main"," you click parent ");
                return false;
            }
        });
        mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                Log.e("Main"," you click child ");
                selectedItem = ((List) (mExpandableListData.get(listDataHeader.get(groupPosition)))).get(childPosition).toString();
                toolbar.setTitle(selectedItem);

                Log.e("Main"," you click child ");
                if (selectedItem.equalsIgnoreCase("Shirt")) {
                    beginTransction(new HomeFragment());
                }
                else if (selectedItem.equalsIgnoreCase("Paint")) {
                    beginTransction(new HomeFragment());
                }
                else if (selectedItem.equalsIgnoreCase("Watch")) {
                    beginTransction(new HomeFragment());
                }

                mDrawerLayout.closeDrawer(GravityCompat.START);
                return false;
            }
        });
        mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {

                toolbar.setTitle(listDataHeader.get(groupPosition).toString());
            }
        });

        mExpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {


            }
        });


    }

    private void setupDrawer() {
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

            /** Called when a drawer has settled in a completely open state. */
           
public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                toolbar.setTitle(R.string.app_name);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
           
}

            /** Called when a drawer has settled in a completely closed state. */
           
public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                if (selectedItem != null) {
                    toolbar.setTitle(selectedItem);
                } else {
                    toolbar.setTitle("MyShopingCart");
                }
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
           
}
        };


        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
       
mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }


    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
       
// super.onBackPressed();
       
new AlertDialog.Builder(this)
                .setMessage("Are you sure you want to exit?")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent intent = new Intent(Intent.ACTION_MAIN);
                                intent.addCategory(Intent.CATEGORY_HOME);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(intent);
                                System.exit(0);
                            }
                        }).setNegativeButton("No", null).show();

    }
    private void beginTransction(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frame_container, fragment);
        transaction.addToBackStack(null);
        transaction.commit();

    }
}


HomeFragment.java



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.samset.expandablelistviewnavigationdrawer.R;

/**
 * A simple {
@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {


    public HomeFragment() {
        // Required empty public constructor
   
}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       
return inflater.inflate(R.layout.fragment_home, container, false);
    }

}

HomeFragment.java


<RelativeLayout 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">

    <!-- TODO: Update blank fragment layout -->
   
<TextView
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="HomeFragment" />

</RelativeLayout>


nav_drawer_item_group.xml



<?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="@color/colorPrimaryDark"
    android:orientation="vertical"
    android:padding="12dp" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/ic_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:contentDescription="@null" />

        <TextView
            android:id="@+id/tv_groupitem"
            android:layout_toRightOf="@+id/ic_txt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
            android:textColor="#fff" />

        <TextView
            android:id="@+id/plus_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingRight="5dp"
            android:textColor="#fff"
            android:textSize="20sp"
            android:textStyle="bold" />
    </RelativeLayout>

</LinearLayout>

nav_drawer_item_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="wrap_content"
    >
<RelativeLayout
    android:id="@+id/parent_lay"
    android:paddingLeft="30dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_childitem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:textColor="#fff"
        android:paddingBottom="5dp"
        android:textStyle="italic"
        android:focusable="false"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
<!--android:listSelector="@drawable/list_selector"--></RelativeLayout>

</RelativeLayout>

nav_header.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/navigation_header"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/header"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/layout_margin_default"
        android:gravity="center"
        android:id="@+id/upper_linear_layout"
        android:orientation="horizontal"
        android:weightSum="1">

        <ImageView
            android:id="@+id/filmIcon"
            android:layout_width="@dimen/header_img_size"
            android:layout_height="@dimen/header_img_size"
            android:src="@drawable/ic_avtar"/>
    </LinearLayout>
</LinearLayout>



Thank you

Full Source Code.SubMenuNavigationDrawer


Live Sample