Saturday, September 16, 2017

android - Navigation drawer example

activity_main.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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ecebff"
        >
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#66ff44"
                >
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.AppBarLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#ecebff"
            android:padding="16dp"
            >
            <TextView
                android:id="@+id/text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Main Content"
                android:layout_gravity="center"
                android:textSize="30sp"
                />
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:background="#f4fff3"
        >
        <TextView
            android:id="@+id/drawer_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Sample Drawer Layout"
            android:background="#ffcc00"
            android:textSize="20sp"
            android:padding="16dp"
            />
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/navigation_view_menu"
            />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private Menu mNavigationMenu;
    private TextView mTextView;
    private ActionBarDrawerToggle mDrawerToggle;

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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from xml layout
        mDrawerLayout = findViewById(R.id.drawer_layout);
        mNavigationView = findViewById(R.id.navigation_view);
        mTextView = findViewById(R.id.text_view);
        mToolbar = findViewById(R.id.toolbar);

        // Get the menu from navigation view
        mNavigationMenu = mNavigationView.getMenu();

        // Set a title for toolbar
        mToolbar.setTitle("Sample toolbar");

        // Set the support action bar
        setSupportActionBar(mToolbar);

        // Initialize a drawer toggle instance
        mDrawerToggle = new ActionBarDrawerToggle(
                mActivity,
                mDrawerLayout,
                mToolbar,
                R.string.drawer_open,
                R.string.drawer_close
        ){
            public void onDrawerClosed(View view){
                super.onDrawerClosed(view);
                Toast.makeText(mContext,"Drawer close",Toast.LENGTH_SHORT).show();
            }

            public void onDrawerOpened(View drawerView){
                super.onDrawerOpened(drawerView);
                Toast.makeText(mContext,"Drawer open",Toast.LENGTH_SHORT).show();
            }
        };
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

        // Menu first item click listener
        MenuItem addNow = mNavigationMenu.findItem(R.id.add_now);
        addNow.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();
                mTextView.setText("Add now clicked.");
                return false;
            }
        });

        // Menu second item click listener
        MenuItem setAlarm = mNavigationMenu.findItem(R.id.set_alarm);
        setAlarm.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();
                mTextView.setText("Set alarm clicked.");
                return false;
            }
        });

        // Menu third item click listener
        MenuItem share = mNavigationMenu.findItem(R.id.share);
        share.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();
                mTextView.setText("Share clicked.");
                return false;
            }
        });
    }
}
res/values/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- Important for toolbar  -->
        <item name="windowNoTitle">true</item>
    </style>

</resources>
res/menu/navigation_view_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item
        android:id="@+id/add_now"
        android:title="Add now"
        android:icon="@drawable/ic_add_circle_black_24dp"
        />
    <item
        android:id="@+id/set_alarm"
        android:title="Set alarm"
        android:icon="@drawable/ic_access_alarm_black_24dp"
        />
    <item
        android:id="@+id/share"
        android:title="Share"
        android:icon="@drawable/ic_share_black_24dp"
        />
</menu>
res/values/strings.xml

<resources>
    <string name="app_name">DrawerLayout example</string>

    <string name="drawer_open">Open</string>
    <string name="drawer_close">Close</string>
</resources>
app build.gradle [dependencies]

compile 'com.android.support:design:26.0.2'