Skip to content
Related Articles

Related Articles

Improve Article

How to Use Morphy Toolbar Library in Android App?

  • Last Updated : 11 Jul, 2021
Geek Week

MorphyToolbar is a library that allows us to have a custom toolbar with a title, subtitle, and a picture that further offers the possibility to animate the view between transitions. This library is extremely easy to integrate and offers several customizations. 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.

Step 2: Add JitPack 

Navigate to the Gradle Scripts > build.gradle(Project: Project Name), add jitpack at the end of repositories, and sync the project.

maven { url "" }

Step 3: 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 {
    implementation 'com.github.badoualy:morphy-toolbar:1.0.4'  

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. 


    <!-- Base application theme. -->
    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>>
    <style name="AppTheme" parent="AppTheme.Base">
    <style name="ToolbarStyle" parent="Widget.AppCompat.Toolbar">
        <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
        <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>

Step 5: 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. 


<?xml version="1.0" encoding="utf-8"?>
<!--Use Coordinator Layout -->
            app:titleTextColor="#FFFFFF" />
    <!--Floating Action Button is attached to 
        the AppBarLayout using app:layout_anchor.-->
        app:layout_anchorGravity="center_vertical|end|right" />

Step 6: Working with the file

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import com.github.badoualy.morphytoolbar.MorphyToolbar;
public class MainActivity extends AppCompatActivity {
    MorphyToolbar morphyToolbar;
    // primary2 determines the color 
    // of morphyToolbar when expanded
    int primary2 = Color.parseColor("#fbc757");
    // primaryDark2 determines the color
    // of status bar when expanded
    int primaryDark2 = Color.parseColor("#e6b449");
    AppBarLayout appBarLayout;
    Toolbar toolbar;
    FloatingActionButton fabPhoto;
    protected void onCreate(Bundle savedInstanceState) {
        appBarLayout = findViewById(;
        toolbar = findViewById(;
        fabPhoto = findViewById(;
        // disableAppBarDrag() disables the scrolling-of
        // AppBarLayout in CoordinatorLayout
        // i.e prevents the user from hiding
        // the ToolBar when swiped above
        // hideFab() hides the floating action button
        // Attaching MorphyToolbar to the given activity/toolbar
        morphyToolbar = MorphyToolbar.builder(this, toolbar)
                // Title of Toolbar
                .withTitle("GeeksForGeeks DS and Algorithms Course")
                // Subtitle of Toolbar
                .withSubtitle("16,000 Participants")
                // Adding image to the toolbar
                // if you want to hide the img when 
                // AppBarLayout collapses, set it to true
        morphyToolbar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // if morphyToolbar is collapsed, expand it
                // and if expanded, collapse it.
                if (morphyToolbar.isCollapsed()) {
                    morphyToolbar.expand(primary2, primaryDark2,
                            new MorphyToolbar.OnMorphyToolbarExpandedListener() {
                                public void onMorphyToolbarExpanded() {
                                    // shows the floating action button
                                    // when morphyToolbar expands
                } else {
                    // hides the floating action 
                    // button when morphyToolbar collapses
        // adding a back button
        if (getSupportActionBar() != null) {
                    | ActionBar.DISPLAY_SHOW_TITLE
                    | ActionBar.DISPLAY_SHOW_CUSTOM);
    private void disableAppBarDrag() {
        // disables the scrolling-of AppBarLayout in CoordinatorLayout
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        AppBarLayout.Behavior behavior = new AppBarLayout.Behavior();
        behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
            public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
                return false;
    // These two methods hideFab() & showFab() are 
    // for hiding and showing the floating
    // action button, which is to be used only 
    // if you are adding a floating action button.
    private void hideFab() {;
        final CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) fabPhoto.getLayoutParams();
    private void showFab() {
        final CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) fabPhoto.getLayoutParams();
        layoutParams.anchorGravity = Gravity.RIGHT | Gravity.END | Gravity.BOTTOM;
    public void onBackPressed() {
        // if morphyToolbar is already
        // collapsed finish the activity
        // else collapse the toolbar
        if (!morphyToolbar.isCollapsed()) {
        } else
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
                // It is called, when a user
                // presses back button
                return true;
        return super.onOptionsItemSelected(item);


GitHub Repository: MorphyToolbarLibrary

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :