In this tutorial, we will learn how to implement same Navigation Drawer for different activities. Navigation Drawer is an important widget in android application. We can use fragments in navigation view to change screens based on the menu selection. It may lead to back stack issue. Instead of using fragments we can use this approach and is very easy to implement.
Coding part
Create new project, select Navigation drawer activity and click "finish".Rename MainActivity into BaseActivity and paste the following code.
public class BaseActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
FloatingActionButton fab;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = (FloatingActionButton) findViewById(R.id.fab);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_activity1) {
startAnimatedActivity(new Intent(getApplicationContext(), FirstActivity.class));
} else if (id == R.id.nav_activity2) {
startAnimatedActivity(new Intent(getApplicationContext(), SecondActivity.class));
}
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Open content_main.xml and paste the following code.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.androidmads.navdraweractivity.BaseActivity"
tools:showIn="@layout/app_bar_main" />
here, the FrameLayout has the id as "content_frame" is used to hold the activity's view.Create New Activity and named as FirstActivity.java and change the parent of this class from AppCompatActivity to BaseActivity as we created. Replace setContentView in onCreate method as shown below
setContentView(R.layout.activity_first)
to
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflate your activity layout here!
@SuppressLint("InflateParams")
View contentView = inflater.inflate(R.layout.activity_first, null, false);
drawer.addView(contentView, 0);
Here,
- activity_first is the designer layout of the activity FirstActivity.java
public class FirstActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflate your activity layout here!
@SuppressLint("InflateParams")
View contentView = inflater.inflate(R.layout.activity_first, null, false);
drawer.addView(contentView, 0);
navigationView.setCheckedItem(R.id.nav_activity1);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Hello First Activity", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@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_first, 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_menu_first) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
finishAffinity();
} else {
super.onBackPressed();
}
}
}
}
here, the parent of FirstActivity is BaseActivity. In this way you can use same NavigationView for all activities.
Note:Don't forget to change your launcher activity into FirstActivity.
Download Code
You can download the full source code for this tutorial from the following Github link. If you Like this tutorial, Please star it in Github.
If you like this tutorial, like & share our facebook page.
Follow Us
Were this world an endless plain, and by sailing eastward we could for ever reach new distances