android - How to change the position of menu items on actionbar -
i'm developing 1 application in have add custom layout on actionbar. adding custom layout done, when i'm adding menu items on actionbar custom layout changes it's position right of actionbar center of actionbar. below image have achieved far.
i want on actionbar.
custom layout(yellow button) @ right part of actionbar , menu items in middle.
adding code achieve custom layout using native android actionbar:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); getactionbar().setdisplayhomeasupenabled(true); getactionbar().sethomebuttonenabled(true); actionbar = getactionbar(); actionbar.setdisplayshowtitleenabled(false); actionbar.setdisplayuselogoenabled(false); actionbar.setdisplayhomeasupenabled(false); actionbar.setdisplayshowcustomenabled(true); cview = getlayoutinflater().inflate(r.layout.header, null); actionbar.setcustomview(cview); } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.websearch_menu, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: finish(); default: return super.onoptionsitemselected(item); } }
how can achieved?
any kind of appreciated.
i assume setting custom view custom view in action bar. if using actionbarsherlock can instead add custom view normal menu item , set action view using .setactionview(yourcustomview or yourcustomlayoutid)
. can set .setshowasaction(menuitem.show_as_action_always)
display view. make sure custom view set width wrap_content
or otherwise push other menu items off screen. might work without actionbarsherlock, haven't tried there yet.
edit: try modifying oncreateoptionsmenu
method
@override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.websearch_menu, menu); // add menu.add(menu.none, 0, menu.none, "custom") .setactionview(r.layout.header) .setshowasaction(menuitem.show_as_action_always); return true; }
and remove oncreate method
actionbar.setdisplayshowcustomenabled(true); cview = getlayoutinflater().inflate(r.layout.header, null); actionbar.setcustomview(cview);
edit:
if need reference views custom layout, can in oncreateoptionsmenu
method follows (note: wrote code in browser, no guarantee didn't make typo or something):
@override public boolean oncreateoptionsmenu(menu menu) { // inflate menu , add item id viewid, // has layout actionview view actionview = menu.getitem(viewid).getactionview(); view viewfrommylayout = actionview.findviewbyid(r.id.viewfrommylayout); // here can set listeners view or store reference // somewhere, can use later return true; }
Comments
Post a Comment