Custom Navigation drawer in android

abarmain.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="nithra.mydrawertwo.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
<!--
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"

         />-->

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/nav_view"
        android:layout_gravity="start"

        >

        <LinearLayout
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:orientation="horizontal"
            android:background="#ea0fcd"
            android:id="@+id/lin_security"
            android:layout_gravity="start"

            >
            <ImageView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:src="@drawable/key"
                android:layout_gravity="center"
                />
            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Security"
                android:layout_marginLeft="5dp"
                android:layout_gravity="center"
                android:textColor="#ffffff"
                android:textStyle="bold"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:orientation="horizontal"
            android:background="#a306c4"
            android:id="@+id/lin_not"
            >
            <ImageView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/notification"
                android:layout_gravity="center"
                />
            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Notification"
                android:layout_marginLeft="5dp"
                android:layout_gravity="center"
                android:textColor="#ffffff"
                android:textStyle="bold"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:orientation="horizontal"
            android:background="#6707ed"
            android:id="@+id/lin_alapp"
            >
            <ImageView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/all_app"
                android:layout_gravity="center"
                />
            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="All Apps"
                android:layout_marginLeft="5dp"
                android:layout_gravity="center"
                android:textColor="#ffffff"
                android:textStyle="bold"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:orientation="horizontal"
            android:background="#2280ec"
            android:id="@+id/lin_backup"
            >
            <ImageView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/backp"
                android:layout_gravity="center"
                />
            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Back Up"
                android:layout_marginLeft="5dp"
                android:layout_gravity="center"
                android:textColor="#ffffff"
                android:textStyle="bold"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:orientation="horizontal"
            android:background="#07bfed"
            android:id="@+id/lin_exit"
            >
            <ImageView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/exit"
                android:layout_gravity="center"
                />
            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Exit"
                android:layout_marginLeft="5dp"
                android:layout_gravity="center"
                android:textColor="#ffffff"
                android:textStyle="bold"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:orientation="horizontal"
            android:background="#0fe4d2"
            android:id="@+id/lin_fdback"
            >
            <ImageView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/feedback"
                android:layout_gravity="center"
                />
            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Feedback"
                android:layout_marginLeft="5dp"
                android:layout_gravity="center"
                android:textColor="#ffffff"
                android:textStyle="bold"/>
        </LinearLayout>

    </LinearLayout>


</android.support.v4.widget.DrawerLayout>

MainActivity.java

package nithra.mydrawertwo;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;

import yalantis.com.sidemenu.interfaces.Resourceble;
import yalantis.com.sidemenu.util.ViewAnimator;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
    LinearLayout lin_security, lin_not, lin_alapp, lin_backup, lin_exit, lin_fdback;
    DrawerLayout drawer;
    LinearLayout navigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

      drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();


        navigationView = (LinearLayout) findViewById(R.id.nav_view);
        lin_security = (LinearLayout) findViewById(R.id.lin_security);
        lin_not = (LinearLayout) findViewById(R.id.lin_not);
        lin_alapp = (LinearLayout) findViewById(R.id.lin_alapp);
        lin_backup = (LinearLayout) findViewById(R.id.lin_backup);
        lin_exit = (LinearLayout) findViewById(R.id.lin_exit);
        lin_fdback = (LinearLayout) findViewById(R.id.lin_fdback);


        lin_alapp.setOnClickListener(this);
        lin_backup.setOnClickListener(this);
        lin_exit.setOnClickListener(this);
        lin_not.setOnClickListener(this);
        lin_fdback.setOnClickListener(this);
        lin_security.setOnClickListener(this);





        /*navigationView.setNavigationItemSelectedListener(this);*/
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onClick(View v) {

        int id = v.getId();
        switch (id) {
            case R.id.lin_security: {

                Intent i=new Intent(MainActivity.this,Loginact.class);
                startActivity(i);
                drawer.closeDrawer(navigationView);
            }
            break;
            case R.id.lin_alapp: {
                Toast.makeText(MainActivity.this, "all apps", Toast.LENGTH_SHORT).show();
                drawer.closeDrawer(navigationView);
            }
            break;
            case R.id.lin_backup: {
                Toast.makeText(MainActivity.this, "back up", Toast.LENGTH_SHORT).show();
                drawer.closeDrawer(navigationView);
            }
            break;
            case R.id.lin_exit: {
                Toast.makeText(MainActivity.this, "exit", Toast.LENGTH_SHORT).show();
                drawer.closeDrawer(navigationView);
            }
            break;
            case R.id.lin_fdback: {
                Toast.makeText(MainActivity.this, "feedback", Toast.LENGTH_SHORT).show();
                drawer.closeDrawer(navigationView);

            }
            break;
            case R.id.lin_not: {
                Toast.makeText(MainActivity.this, "notification", Toast.LENGTH_SHORT).show();
                drawer.closeDrawer(navigationView);
            }
            break;

        }
    }
}

Leave a comment