Iam trying to create collapsing toolbar in my project for fragment and activity, but the collapsing toolbar is not working well.Please help me find the solution, as iam new to android. Thank you in advance.
Asked
Active
Viewed 5,549 times
2
-
Have you tried this solution? http://stackoverflow.com/questions/30739806/coordinator-layout-with-toolbar-in-fragments-or-activity – AndroidBeginner Feb 06 '17 at 09:15
1 Answers
1
Firstly you need to create a AppBar layout as the main parent. Inside this add a CollapsingToolbarLayout. Inside CollapsingToolbarLayout add whatever you want inside your toolbar and finally the toolbar.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways">
<!--This can be anything, here I've added a view pager inside collapsing toolbar-->
<android.support.v4.view.ViewPager
android:id="@+id/pager_introduction"
android:layout_width="match_parent"
android:layout_height="192dp"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
tools:listitem="@layout/pager_item" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Now for the android part instantiate the toolbar and set it.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
In my case I've added a viewpager, a seperate adapter will have to created to populate that viewpager.
-
Would you recommend putting the CollapsingToolbarLayout in the Activity or Fragment layout? – Peter G. Williams Apr 22 '20 at 12:16