IntelliJ has made changes to the Navigation Drawer template Activity in Android Studio with fewer lines of code in the Activity class. The new Activity class looks like this
I used a template auto-created by Android Studio 1.4 too, and I had encounter same difficulties as you.
First, I changed activity_main.xml
A DrawerLayout that contains two parts:
LinearLayout)NavigationView)Main Screen contains the ActionBar and the FrameLayout (R.id.content) will be replaced by a fragment.
The Drawer contains the header and the menu.
So, we now override onNavigationItemSelected and replace R.id.content with a fragment.
Here is my MainActivity code:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment;
FragmentTransaction ft = getFragmentManager().beginTransaction();
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_A) {
fragment = new AFragment();
ft.replace(R.id.content, fragment).commit();
} else if (id == R.id.nav_B){
fragment = new BFragment();
ft.replace(R.id.content, fragment).commit();
} else if (id == R.id.nav_settings) {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Main page is also a fragment.
And I set up main page in onCreate method although I am not sure if there is a better way to set up main page.
//MainPageFragment
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, new MainPageFragment()).commit();
PS. here is the toolbar.xml, the ActionBar