February 28, 2010

[SOLVED] Android: Dynamically change Options Menu Items








import android.app.Activity;








02import android.os.Bundle;








03import android.view.Menu;








04import android.view.View;








05import android.view.View.OnClickListener;








06import android.widget.Button;








07








08/**








09 * Class which shows how to change dynamically options menu items








10 * @author FaYnaSoft Labs








11 */








12public 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.

No comments:

Post a Comment