java - Search icon not appear in action bar -
i have found same problem me 1. action buttons doesn't show on action bar?
the problem icon search never appear eventhough text there
however when follow resolution there , problem still never solve.
can please view codes? patient me today 2nd day in learning android development.
main_activity_actions.xml
<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="always" /> <!-- settings, should in overflow --> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:showasaction="never" /> </menu>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">my first app</string> <string name="edit_message">enter message</string> <string name="button_send">send</string> <string name="action_settings">settings</string> <string name="title_activity_main">mainactivity</string> <string name="title_activity_display_message">my message</string> <string name="hello_world">hello world!</string> <string name="action_search">-search-</string> </resources>
mainactivity.java
package com.example.myfirstapp; import android.support.v7.app.actionbaractivity; import android.support.v4.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.content.intent; import android.widget.edittext; import android.widget.toast; public class mainactivity extends actionbaractivity { public final static string extra_message = "com.example.myfirstapp.message"; /** called when user clicks send button */ public void sendmessage(view view) { // in response button intent intent = new intent(this, displaymessageactivity.class); edittext edittext = (edittext) findviewbyid(r.id.edit_message); string message = edittext.gettext().tostring(); intent.putextra(extra_message, message); startactivity(intent); toast.maketext(this,"clicked", toast.length_long).show(); } @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. //getmenuinflater().inflate(r.menu.main, menu); //return true; //} @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().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); } } //@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_settings) { //return true; //} //return super.onoptionsitemselected(item); //} private void opensettings() { // todo auto-generated method stub } private void opensearch() { // todo auto-generated method stub } /** * 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); return rootview; } } }
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myfirstapp" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="11" android:targetsdkversion="19" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.example.myfirstapp.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="com.example.myfirstapp.displaymessageactivity" android:label="@string/title_activity_display_message" android:parentactivityname="com.example.myfirstapp.mainactivity" > <meta-data android:name="android.support.parent_activity" android:value="com.example.myfirstapp.mainactivity" /> </activity> </application> </manifest>
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.myfirstapp.mainactivity" > <item android:id="@+id/action_settings" android:orderincategory="100" android:title="@string/action_settings" app:showasaction="never"/> <item android:id="@+id/action_search" android:title="@string/action_search" android:icon="@drawable/ic_action_search" android:showasaction="always" android:actionviewclass="android.widget.searchview"/> </menu>
thank help. please not judge me did not research because have spent whole 1 day,searching here , there make right. reason still cannot right because dumb...:-)
you need change menu xml search should this, since using actionbaractivity support library.
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:appname="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_search" android:icon="@drawable/abc_ic_search_api_holo_light" android:title="@string/action_search_title" appname:showasaction="collapseactionview|always" appname:actionviewclass="android.support.v7.widget.searchview" /> </menu>
the showasaction
attribute above uses custom namespace defined in tag. necessary when using xml
attributes defined support library, because these attributes not exist in android framework on older devices. must use own namespace prefix attributes defined support library.
from link http://developer.android.com/guide/topics/ui/actionbar.html
Comments
Post a Comment