Can I have toolbar for each fragment separately. How to handle navigation drawer
Asked Answered
A

1

2

In my app some pages have custom view in toolbar. Some fragment have transparent toolbar and some have coordinate layout scroll.

So I have decided to have toolbar separate for each fragment I want to know is it a good practice or not.

If someone has already done this please share code or example.

Ahmedahmedabad answered 4/12, 2017 at 7:17 Comment(7)
If navigation drawer is single then why you want to have toolbar in each fragment ? ExplainShamble
no some internal detail pages/fragments are on non-drawer activityAhmedahmedabad
I think You can just follow the traditional way Single ToolBar multiple fragment and you can access toolbar from each fragment to manipulate it. For non-drawer activity use a new Activity. This is just a suggestion, wait for other answer.Shamble
Yes you can use separate toolbars for each fragment. It's not a bad practice. You can implement it if that's your requirement.Weld
@apk you want to implement separate toolbar in each fragment or get the activity's toolbar and change it in every fragment ? These two are different things.Weld
want to implement separate toolbar in each fragmentAhmedahmedabad
@apk take a look at the answer it will clear things for you.Weld
W
4

You can use custom toolbars in your fragments. and you have to implement them separately for each fragment. First of all declare your toolbar in your fragment layout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/activity_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            >
<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:gravity="start"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    >

    <RelativeLayout
       // your custom toolbar layout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </RelativeLayout>


</android.support.v7.widget.Toolbar>

Then implement it in your fragment:

find it in fragment:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle 
    savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, container, false);
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);

        //set toolbar appearance
        toolbar.setBackground(your background);

        //for create home button
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

  return view;
}

And yes you can implement click listeners and whatever you want to do with your toolbar. For more take a look at the second answer: How to get custom toolbar

Weld answered 4/12, 2017 at 7:50 Comment(5)
is there any gaurantee that 'getActivity()' will not return null in onCreateView??? getActivity is gauranteed to be not null in onActivityCreated() callback which is called by the framework in sequence like onCreateView->onViewCreated->onActivityCreatedBreeks
@Breeks getActivity() shouldn't be null. Yeah it can be in any other method you make but onCreate method should have getActivity not null. Or if you want to make sure that it shouldn't be null you can have context cast to the activity calling the fragment in onAttach(Context ctx) method.Weld
@Weld As already suggested as edit: 1. You have to use setBackgroundColor, setBackground doesn't take an int (= Color, e.g. Color.RED) but only a Drawable. 2. You have to return the view because that's what the @Override demands of the function. So basically: The code, as it is currently, won't build. I'll suggest the edit again, please test/accept it or provide alternative code that'll build.Gravesend
@Gravesend Please take a look at the question the OP doesn't want the color to be set but instead wants a separate toolbar for a fragment. These two are different things :) and last thing you are right that we should return a view in onCreateView method.Weld
@Weld I know, that's why I was confused why you'd added it and the "home button" bit in the first place but since it was already there, I didn't want to completely change the answer by deleting it (that's not what edits are for after all ;)), even though it has nothing to do with the question. But still, setBackground isn't the right function to use if you want to set the background color with a color (= int), as corrected in both edits that were rejected (the 1st by you). So please either delete the irrelevant code or accept my edit to provide the correct functions to make it compile-able.Gravesend

© 2022 - 2024 — McMap. All rights reserved.