Show hide buttons on Activity screen from Action Bar in android

The action bar is a great place to add overall application controlling actions. We can attach action code for each item in the action bar. In this tutorial, we shall add two buttons on the action bar and add the corresponding implementation code for each of them.

Layout XML

<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=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="33dp"
        android:layout_marginTop="16dp"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="16dp"
        android:text="Button" />

</RelativeLayout>

menu xml file

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/itShow"
        android:showAsAction="always" android:title="Show">
    </item>
    <item
        android:id="@+id/itHide"
        android:showAsAction="always" android:title="Hide">
    </item>

</menu>

Java Code

package com.example.showhideitemexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button bt1, bt2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bt1 = (Button) findViewById(R.id.button1);
		bt2 = (Button) findViewById(R.id.button2);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.itShow:
			bt1.setVisibility(View.VISIBLE);
			bt2.setVisibility(View.VISIBLE);
			break;

		case R.id.itHide:
			bt1.setVisibility(View.GONE);
			bt2.setVisibility(View.GONE);
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}

Screenshots for show/hide using action bar

1) Once launched, both the buttons are shown:

showhide1

2) On pressing the hide button in action bar, the buttons on the screen are hidden.

showhide2

Download code for this example

How to show hide items using action bar

Show hide buttons on Activity screen from Action Bar in android admin Android

The action bar is a great place to add overall application controlling actions. We can attach action code for each item in the action bar. In this tutorial, we shall add two buttons on the action bar and add the corresponding implementation code for each of them. Layout XML <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

The action bar is a great place to add overall application controlling actions. We can attach action code for each item in the action bar. In this tutorial, we shall add two buttons on the action bar and add the corresponding implementation code for each of them. <h2>Layout XML</h2> 1 <h2>menu xml file</h2> 1 <h2>Java Code</h2> 1 <h2>Screenshots for show/hide using action bar</h2> 1) Once launched, both the buttons are shown: <img src="http://www.javaexperience.com/wp-content/uploads/2013/09/showhide1-180x300.png" alt="showhide1" width="180" height="300" class="alignnone size-medium wp-image-3631" /> 2) On pressing the hide button in action bar, the buttons on the screen are hidden. <img src="http://www.javaexperience.com/wp-content/uploads/2013/09/showhide2-180x300.png" alt="showhide2" width="180" height="300" class="alignnone size-medium wp-image-3632" /> <h2>Download code for this example</h2> <div class="download-code"><img src="http://www.javaexperience.com/wp-content/uploads/2012/10/downloadcode-e1349278942164.jpg"><a href="http://www.javaexperience.com/android/ShowHideItemExample.zip"><br><br><strong>How to show hide items using action bar</strong></a></div>

我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章