android - How to bind tabs from menu.xml with code? -
i'm new in android. stumbled on connecting tabs menu code.
here menu.xml file:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/multitouchexample" android:title="multitouch" android:icon="@drawable/ic_multitouch_grey" android:showasaction="ifroom"> </item> </menu>
and here myactivity.java:
//includes public class myactivity extends activity { final string log_tag = "taglogs"; actionbar actionbar; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); actionbar = this.getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); log.d(log_tag, "oncreate start"); log.d(log_tag, new integer(actionbar.gettabcount()).tostring()); } }
and have in logcat:
> d/taglogs﹕ oncreate start > d/taglogs﹕ 0
so says me, have 0 tabs, have 1 in menu.xml , see when run program. it? need do?
this screenshot of working:
update: when want link button res/layout/main.xml code:
button = (button) findviewbyid(r.id.button);
so don't create button operator new in code. there way solve problem button?
so, found solution on stackoverflow in context.
here answer:
first, should save reference menu:
publid boolean oncreateoptionsmenu(menuitem menu){ getmenuinflater().inflate(r.menu.menu, menu); this.menu = menu;// this.menu field menu menu in myactivity class return true; }
via reference have full access menuitems.
for example:
menuitem item = menu.finditem(r.id.multitouchexample); item.setonmenuitemclicklistener(new menuitem.onmenuitemclicklistener() { @override public boolean onmenuitemclick(menuitem item) { item.seticon(r.drawable.ic_multitouch_red); multitouchexample(); return true; } });
Comments
Post a Comment