0

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.

Madushan Perera
  • 2,568
  • 2
  • 17
  • 36

1 Answers1

1

There are many solutions to this issue.

If you include different toolbars you can add set android:visibility=""

Ex:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="#2196F3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone">
    </android.support.v7.widget.Toolbar>

And set toolbar Visibility when you change fragment:

toolbar.setVisibility(View.VISIBLE);

Or you can edit toolbar in the fragment:

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);

Get toolbar from Main Activity and edit.

Abdelilah El Aissaoui
  • 4,204
  • 2
  • 27
  • 47
Hoang Nguyen
  • 141
  • 1
  • 8
  • Good answer Hoang Nguyen. I made it with your first solution. I didnt know about `toolbar.setVisibility(View.VISIBLE);` . I m just a beginner for Android development . And I was trying with your second solution before. But It always gives me null pointer exception if I did not include toolbars inside main_activity .once I included , I get more than one toolbars in my display. Anyway thanks a lot for the solution. :) – Madushan Perera Feb 27 '16 at 08:28
  • 1
    No problem. With the second solution, it will get the toolbar you have included inside Main Activity and you can change view like this: http://stackoverflow.com/questions/3334048/android-layout-replacing-a-view-with-another-view-on-run-time Sorry, I forgot about it. – Hoang Nguyen Feb 28 '16 at 04:07