Open In App

How to Use multiline-collapsingtoolbar Library in Android App?

Last Updated : 11 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 
    xmlns:tools="http://schemas.android.com/tools"
    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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads