Reputation: 71
I was following this tutorial on Android when i came across a problem. As mentioned in topic my search icon isn't showing up on the action bar. I had problem with that step. http://developer.android.com/training/basics/actionbar/adding-buttons.html I think i have done everything the same but still nothing shows up. I've seen other people having similar problem but their answers didn't work for me.
The worst thing is unlike others people problems related to search icon not showing up, mine isn't even showing up on overflow while theirs is. Maybe it can guide someone to what's my issue.
main_activity_actions.xml code below
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never"/>
</menu>
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never"/>
</menu>
activity.main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.pawel.androidapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here.
switch(item.getItemId())
{
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openSearch() {
Toast.makeText(this, "Search pressed", Toast.LENGTH_SHORT).show();
}
private void openSettings() {
startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
}
public void sendMessage(View view) {
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);
}
}
androidapp manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pawel.androidapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" >
<activity
android:name=".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=".DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.pawel.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.pawel.androidapp.MainActivity" />
</activity>
</application>
</manifest>
Upvotes: 2
Views: 4945
Reputation: 11
I had the ditto problem and i did not change anything in the code as others suggest above. I had downloaded the Action Pack icons from Android URL "http://developer.android.com/design/downloads/index.html#action-bar-icon-pack" and this has two folders named "Core Icons" and "Action Bar Icons". I used the icon ic_action_search from within the "Core Icons" folder and that caused the problem. I replaced the icon at "drawable" folder with the one from "Action Bar Icons/Holo Dark" and it was working the way intended. That was a moment of relief for a android developer. :)
Upvotes: 1
Reputation: 11
I have same problem, I got the answer after exchanging below 3 "android" to "app":
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="ifRoom"
android:orderInCategory="100" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="always"
android:orderInCategory="101" />
</menu>
Upvotes: 1
Reputation: 61
I had the same problem. You're using minSdkVersion="11" which is when the ActionBar APIs were defined. (http://developer.android.com/guide/topics/ui/actionbar.html)
The line I had wrong was: app:showAsAction="ifRoom"
I had looked at the development tutorials which are illustrating use of the Support Library and my line looked the same as yours. I changed it to this: android:showAsAction="ifRoom"
and it worked. Use the "android" namespace instead of the "app" namespace. (https://developer.android.com/training/basics/actionbar/adding-buttons.html)
Upvotes: 1
Reputation: 87
I tested your code in my project and I found that the issue was in the Manifest file.
android:theme="@style/Theme.AppCompat" >
Change it to:
android:theme="@style/Theme.AppCompat.Light" >
Upvotes: 0
Reputation: 18977
you have missed:
app:actionViewClass="android.support.v7.widget.SearchView"
from your item.
Upvotes: 0
Reputation: 1040
It's more than likely down to
app:showAsAction="ifRoom"
You're basically saying only show the icon if there is room available to show it. So try :
app:showAsAction="always"
See http://developer.android.com/guide/topics/resources/menu-resource.html for more.
Edit :
Sorry, I've just noticed that you're using the namespace http://schemas.android.com/apk/res-auto for this attribute.
Does it work if you change it to :
android:showAsAction="always"
Upvotes: 0