-2

I've got one activity and a lot of fragments.

One of my fragments is full screen and has CollapsingToolbarLayout. It was added by .add by fragment manager.

I need toolbar, buttons etc, on my fragment: enter image description here

But for this I need to set toolbar for activity like this:

((MyActivity)getActivity()).setSupportActionBar(toolbarFromFragment);

But after that I can't change title of activity. I tried a lot of ways, like getting CollapsingToolbarLayout from fragment and setting title to it, or making it static, etc. Also I tried to set toolbar again from findViewById. And other ways from google, but no one helps.

How I can resolve this problem? I need to set own toolbar to this fragment, and then dynamically change title.

Peter Samokhin
  • 864
  • 1
  • 9
  • 19

3 Answers3

1

In my app i have a few activities. In each activity I have my own toolbar . I set the title of the activity in the toolbar as follows:

((Toolbar) findViewById (R.id.myToolbar)) .setTitle(myActivityTitle);

It is working.

0

Try this

((MyActivity)getActivity()).setTitleEnabled(true);
((MyActivity)getActivity()).setTitle("Your Title");
Sabbir Ahmed
  • 351
  • 1
  • 13
0

Add the below in fragment_**.xml

<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".assign.AssignOrderActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:elevation="@dimen/appbar_elevation"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                app:title="@string/app_name"
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="?attr/colorPrimary"
                app:titleEnabled="false"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">


                <android.support.v7.widget.Toolbar

                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?android:attr/actionBarSize"
                    android:layout_marginLeft="@dimen/list_toolbar_side_margin"
                    android:layout_marginRight="@dimen/list_toolbar_side_margin"
                    android:elevation="@dimen/appbar_elevation"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />


            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView

            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <Add your views here>

    </android.support.v4.widget.NestedScrollView>
    </android.support.design.widget.CoordinatorLayout>

In the fragment (Java):

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

    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

            ActionBar ab = ((AppCompatActivity) getActivity()).getSupportActionBar();
            // Enable the Up button
            ab.setDisplayHomeAsUpEnabled(true);

              CollapsingToolbarLayout collapsingToolbar =
                (CollapsingToolbarLayout) 
                     rootView.findViewById(R.id.collapsing_toolbar);

            collapsing_toolbar.setTitleEnabled(true);
            collapsing_toolbar.setTitle("My Title");
loics2
  • 616
  • 1
  • 10
  • 24
tanni tanna
  • 544
  • 4
  • 12