After switching to toolbar there is a problem with menu icons. Although I set for a menu item android:showAsAction="always" it does not show the icon, I can only find it clicking on the popup icon.
This is myActivity
public class myActivity extends AppCompatActivity{
.........
public void onCreate(....){
.............
Toolbar toolbar = (Toolbar) findViewById(....);
setSupportActionBar(toolbar);
}
............
public boolean onCreateOptionsMenu(Menu menu{
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
.............
}
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/settings"
android:icon="#drawable/settings"
android:title="settings"
android:showAsAction="always"
/>
<item
android:id="#+id/help"
android:icon="#drawable/help"
android:title="help"
android:showAsAction="never"
/>
</menu>
Both settings and help icons are only in popup menu. So how to show settings icon on toolbar?
Replace android:showAsAction with app:showAsAction. You will also need to add xmlns:app="http://schemas.android.com/apk/res-auto" alongside your existing xmlns item in your root element.
With AppCompat there is a little change. If you are running lint it will complain about it. Type 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:id="#+id/someId"
android:title="#string/someText"
app:showAsAction="always"/>
</menu>
You need to declare the "app" namespace and reference it.
Related
I'm new to Android and am having a problem that seems to be pretty popular. None of the solutions are working for me so I can't figure out what's wrong.
I've got a Menu with 2 items in it (Search, Shop By), and I want Search to appear in the Action Bar. However, app:showAsAction="ifRoom" is not moving Search as it still appears in the overflow.
Here is the XML for the menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/res-auto">
<item android:id="#+id/shop_by"
android:icon="#drawable/ic_shop_by"
android:title="#string/shop_by_title"/>
<item android:id="#+id/search"
android:icon="#drawable/ic_search"
android:title="#string/search"
app:showAsAction="ifRoom"/>
</menu>
Here is where I override onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
What am I missing?
I tried app:showAsAction="ifRoom|withText" and got the same behavior.
I tried android:showAsAction="ifRoom"and got an error.
EDIT:
SOLVED!
The namespace for app should look like this:
xmlns:app="http://schemas.android.com/apk/res-auto"
You need to try adding orderInCategory attribute. Something like 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:id="#+id/shop_by"
android:icon="#drawable/ic_shop_by"
android:title="#string/shop_by_title"
android:orderInCategory="100"/>
<item android:id="#+id/search"
android:icon="#drawable/ic_search"
android:title="#string/search"
android:orderInCategory="99"
app:showAsAction="ifRoom"/>
</menu>
Use app:showAsAction="always" if you want to always show the menu item.
Note: the answer is updated from xmlns:app="http://schemas.android.com/res-auto" to xmlns:app="http://schemas.android.com/apk/res-auto"
kudos to #MikeM
If you are using
android.support.v7.app.Activity
the xml should be like yours.
And if you are using
android.app.Activity
please use:
android:showAsAction="ifRoom|withText"
instead of
app:showAsAction="ifRoom|withText"
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.
I am new in Android applications development, and I am trying to develop an application in UI I added a toolbar but In don't know why the three dots in the right side of the toolbar, it is necessary for me because I want to add a logout button there.
here is the toolbar layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="#000">
</android.support.v7.widget.Toolbar>
And here is where i add the toolbar inside the activity class:
toolbar= (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
toolbar.setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.com_facebook_button_background_color_focused));
and this is my menu.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=".HomeActivity">
<item android:id="#+id/action_settings"
android:title="Settings"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/icon"
/>
<item
android:id="#+id/action_search"
android:title="Search">
</item>
</menu>
And what is weird is that in the android studio preview the three dots are shown:
what I am doing wrong ?
Create Android Resource Directory of type menu in res folder and add xml file named: user_menu.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">
<item
android:id="#+id/logout_menu"
android:orderInCategory="100"
android:title="#string/action_logout"
app:showAsAction="never" />
</menu>
In your Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.user_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.logout_menu:
// Do whatever you want to do on logout click.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The three dots were hidden because they were black, and the toolbar background is black, so I had to add this white theme
android:theme="#style/ThemeOverlay.AppCompat.Dark"
to my toolbar layout.
In onCreateOptionmenu() one file will be inflating automatically. Remove that file and three dots will be removed.
Or
If this function is not included in your activity file then remove the file under res\menu folder.
I want to use android v7.toolbar in my project with fragments. So i did something like below in my main activity xml. Because i dont want to add all fragments xml to android.support.v7.widget.Toolbar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolBar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
</LinearLayout>
i am adding fragments with programmatically. And in some fragments i have to change right side of the toolbar. For example in some fragments i have to use just 1 right menu icon and another fragments 2. How can i achieve it?
Also is there a way to change icon resource which is in right side of the toolbar from fragments?
Thanks,
You can create menu.xml and put all your menu items in it. Set all the items visibility to false. This hides everything.
Then in your fragment's onCreate set setHasOptionsMenu(true), this will allow you to override onCreateOptionsMenu(Menu m, MenuInflater inflater).
In this method you can do menu.findItem(id.of.item).setVisible(true/false).
Examples:
menu.xml:
<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="com.sample.app.MainActivity">
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:orderInCategory="101"
app:showAsAction="ifRoom"
android:title="#string/action_refresh"
android:visible="false"/>
<item
android:id="#+id/action_edit_account"
android:icon="#drawable/ic_action_edit"
android:orderInCategory="102"
app:showAsAction="never"
android:title="#string/action_edit_account"
android:visible="false"/>
<item
android:id="#+id/action_enable_offline_token"
android:orderInCategory="105"
app:showAsAction="never"
android:title="#string/action_enable_offline_token"
android:visible="false"/>
<item
android:id="#+id/action_disable_offline_token"
android:orderInCategory="105"
app:showAsAction="never"
android:title="#string/action_disable_offline_token"
android:visible="false"/>
<item
android:id="#+id/action_save"
android:icon="#drawable/ic_action_save"
android:orderInCategory="106"
app:showAsAction="ifRoom"
android:title="#string/action_save"
android:visible="false"/>
</menu>
Fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
menu.findItem(R.id.action_refresh).setVisible(false);
menu.findItem(R.id.action_save).setVisible(true);
}
To change icons you can just get the menu and do a findItem and setIcon.
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>