import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class Main extends Activity { private int searchBtnId = Menu.FIRST; private int scheduleBtnId = Menu.FIRST + 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuItem searchMItm = menu.add(Menu.NONE, searchBtnId ,searchBtnId,"Search"); searchMItm.setIcon(R.drawable.search); MenuItem scheduleMItm = menu.add(Menu.NONE, scheduleBtnId ,scheduleBtnId,"Schedule" ); scheduleMItm.setIcon(R.drawable.schedule); return super.onCreateOptionsMenu(menu); } }
Quick Explanation
MenuItem scheduleMItm = menu.add(Menu.NONE, scheduleBtnId ,scheduleBtnId,"Schedule" );
menu.add returns a MenuItem in which we can use its method setIcon, scheduleMItm.setIcon(R.drawable.schedule);, here we imported a png into our folder and named it as schedule.
Source Code
Main.java
References
MenuItem
Options Menu in Android (Code)
Update History
Jan 17, 2012 - Visual Update
4 comments:
just wanted to add that the icon size of 32x32 is fine. i've used png's with transparency, works like a charme.
Hi how to design a option menu similar to windows start menu in android? any hint or idea on this regard will be really helpful.
Thanks
Droidcoder
hey ... i want to open up options menu by clicking a button ... is it possible? if so, please help me out ...
thanks in advance
Thanks for this code!
Post a Comment