{"id":3628,"date":"2025-04-14T05:49:51","date_gmt":"2025-04-14T05:49:51","guid":{"rendered":"https:\/\/alsaeeddev.com\/?p=3628"},"modified":"2025-04-14T06:20:15","modified_gmt":"2025-04-14T06:20:15","slug":"collapsing-toolbar-layout-in-android-java","status":"publish","type":"post","link":"https:\/\/alsaeeddev.com\/shop\/collapsing-toolbar-layout-in-android-java\/","title":{"rendered":"Collapsing Toolbar Layout in Android Java"},"content":{"rendered":"<p><strong>Introduction<\/strong><br \/>\nIn this tutorial, we will implement a Collapsing Toolbar Layout in Android Java using <code>AppBarLayout<\/code> and <code>NestedScrollView<\/code>. The <code>CollapsingToolbarLayout<\/code> provides a smooth collapsing effect when the user scrolls through a <code>NestedScrollView<\/code>.<\/p>\n<h3>Prerequisites<\/h3>\n<ul>\n<li>Android Studio installed<\/li>\n<li>Basic knowledge of XML and Java\/Kotlin<\/li>\n<li>A working Android project<\/li>\n<\/ul>\n<h3>Step 1: activity_main.xml<\/h3>\n<p>Create an <code>activity_main.xml<\/code> file with the following code:<\/p>\n<pre><code>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"&gt;\r\n\r\n    &lt;com.google.android.material.appbar.AppBarLayout\r\n        android:id=\"@+id\/appBarLayout\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"300dp\"\r\n        android:theme=\"@style\/ThemeOverlay.AppCompat.Dark.ActionBar\"&gt;\r\n\r\n        &lt;com.google.android.material.appbar.CollapsingToolbarLayout\r\n            android:id=\"@+id\/collapsingToolbar\"\r\n            android:layout_width=\"match_parent\"\r\n            android:layout_height=\"match_parent\"\r\n            app:contentScrim=\"@color\/black\"\r\n            app:layout_scrollFlags=\"scroll|exitUntilCollapsed\"\r\n            android:fitsSystemWindows=\"true\"\r\n            app:titleEnabled=\"true\"\r\n            app:expandedTitleTextAppearance=\"@android:color\/transparent\"&gt;\r\n\r\n            &lt;ImageView\r\n                android:id=\"@+id\/headerImage\"\r\n                android:layout_width=\"match_parent\"\r\n                android:layout_height=\"match_parent\"\r\n                android:alpha=\"0.6\"\r\n                android:scaleType=\"centerCrop\"\r\n                android:src=\"@drawable\/img\"\r\n                app:layout_collapseMode=\"parallax\" \/&gt;\r\n\r\n            &lt;androidx.appcompat.widget.Toolbar\r\n                android:id=\"@+id\/toolbar\"\r\n                android:layout_width=\"match_parent\"\r\n                android:layout_height=\"?attr\/actionBarSize\"\r\n                android:background=\"@android:color\/transparent\"\r\n                app:layout_collapseMode=\"pin\"\r\n                app:popupTheme=\"@style\/ThemeOverlay.AppCompat.Dark\" \/&gt;\r\n\r\n        &lt;\/com.google.android.material.appbar.CollapsingToolbarLayout&gt;\r\n\r\n    &lt;\/com.google.android.material.appbar.AppBarLayout&gt;\r\n\r\n    &lt;androidx.core.widget.NestedScrollView\r\n        android:id=\"@+id\/nested_scroll\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"match_parent\"\r\n        app:layout_behavior=\"@string\/appbar_scrolling_view_behavior\"&gt;\r\n\r\n        &lt;TextView\r\n            android:id=\"@+id\/text_view\"\r\n            android:layout_width=\"match_parent\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:padding=\"16dp\"\r\n            android:text=\"Your long text here...\" \/&gt;\r\n    &lt;\/androidx.core.widget.NestedScrollView&gt;\r\n\r\n&lt;\/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;\r\n<\/code><\/pre>\n<h3>Step 2: MainActivity.java<\/h3>\n<p>Modify <code>MainActivity.java<\/code> to initialize the toolbar and set the collapsing behavior:<\/p>\n<pre><code>\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n\r\n        \/\/ Set up Toolbar\r\n        Toolbar toolbar = findViewById(R.id.toolbar);\r\n        toolbar.setTitle(\"\");\r\n        setSupportActionBar(toolbar);\r\n    \r\n\r\n        \/\/ Collapsing Toolbar Layout\r\n        CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsingToolbar);\r\n        AppBarLayout appBarLayout = findViewById(R.id.appBarLayout);\r\n\r\n        collapsingToolbarLayout.setTitle(\"\");\r\n\r\n\r\n\r\n        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {\r\n            boolean isShown = true;\r\n            int scrollRange = -1;\r\n\r\n            @Override\r\n            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {\r\n                if (scrollRange == -1) {\r\n                    scrollRange = appBarLayout.getTotalScrollRange();\r\n                }\r\n\r\n                if (Math.abs(verticalOffset) &gt;= scrollRange) {\r\n                    \/\/ Toolbar is fully collapsed\r\n                    Log.d(\"Alsaeed\", \"onOffsetChanged: COLLAPSED\");\r\n                    collapsingToolbarLayout.setTitle(\"Toolbar Title\");\r\n                    collapsingToolbarLayout.setCollapsedTitleTextColor(Color.RED);\r\n\r\n                    isShown = true;\r\n                } else if (verticalOffset == 0) {\r\n                    \/\/ Toolbar is fully expanded\r\n                    Log.d(\"Alsaeed\", \"onOffsetChanged: EXPANDED\");\r\n                    collapsingToolbarLayout.setTitle(\"\");  \/\/ Hide title when expanded\r\n                    isShown = false;\r\n                }\r\n            }\r\n        });\r\n    }\r\n\r\n \r\n}\r\n\r\n<\/code><\/pre>\n<h3>Step 3: Run the Application<\/h3>\n<ol>\n<li>Compile and run the app.<\/li>\n<li>Scroll through the content.<\/li>\n<li>Observe the collapsing effect on the toolbar.<\/li>\n<\/ol>\n<hr \/>\n<p><iframe loading=\"lazy\" title=\"Collapsing Toolbar Layout in Android | Free Source Code | Android Studio Tutorial #shorts #android\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/1wzfaz_BLW8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<hr \/>\n<p><a href=\"https:\/\/github.com\/alsaeeddev\/CollapsingToolbar\/archive\/refs\/heads\/main.zip\">Download Source Code<\/a><\/p>\n<h3>Conclusion<\/h3>\n<p>The <code>CollapsingToolbarLayout<\/code> provides a visually appealing effect for modern UI designs. It enhances user experience by offering a smooth transition between expanded and collapsed toolbar states. You can further customize it with animations, icons, and additional widgets inside the <code>AppBarLayout<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this tutorial, we will implement a Collapsing Toolbar Layout in Android Java using AppBarLayout and NestedScrollView. The CollapsingToolbarLayout provides a smooth collapsing effect when the user scrolls through a NestedScrollView. Prerequisites Android Studio installed Basic knowledge of XML and Java\/Kotlin A working Android project Step 1: activity_main.xml Create an activity_main.xml file with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3632,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,117,164],"tags":[],"class_list":["post-3628","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-java","category-source-codes"],"_links":{"self":[{"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/posts\/3628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/comments?post=3628"}],"version-history":[{"count":19,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/posts\/3628\/revisions"}],"predecessor-version":[{"id":4044,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/posts\/3628\/revisions\/4044"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/media\/3632"}],"wp:attachment":[{"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/media?parent=3628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/categories?post=3628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/tags?post=3628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}