TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Navigation Drawer in Android Studio
Harshad Pansuriya
Sep 10
2015
Code
2.4
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
package
com.abc.navigationdrawer;
import
android.content.Intent;
import
android.os.Bundle;
import
android.support.design.widget.NavigationView;
import
android.support.v4.view.GravityCompat;
import
android.support.v4.widget.DrawerLayout;
import
android.support.v7.app.ActionBar;
import
android.support.v7.app.AppCompatActivity;
import
android.support.v7.widget.Toolbar;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
MainActivity
extends
AppCompatActivity {
DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBar actionBar;
TextView textView;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayHomeAsUpEnabled(
true
);
drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
if
(navigationView !=
null
) {
setupNavigationDrawerContent(navigationView);
}
setupNavigationDrawerContent(navigationView);
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return
true
;
}
@Override
public
boolean
onOptionsItemSelected(MenuItem item) {
switch
(item.getItemId()) {
case
android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return
true
;
}
return
super
.onOptionsItemSelected(item);
}
private
void
setupNavigationDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new
NavigationView.OnNavigationItemSelectedListener() {
@Override
public
boolean
onNavigationItemSelected(MenuItem menuItem) {
textView = (TextView) findViewById(R.id.textView);
switch
(menuItem.getItemId()) {
case
R.id.item_navigation_drawer_inbox:
menuItem.setChecked(
true
);
textView.setText(menuItem.getTitle());
drawerLayout.closeDrawer(GravityCompat.START);
return
true
;
case
R.id.item_navigation_drawer_starred:
menuItem.setChecked(
true
);
textView.setText(menuItem.getTitle());
drawerLayout.closeDrawer(GravityCompat.START);
return
true
;
case
R.id.item_navigation_drawer_sent_mail:
menuItem.setChecked(
true
);
textView.setText(menuItem.getTitle());
drawerLayout.closeDrawer(GravityCompat.START);
return
true
;
case
R.id.item_navigation_drawer_drafts:
menuItem.setChecked(
true
);
textView.setText(menuItem.getTitle());
drawerLayout.closeDrawer(GravityCompat.START);
return
true
;
case
R.id.item_navigation_drawer_settings:
menuItem.setChecked(
true
);
textView.setText(menuItem.getTitle());
Toast.makeText(MainActivity.
this
,
"Launching "
+ menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
Intent intent =
new
Intent(MainActivity.
this
, SettingsActivity.
class
);
startActivity(intent);
return
true
;
case
R.id.item_navigation_drawer_help_and_feedback:
menuItem.setChecked(
true
);
Toast.makeText(MainActivity.
this
, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return
true
;
}
return
true
;
}
});
}
}
MainActivity.Java
package
com.abc.navigationdrawer;
import
android.os.Build;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.support.v7.widget.Toolbar;
import
android.util.TypedValue;
import
android.view.Menu;
import
android.view.MenuItem;
public
class
SettingsActivity
extends
AppCompatActivity {
Toolbar toolbar;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(
true
);
TypedValue typedValueColorPrimaryDark =
new
TypedValue();
SettingsActivity.
this
.getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValueColorPrimaryDark,
true
);
final
int
colorPrimaryDark = typedValueColorPrimaryDark.data;
if
(Build.VERSION.SDK_INT >=
21
) {
getWindow().setStatusBarColor(colorPrimaryDark);
}
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_settings, 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);
}
}
android