android - Action Bar not showing in 4.2.2 -
solution: problem image search icon. android did not find image search action , had not added them in res-folders , started show after that...
i trying add action bar app , following basic app tutorial shown in google developer website
i have written following code.
mainactivity
public class mainactivity extends actionbaractivity { public void opensearch(){ system.out.println("test search"); } public void opensettings(){ system.out.println("test settings"); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @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); return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // handle presses on action bar items switch (item.getitemid()) { case r.id.action_search: opensearch(); return true; case r.id.action_settings: opensettings(); return true; default: return super.onoptionsitemselected(item); } } }
res/menu/main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- search, should appear action button --> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" android:showasaction="ifroom" /> <!-- settings, should in overflow --> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:showasaction="never" /> </menu>
androidmanifest.xml
<uses-sdk android:minsdkversion="7" android:targetsdkversion="18" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.sample.actionbarsample.mainactivity" android:label="@string/app_name" android:theme="@style/theme.appcompat" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application>
but instead of action bar
i options menu. shows when press menu button while running app.
what missing out tutorial, or there wrong in code?
edit
after doing beworker asked me got result, still missing search box.
nothing wrong either code or tutorial. it's configuration of emulator. need configure emulator in way, has software buttons. switch "device definition" tab , edit device uses "software" buttons in picture below.
alternatively can uncheck "keyboard / hardware keyboard present" property in adv configuration (see picture below) , restart emulator.
android 4.0 emulates old-style menu, if hardware menu button present. why menu actions go in there. if disable hardware menu in emulator, actions go action bar, expected.
please note, on phones hardware menu buttons samsung g2, g3 etc. menu popup still appear , cannot change in code. android decide better user experience on each , every device.
update
another note. if use compatibility library, make sure use xmlns:yourapp="http://schemas.android.com/apk/res-auto"
, yourapp:showasaction="always"
described below.
however, if app using support library compatibility on versions low android 2.1, showasaction attribute not available android: namespace. instead attribute provided support library , must define own xml namespace , use namespace attribute prefix.
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" > <!-- search, should appear action button --> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" yourapp:showasaction="always" /> ... </menu>
Comments
Post a Comment