Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show toolbar after hidden by scrolling on button click

I found this question in stack overflow but there is no proper solution.
Programmatically show Toolbar after hidden by scrolling (Android Design Library)

I have a custom view in a toolbar and the toolbar will show and hide when I scroll up and down.
When I scroll up, the toolbar will hide and I am showing one icon in ActionBar menu. when I click on that menu, I want to show the hidden toolbar with its content with the same animation.
I tried to translate the toolbar but no use.
This is my code of toolbar with the custom view.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="3dp"
    android:layout_marginBottom="3dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="15dp"
            android:background="@drawable/searchbox_bg"
            android:gravity="center"
            android:orientation="horizontal"
            android:weightSum="2">

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:hint="@string/home_search_hint"
                android:maxLines="1"
                android:textSize="15dp"
                android:singleLine="true"
                android:id="@+id/home_search"
                android:drawableStart="@android:drawable/ic_menu_search"
                android:drawableLeft="@android:drawable/ic_menu_search"
                android:drawablePadding="3dp"
                android:drawableTint="@color/gray_2"
                android:drawingCacheQuality="high"
                android:paddingLeft="10dp"
                android:background="@drawable/searchbox_bg"
                android:layout_weight="2"
                />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:id="@+id/home_speak"
                android:background="@drawable/searchbox_bg"
                android:src="@android:drawable/ic_btn_speak_now"/>
        </LinearLayout>
        </android.support.v7.widget.Toolbar>


    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/transparent"
        android:visibility="invisible"/>


</android.support.design.widget.AppBarLayout>

visible toolbar with invisible menu hidden toolbar with visible menu

like image 429
Anooj Krishnan G Avatar asked Jan 06 '23 17:01

Anooj Krishnan G


1 Answers

To expand or collapse the toolbar programmatically you should use

setExpanded(boolean expanded, boolean animate) 

method see this Doc

lets come to your answer so to expand a tool bar programmatically use this

To Expand Tool bar

AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBarLayout);
        appBarLayout.setExpanded(true, true);

and if you want to collapse its use:

AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBarLayout);
        appBarLayout.setExpanded(false, true);

hope this will help you :)

like image 173
Ramz Avatar answered Jan 21 '23 13:01

Ramz