Sunday, May 12, 2013

Show and hide ActionBar using Java code

With android:theme="@android:style/Theme.Holo" is using, Java code can detect if ActionBar is showing currently by calling ActionBar.isShowing() method, and show/hide it by calling ActionBar.show()/ActionBar.hide().

Show and hide ActionBar using Java code


package com.example.androidactionbar;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Button buttonToggleActionBar = (Button)findViewById(R.id.toggleactionbar);
  buttonToggleActionBar.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    ActionBar myActionBar = getActionBar();
    boolean myActionBarisShowing = myActionBar.isShowing();
    if(myActionBarisShowing){
     myActionBar.hide();
     Toast.makeText(getApplicationContext(), 
       "Hide ActionBar", 
       Toast.LENGTH_LONG).show();
    }else{
     myActionBar.show();
     Toast.makeText(getApplicationContext(), 
       "Show ActionBar", 
       Toast.LENGTH_LONG).show();
    }
   }});
 }

}


<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/toggleactionbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Toggle ActionBar" />

</LinearLayout>


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidactionbar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo" >
        <activity
            android:name="com.example.androidactionbar.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Related:
- Display and hide ActionBar in XML

Next:
- Save state of ActionBar visibility

No comments:

Post a Comment