n my main activity I have a navigation drawer and by clicking it I m loading different fragments like Home , Cart , Settings etc.. Initially in my main activity I got the toolbar with spinner and button to navigation drawer as well. But for other fragments I need to customize tool bar as I need.
Here is how my activity_main.xml implemented :
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigation_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="@bool/fitsSystemWindows">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_kitkat_height"
android:background="?colorPrimary" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_lollipop_height"
android:background="?colorPrimaryDark" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/status_bar_margin_top">
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?actionBarSize" />
<!--include different toolbars into the activity dont know this is correct implementation... If I missed any of them It gives me a null pointer exception in MainActivity..-->
<include
android:id="@+id/toolbar_cart"
layout="@layout/toolbar_cart" />
<include
android:id="@+id/toolbar_home"
layout="@layout/toolbar_home" />
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="@bool/fitsSystemWindows"
app:headerLayout="@layout/navigation_drawer_header"
app:menu="@menu/navigation_drawer_menu"
app:theme="@style/NavigationViewTheme" />
</android.support.v4.widget.DrawerLayout>
Then I implemented a method to customize toolbar according to the fragment loading (inside main activity) :
public void customizeToolBar(int position) {
switch (position) {
case 0:
toolbar = (Toolbar) findViewById(R.id.toolbar_home);
setSupportActionBar(toolbar);
subCategories_spinner = (Spinner) findViewById(R.id.spinner_subCategories);
actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
break;
case 1:
toolbar = (Toolbar) findViewById(R.id.toolbar_cart);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setTitle("Check Out");
actionBar.setDisplayHomeAsUpEnabled(true);
break;
....
Then I call that method inside the NavigationItemSelectedListener like below :
private void setupNavigationDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.item_navigation_drawer_home:
menuItem.setChecked(true);
customizeToolBar(0);
setFragment(0);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_navigation_drawer_cart:
menuItem.setChecked(true);
customizeToolBar(1);
setFragment(1);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
...
But this is not working. Every time displays the same toolbar. I m new to android. Any suggestion to make this work would be grateful. Thank you.