Android Program to Demonstrate Adding Actions to Navigation Bar

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

Main Activity

package com.example.nav_bar;
 
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
    private String[] titles;
    private DrawerLayout drawer_layout;
    private ListView drawer_content;
    private CharSequence main_title;
    private ActionBarDrawerToggle action_drawer_toggle;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        main_title = "test";
 
        titles = new String[] { "one", "two", "three" };
        drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer_content = (ListView) findViewById(R.id.left_drawer);
 
        drawer_content.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, titles));
 
        drawer_content.setOnItemClickListener(new DrawerItemClickListener());
 
        action_drawer_toggle = new ActionBarDrawerToggle(this, /* Main Activity */
        drawer_layout, /* DrawerLayout object */
        R.drawable.ic_drawer, /* the hamburger */
        R.string.open, /* open description */
        R.string.close /* close description */
        ) {
            /* on close drawer */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(main_title);
            }
 
            /* drawer is in a open state. */
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(main_title);
            }
        };
 
        drawer_layout.setDrawerListener(action_drawer_toggle);
 
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        action_drawer_toggle.syncState();
    }
 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        action_drawer_toggle.onConfigurationChanged(newConfig);
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (action_drawer_toggle.onOptionsItemSelected(item)) {
            return true;
        }
 
        return super.onOptionsItemSelected(item);
    }
 
    private void selectItem(int position) {
        Toast.makeText(this, R.string.app_name, Toast.LENGTH_SHORT).show();
        drawer_content.setItemChecked(position, true);
        setTitle(titles[position]);
        drawer_layout.closeDrawer(drawer_content);
    }
 
    @Override
    public void setTitle(CharSequence title) {
        main_title = title;
        getActionBar().setTitle(main_title);
    }
 
    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position,
                long id) {
            selectItem(position);
        }
    }
 
}

string

<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <string name="app_name">Nav_Bar</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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
 
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- navigation drawer layout -->
 
    <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_layout

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

Screenshot_2013-11-16-19-17-21

Screenshot_2013-11-16-19-17-36

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

advertisement
If you wish to look at all Tutorials, go to Java Android Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.