This is my first time making an Android Actionbar. I just want to add a search image in the bar. But for some reason the image button is not appearing. Here is the Actionbar XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
I put showAsAction as true, so the image should appear.
MainActivtiy.java
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Here is the screen:
Clearly the screen large enough.
and I have the images in the folders. I'm not sure why its not appearing?
the following code worked for me
this was my menu file res/menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" > <!-- this line matters -->
<item
android:id="#+id/searchItemMenu"
android:icon="#drawable/action_search"
yourapp:showAsAction="ifRoom|withText" <!-- use it here -->
android:title="#string/new_search"/>
</menu>
and in java file
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Hope this works for you.
Related
menu_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">
<item android:id="#+id/search"
android:orderInCategory="100"
android:title="search"
android:icon="#drawable/ic_search"
app:showAsAction="always"/>
<item
android:id="#+id/refresh"
android:icon="#drawable/ic_refresh_black_24dp"
android:orderInCategory="100"
app:showAsAction="ifRoom|collapseActionView"
android:title="refresh" />
</menu>
MainActivity.java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
menu.findItem(R.id.search);
return super.onCreateOptionsMenu(menu);
}
First time If I am open my app below one is my screen
First time my screen was looks like this
Next time onward my screen looks like this
Next time on-wards My screen was looking like this
Here is menu xml file.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:icon="#mipmap/ic_shopping_cart_black_36dp"
android:orderInCategory="1"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_share"
android:title="#string/action_share"
android:orderInCategory="2"
app:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
Here is my onCreateOptionsMenu(Menu menu) method
#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_menu, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
setIntent("This is example text");
return super.onCreateOptionsMenu(menu);
}
I already use xmlns:app="http://schemas.android.com/apk/res-auto" and app:showAsAction but it still doesn't show the icon for some reason.
My project has an appcompat.v7 dependency.
can anyone help me please?
You can dynamically add buttons etc. to the default action bar.
in onCreateOptionsMenu add the following code in order to add an item.
MenuItem aBarItem= menu.add(0,1,0,"Save");
aBarItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
aBarItem.setIcon(//Drawable icon//);
then just add onclicklistener to this menu item to make it work.
I hope this helped
Im trying to remove one of my action bar items(the settings menu) and add another one but its not adding the right icon/action to the action bar.
my layout file is as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/settings"
android:showAsAction="always" />
</menu>
and im initializing it like this
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDefaultDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(true);
actionBar.setTitle(mTitle);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
it shows the three android dots instead of my own icon(it also puts it in the dropdown
i want to change the dots in the red circle
what am i doing wrong
your layout file should be like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/settings"
app:showAsAction="always" />
very easy ,Replace your layout code:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/settings"
android:showAsAction="never" />
</menu>
Code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.start_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
start_activity.actions.xml:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"
/>
<item
android:id="#+id/action_collection"
android:title="#string/action_collection"
android:icon="#drawable/ic_action_collection"
android:showAsAction="always"
/>
My menu appears only after clicking settings button. It's not visibile on launch. How to change it ?
If You have extend the activity to android.app.Activity; then the menu xml you have should work.
If your using support library android.support.v7.app.ActionBarActivity then to display the action menus in action bar, change your menu xml like below..
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
custom:showAsAction="always"/>
<item
android:id="#+id/action_collection"
android:title="#string/action_collection"
android:icon="#drawable/ic_action_collection"
custom:showAsAction="always"/>
</menu>
I don't know how to create the bottom menu with sherlock on the button with the ActionBarSherlock, while my top menu is ok when created with sherlock.
I want the light style for my menu but I don't know how to create it with sherlock.
My code:
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.submenu, menu);
return super.onCreateOptionsMenu(menu);
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item"
android:showAsAction="always"
android:title="settings"
android:icon="#drawable/advancedsettings" >
<menu>
<item
android:id="#+id/removerss"
android:showAsAction="always"
android:icon="#drawable/remove"
android:title="Remove Rss" />
<item
android:id="#+id/addrss"
android:showAsAction="always"
android:title="Add Rss"
android:icon="#drawable/add" />
</menu>
</item>
</menu>
But this menu isn't connected with the style sherlock. So how can I create this menu with sherlock. Please if you can give some code to do it simple.