Native Mobile App Development With Java

Android Collapsing toolbar Example java

24-Sep-2024

Android Collapsing toolbar Example java

1/  Design layout xml file and customize as per your requirment

<!-- Use coordinetor layout -->

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_height="250dp"
android:layout_width="match_parent"
>

<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:toolbarId="@+id/toolbar"
app:titleCollapseMode="scale"
app:titleEnabled="true"
app:collapsedTitleTextColor="@color/black"
app:expandedTitleTextColor="#0032CD32"
app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
app:layout_scrollInterpolator="@android:anim/decelerate_interpolator"

app:contentScrim="@color/mint_green">



<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:title="Ghor Bagan"
app:titleTextColor="@color/black"
app:layout_collapseMode="pin"
app:titleTextAppearance="@style/TextAppearance.AppCompat.Large"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:background="@drawable/dashboard_bg_shape"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">


</androidx.core.widget.NestedScrollView>


</androidx.coordinatorlayout.widget.CoordinatorLayout>


2/  to set the toolbar and enable the collapsing effect in Java

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

// To set title programmatically
getSupportActionBar().setTitle("Toolbar title");

Comments