Monday, 15 April 2013

Android action buttons in action bar not appearing? -



Android action buttons in action bar not appearing? -

i want create add together button in action bar doesn't seem appear when run thee code.

here main_activity.java

public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.main_activity_actions, menu); homecoming super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_add) { homecoming true; } homecoming super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); homecoming rootview; } } }

this main_activity_actions.xml

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_add" android:icon="@drawable/ic_add" android:title="add" android:showasaction="always" /> </menu>

please help cant figure out what's wrong !

since activity extends actionbaractivity using appcompat back upwards library.

so alter

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_add" android:icon="@drawable/ic_add" android:title="add" yourapp:showasaction="always" /> ... </menu>

quoting docs

notice showasaction attribute above uses custom namespace defined in tag. necessary when using xml attributes defined back upwards library, because these attributes not exist in android framework on older devices. must utilize own namespace prefix attributes defined back upwards library.

android android-actionbar action-menu

No comments:

Post a Comment