How to set action bar visibility dynamically via coding so user can display and hide action bar.
In this tutorial we are dynamically showing and hiding action bar on button click. This is a most required feature for app user because some times when you are showing some images with zoom in, zoom out and want to add full screen mode as user choice then this feature comes that user can hide or show progress bar by itself. So here is the complete step by step tutorial for Show Hide ActionBar in android programmatically on button click.
How to Show Hide ActionBar in android programmatically on button click.
Code for MainActivity.java file.
package com.showhideactionbar_android_examples.com; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { Button show,hide; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); hide = (Button)findViewById(R.id.button1); show = (Button)findViewById(R.id.button2); hide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub getActionBar().hide(); } }); show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub getActionBar().show(); } }); } }
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.showhideactionbar_android_examples.com.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="80dp" android:text="Click here to Hide Action Bar" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:text="Click here to Show Action Bar" /> </RelativeLayout>
Screenshot: