Noman uddin Noman uddin - 6 months ago 104
Android Question

how to hide/show widget.AppBarLayout on click in android app

I have developed an application and my requirement is to show setting menu at the top on click.

Below is the code for my Activity_main.xml,

Any shortcut way to auto hide and show the app bar layout?




<android.support.design.widget.AppBarLayout
android:id="@+id/appbarlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

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

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />




Following is the code at main_activity.java for onclick logic:

final AppBarLayout appbar = (AppBarLayout) findViewById(R.id.appbarlayout);
appbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
appbar.setVisibility(View.GONE);
}
});


Thanks

example

in the screen shot you can see the setting button, I want to hide this whole blue color appbar.

Answer

Just use View's setVisibility() to hide and show the AppBarLayout.

If you want it to animate then add android:animateLayoutChanges="true" to the AppBarLayout's xml.