Multiline-CollapsingToolbar library, as the name itself suggests, allows us to have more than one line in the CollapsingToolbarLayout. Using this library, we can deal with the multiline titles (with a customizable maximum number of lines) in the expanded state with a nice fade-away animation. In this article, we will be implementing this library in an Android App using Java language. A sample GIF is given below to get an idea about what we are going to do in this article.Â
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Note: Make sure you are using android support libraries while creating a new project, since multiline-collapsingtoolbar library does not support AndroidX.
Step 2: Add the library dependencyÂ
Navigate to the Gradle Scripts > build.gradle(Module:app), add the library in the dependencies section, and sync the project.
dependencies {
// use the same version of Android support libraries as of collapsing toolbar
implementation 'net.opacapp:multiline-collapsingtoolbar:27.1.1'
}
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in detail.
XML
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout     android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    tools:context=".MainActivity">      <android.support.design.widget.AppBarLayout        android:layout_width="match_parent"        android:layout_height="192dp"        android:background="?attr/colorPrimary"        android:fitsSystemWindows="true"        android:theme="@style/AppTheme.AppBarOverlay">          <!-- Adding CollapsingToolbarLayout-->        <!-- maxLines sets the maximum no of lines to be displayed in expanded form-->        <net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:theme="@style/AppTheme.AppBarOverlay"            app:expandedTitleTextAppearance="@style/TextAppearance.ExpandedTitle"            app:layout_scrollFlags="scroll|exitUntilCollapsed"            app:lineSpacingMultiplier="1.2"            app:maxLines="3"            app:title="GeeksForGeeks is an amazing website for DS and Algorithms. ">              <android.support.v7.widget.Toolbar                android:id="@+id/toolbar"                android:layout_width="match_parent"                android:layout_height="?attr/actionBarSize"                app:layout_collapseMode="pin"                app:popupTheme="@style/AppTheme.PopupOverlay" />          </net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout>      </android.support.design.widget.AppBarLayout>      <!--Nested Scroll view contains the main_content-->    <android.support.v4.widget.NestedScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        app:layout_behavior="@string/appbar_scrolling_view_behavior">          <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="15dp"            android:text="@string/app_name" />              </android.support.v4.widget.NestedScrollView>  </android.support.design.widget.CoordinatorLayout> |
Step 4: Working with the styles.xml file
Navigate to the app > res > values> styles.xml and add the below code to that file. Below is the code for the styles.xml file.Â
XML
<resources>Â Â Â Â <!-- Base application theme. -->Â Â Â Â <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">Â Â Â Â Â Â Â Â <!-- Customize your theme here. -->Â Â Â Â Â Â Â Â <item name="colorPrimary">@color/colorPrimary</item>Â Â Â Â Â Â Â Â <item name="colorPrimaryDark">@color/colorPrimaryDark</item>Â Â Â Â Â Â Â Â <item name="colorAccent">@color/colorAccent</item>Â Â Â Â Â Â </style>Â Â Â Â Â Â <style name="AppTheme.NoActionBar">Â Â Â Â Â Â Â Â <item name="windowActionBar">false</item>Â Â Â Â Â Â Â Â <item name="windowNoTitle">true</item>Â Â Â Â </style>Â Â Â Â Â Â <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />Â Â Â Â Â Â <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />Â Â <!--Â Â Â Sets the textSize of Title in Collapsing Toolbar-->Â Â Â Â <style name="TextAppearance.ExpandedTitle"Â Â Â Â Â Â Â Â parent="TextAppearance.AppCompat.Widget.ActionBar.Title">Â Â Â Â Â Â Â Â <item name="android:textSize">26sp</item>Â Â Â Â </style>Â Â </resources> |
Step 5: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file.Â
Java
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.Toolbar;  public class MainActivity extends AppCompatActivity {      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                  // set the toolbar as the action bar for the activity        Toolbar toolbar = findViewById(R.id.toolbar);        setSupportActionBar(toolbar);    }} |
Step 6: Working with the AndroidManifest.xml file
Navigate to the app > manifests > AndroidManifest.xml and add the below code in the MainActivity Tag.Â
android:theme="@style/AppTheme.NoActionBar"
Output: Â
GitHub Repository: Multiline-CollapsingToolbar Library

