Android: Action button icons always appear in overlay - android

Action items always appear in overlay. Here is the code:
XML:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:icon="#drawable/ic_event_black_18dp"
android:orderInCategory="1"
app:showAsAction="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
and in the main activity:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
Shouldn't the first item appear on the action bar because of "app:showAsAction="always" ?

Use this inside your item ,to display in Action bar
app:showAsAction="always"
When you dont want to display item in Action bar
app:showAsAction="never"
Try this piece of code , instead of your creating Option Menu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

If it is for fragments
custom namespace will prevent showAsAction from showing. so
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:icon="#drawable/ic_event_black_18dp"
android:orderInCategory="1"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_add" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>

Related

How do i make sure the Menu Item is always on the topbar?

the 2 menu item connect and disconnect coded such that only 1 of them shows at a time.
I want to make it on the top bar, and not under the ... button.
following is my menu xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_refresh"
android:checkable="false"
android:orderInCategory="1"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_connect"
android:icon="#android:color/holo_blue_bright"
android:orderInCategory="100"
android:title="#string/menu_connect"
app:showAsAction="ifRoom|withText" />
<item
android:id="#+id/menu_disconnect"
android:orderInCategory="101"
android:title="#string/menu_disconnect"
app:showAsAction="ifRoom|withText" />
You can try:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_refresh"
android:checkable="false"
android:orderInCategory="1"
app:showAsAction="always" />
<item
android:id="#+id/menu_connect"
android:icon="#android:color/holo_blue_bright"
android:orderInCategory="100"
android:title="#string/menu_connect"
app:showAsAction="always" />
<item
android:id="#+id/menu_disconnect"
android:orderInCategory="101"
android:title="#string/menu_disconnect"
app:showAsAction="always" />
I hope it will help your problem!
If you still have same problem even you set app:showAsAction="always", you should check onCreateOptionsMenu. Please try this if you're creating menu differently, it will help you.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
Have you tried setting...
app:showAsAction="always"
... in the item you want to be shown in the top bar (app bar) always?
EDIT:
or...
app:showAsAction="always|withText"
if yout want to show the title too.

How to display menu item in action bar in android

menu item is not appearing in action bar. I am trying this code on API 19, kitkat 4.4
using this XML code.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="Sign out"
android:id="#+id/iSignout"
android:icon="#drawable/signout"
android:orderInCategory="100"
android:showAsAction="always"/>
<item
android:title="My Profile"
android:id="#+id/iEditProfile"/>
<item
android:title="Edit Profile"
android:id="#+id/iMyProfile"/>
</menu>
and
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_menu, menu);
return true;
}
Try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Sign out"
android:id="#+id/iSignout"
android:icon="#drawable/signout"
android:orderInCategory="100"
app:showAsAction="always"/>
<item
android:title="My Profile"
android:id="#+id/iEditProfile"/>
<item
android:title="Edit Profile"
android:id="#+id/iMyProfile"/>
</menu>
Replace android:showAsAction="always" with app:showAsAction="always"
Try
android:showAsAction="ifRoom"
How to display settings menu on click of settings icon? settings-menu-on-click-of-settings-icon/41716117#41716117

action item's icon is not showing

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

Android Action bar not showing Icons

How can i display Icons in Actions bar, here is my code,
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search_icon"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:orderInCategory="0"
android:title="Search" >
</item>
</menu>
and
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater mif = getMenuInflater();
mif.inflate(R.menu.custom_action_bar,menu);
return super.onCreateOptionsMenu(menu);
}
Thanks for Help !
If using app compat use app:showAsAction
<item android:id="#+id/search_icon"
android:icon="#drawable/ic_action_search"
app:showAsAction="always"
android:orderInCategory="100"
android:title="Search" >
</item>
In Manifest.xml File
<activity android:name="your activity name"
android:theme="#style/Theme.Holo.Light"></activity>

Actionbar action button shown as drop down item instead of button

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>

Categories

Resources