110 Java Android Program to Demonstrate Navigation Drawer in Android
Posted by Superadmin on February 28 2017 12:48:23

Java Android Program to Demonstrate Navigation Drawer in Android

Here is source code of the Program to Demonstrate Navigation Drawer in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

 

The navigation drawer is a panel that transitions in from the left edge of the screen and displays the app’s main navigation options.This is the panel that u see in the gmail app also.Now the below given code demonstrates that for more info go to –
http://developer.android.com/design/patterns/navigation-drawer.html

Main Activity

package com.example.navigation_drawer;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
 
@SuppressLint("NewApi")
public class MainActivity extends Activity {
    private DrawerLayout d_layout;
    private ActionBarDrawerToggle action_bar_toggle;
    String title = "navigation-bar";
    String d_title = "navigation-bar";
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        d_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
        action_bar_toggle = new ActionBarDrawerToggle(this, /* host Activity */
        d_layout, /* DrawerLayout object */
        R.drawable.ic_drawer, R.string.open, R.string.close) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(title);
            }
 
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(d_title);
            }
        };
 
        d_layout.setDrawerListener(action_bar_toggle);
 
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }
 
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        action_bar_toggle.syncState();
    }
 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        action_bar_toggle.onConfigurationChanged(newConfig);
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (action_bar_toggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
 
}
 
class listener implements ListView.OnItemClickListener {
 
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
 
    }
 
}

string

<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <string name="app_name">Navigation_Drawer</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="open">Open</string>
    <string name="close">Close</string>
 
</resources>

Activity_Main

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="188dp"
        android:text="Navigation Drawer Demonstration"
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
</RelativeLayout>

drawer_layout

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- navigation drawer -->
 
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
 
</android.support.v4.widget.DrawerLayout>

drawer_list

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="New Text"
          android:id="@+id/textView"/>