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.

amodkanthe
  • 4,345
  • 6
  • 36
  • 77
  • 1
    If navigation drawer is single then why you want to have toolbar in each fragment ? Explain – ADM Dec 04 '17 at 07:19
  • no some internal detail pages/fragments are on non-drawer activity – amodkanthe Dec 04 '17 at 07:20
  • 2
    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. – ADM Dec 04 '17 at 07:23
  • Yes you can use separate toolbars for each fragment. It's not a bad practice. You can implement it if that's your requirement. – Umair Dec 04 '17 at 07:23
  • @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. – Umair Dec 04 '17 at 07:37
  • want to implement separate toolbar in each fragment – amodkanthe Dec 04 '17 at 07:40
  • @apk take a look at the answer it will clear things for you. – Umair Dec 04 '17 at 07:51

1 Answers1

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

Umair
  • 6,366
  • 15
  • 42
  • 50
  • 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->onActivityCreated – Farhan May 10 '19 at 03:17
  • @Farhan 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. – Umair May 10 '19 at 03:48
  • @Umair 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. – Neph Feb 27 '20 at 10:01
  • @Neph 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. – Umair Feb 27 '20 at 11:23
  • @Umair 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. – Neph Feb 27 '20 at 12:06