Friday, April 8, 2016

NavigationDrawer Part- I

"Jai Saraswati Maa"

Hi ... Dear All ..Today I am uploading a superb example of NavigationDrawer in which Navigation  View will come from left side cursor drag.
Please check.
I am using Android Studio 1.5.1
Minimun SDK API 21 (Suggested)
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
Special Thanks Monu jangir (Student Partner)****** Please add dependencies in build.gradle file as 
I have added...
compile 'com.android.support:design:23.1.1' 

**** select app theme with NoActionBar

A. Output will be like 

B. XML Files     
 a. activity_main.xml (Read the root element)

<?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:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.exam.navigationdrawer.MainActivity"

    android:id="@+id/drawer_layout"    >


    <LinearLayout        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical"        >
        <include
            android:layout_height="wrap_content"

            android:layout_width="match_parent"

            layout="@layout/toolbar_layout"            />

    </LinearLayout>


    <android.support.design.widget.NavigationView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/navigation_view"

        android:layout_gravity="start"

        app:menu="@menu/draw_menu"        >

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

b. toolbar_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"
    android:id="@+id/toolbar"

    android:background="?attr/colorPrimaryDark"

    android:minHeight="?attr/actionBarSize"

    android:fitsSystemWindows="true"

    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    >

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

C.  Create menu folder in res directory
   and create a draw_menu.xml  file in this menu folder. as

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
         <item             android:id="@+id/home_id"

             android:title="Monu"             >
         </item>
        <item            android:id="@+id/message_id"

            android:title="Jangid"            >

        </item>
        <item            android:id="@+id/setting_id"

            android:title="Setting"            >

        </item>

        <item            android:title="Add to Group"

            android:id="@+id/add_to_group"            >

        </item>

        <item            android:title="Group"

            android:id="@+id/id_group"            >

        </item>

        <item            android:title="Share"

            android:id="@+id/id_share"            >

        </item>

</menu>

D. Java Files MainActivity.java

package com.exam.navigationdrawer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
}





No comments:

Post a Comment