[SOLVED] Android: Dynamically change Options Menu Items

import android.app.Activity;
02 import android.os.Bundle;
03 import android.view.Menu;
04 import android.view.View;
05 import android.view.View.OnClickListener;
06 import android.widget.Button;
07
08 /**
09 * Class which shows how to change dynamically options menu items
10 * @author FaYnaSoft Labs
11 */
12 public class Main extends Activity {
13
14 private Button clickBtn;
15 private boolean isChangedStat = false;
16 private static final int MENUITEM = Menu.FIRST;
17
18 @Override
19 public void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.main);
22 clickBtn = (Button) findViewById(R.id.click);
23 clickBtn.setText("Click me");
24 clickBtn.setOnClickListener(new OnClickListener() {
25
26 @Override
27 public void onClick(View v) {
28 if (isChangedStat) {
29 isChangedStat = false;
30 } else {
31 isChangedStat = true;
32 }
33 }
34 });
35 }
36
37 @Override
38 public boolean onPrepareOptionsMenu(Menu menu) {
39 menu.clear();
40 if (isChangedStat) {
41 menu.add(0, MENUITEM, 0, "True");
42 } else {
43 menu.add(0, MENUITEM, 0, "False");
44 }
45 return super.onPrepareOptionsMenu(menu);
46 }
47 }

Dynamically change Options Menu Items in Android The Developers Info.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: