I'm trying to remove the dropdown menu ActionButton Overflow, from Action Bar. In the case, it would be the one that has 3 points and default option "Settings".I would like to remove this item "Settings", so that, by clicking on the menu icon, he has to take action automatically. Any idea?
go to folder menu->main then remove item settings then you can add another item (for example)
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"
android:icon="#drawable/refresh">
</item>
and in your activity:
onOptionsItemSelected(MenuItem item) // write your switch case to handle the clicked item.
the line that allows the option to be directly on the action bar is:
android:showAsAction="ifRoom"
You cannot get rid of the overflow. You can ask for the action bar to consider making one (or more) of your action bar items be toolbar-style buttons in the action bar. To do that:
if you are using the native action bar, add android:showAsAction="ifRoom" to the <item> in your menu resource
if you are using the appcompat-v7 action bar backport, add app:showAsAction="ifRoom" to the <item> in your menu resource (assuming that you have the appcompat-v7 namespace set to be app)
However, the decision on whether or not to show the item as a toolbar button or to put it in the overflow is up to the action bar, not you. For example, if you ask for 8 items to all be toolbar buttons, while that may be possible on a 10" tablet in landscape, there will not be enough room for all of them on a phone-sized screen in portrait. Some of your items will be toolbar buttons; the rest will go into the overflow.
The Menu is inflated in the onCreateOptionsMenu method, and uses a menu XML in the menu folder (default) to retrieve items. If you would like to perform an action on the keycode menu, you can use the following
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
Toast.makeText(getApplicationContext(), "Menu Pressed", Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keycode, e);
}
if you have no use for menu items, you can also go ahead and get rid of the onCreateOptionsMenu and onOptionsItemSelected methods, as well as the menu XML, that is up to you though
You have to remove all methods that create the menu (onCreateOptionsMenu etc).
Then in the XML file for the Toolbar, you have to add in the toolbar a custom icon. For example:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<----YOUR CUSTOM ICON/BUTTON HERE---->
</android.support.v7.widget.Toolbar>
Then programmatically you can add an onClickListener on the custom icon/button and you can do whatever you want with it then.
Related
in my app on the top right of the action bar, I have the setting as three dots (as the usual setting). but when I click on it, it shows a white rectangle that has the word "setting" on it! But I don't want that!! I want just the three dots and when I click it I want it to take me to another activity right away(without showing the option setting on a rectangle).
this my code for the setting xml:
<item
android:id="#+id/action_settings"
android:orderInCategory="0"
android:title="#string/action_settings"
android:textColor="#color/colorBackground"
app:showAsAction="never" />
</menu>
The three dots you're talking about are usually there if you don't have enough room in your action bar and you want some of your menu items to show in a menu instead of directly displaying it to the action bar. The best thing for you is to create another item and set app:showAsAction to always and call a method using onOptionsItemSelected event:
<item
android:id="#+id/your_id"
android:title="your title"
app:showAsAction="always" />
Call it like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.your_id:
//code here...
}
return super.onOptionsItemSelected(item);
}
Currently your app has a default menu that has a settings item added. You can create your own menu options.
Create a new menu.xml file that contains 1 item whose image is the three dots.
In your class, inside the onOptionsItemSelected method, call the activity that you want to open.
Hope this helps :)
I need help figuring the right way to control the actionbar.
At the onCreate function i added:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar);
And added actionbar.xml to the layout folder with 3 items in it: TextView (app name) and 2 ImageViews.
It works, i mean i can see this new actionbar, but i'm pretty sure i'm doing it the wrong way, in tutorials im reading they're talking about adding those items to a menu.xml file which i couldn't find, and using onOptionsItemSelected function to set the behaviour.
As I'm a begginer, would appreciate an explanation of what it is that i'm doing, why it's wrong and how to do it correctly with the menu.xml
Ok, first you are right, Android have an out of the box solution to put "Action Button" and Title on the Action Bar
1.Title
When you create an Activity, it will add some code into your AndroidManifest.xml, here is the example:
<activity
android:name="AddAddressActivity"
android:label="#string/title_activity_add_address" >
</activity>
so you change your title here by changing android:label, the best practice is you need to put all your string asset under res/values/string.xml like this
<string name="title_activity_add_address">Add New Address</string>
Action Button
First add this override method to your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.address_list, menu);
return true;
}
Later, you create address_list.xml under res/menu folder, and inside it you can put the list of your Action Button there
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.vi8e.giant.AddressListActivity">
<item
android:id="#+id/action_add"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_action_new"
android:title="#string/action_save"/>
Android:title is to put your menu title, this title will show up when you long press the menu
Android:icon here is the image that you want to show for the menu, put the image under res/drawable
Edit: forgot to mention about how to trigger your menu
you can put onOptionsItemSelected method on your Activity, here is the example
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
//do something
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
note: Action Bar has a limited ammount of space, so if you have a lot of menu, it will be collapse into "three dots" icon on the top-right corner
I am trying to add a 'Spinner' in the OPTIONS MENU in my Android App that uses AppCompatActivity.
In the example I have prepared, I want the spinner to present a dropdown list of options such as 'All/Successful/Failed'.
Following instructions found elsewhere on StackOverflow, I am able to add the spinner to the action bar, but it looks really odd. I want the spinner to match the look of action bar items, which in my case means that the title on the action bar should be white, and when I click on the spinner, the pop up should have white background and black test just like the overflow options menu.
Please see the screenshot below for the appearance I am currently getting.
Image of collapsed and expanded spinner menu item
As you can see, the problem is that spinner selection shows up in black when the rest of the actionbar item are in white. Similarly, when the spinner is clicked, the items in the dropdown have dark background with black text, whereas the rest of the menu items (overflow menu for example), have light background with black text.
I have added this spinner by doing the following:
Step 1: Added the menu item in menu xml:
<item android:id="#+id/spinner"
app:showAsAction="always"
android:title="Filter"
app:actionViewClass="android.widget.Spinner" />
Step 2: In my Activity (in this this case Fragment) menu expansion code, added the following:
String [] adapterValues = new String[]{"All", "Successful", "Failed"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, adapterValues);
MenuItem item = menu.findItem(R.id.spinner);
Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
spinner.setAdapter(adapter);
Solution Tried:
So in my array adapter, instead of using the default spinner layout, I created and provided my owner layout as shown below:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.actionbar_spinner_filter, R.id.spinner_text, adapterValues);
And the corresponding layout file is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp"
android:background="#android:color/white"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_text"
/>
</LinearLayout>
This gave me the following results, and as you can see, these are quite weird.
Image of collapsed and expanded spinner menu
Summary of Question:
Is this the best way of adding an options menu to my AppCompatActivity for a 'dropdown menu' effect?
What is the best way I can use to make sure that the appearance of spinner menu matches the actionbar when it's collapsed, and when it's expanded, it matches the description of rest of the pop up items in the action bar (such as overflow menu)?
Thanks
In your menu xml file do something like:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app.polar="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/item0"
app.polar:showAsAction="always"
android:icon="#drawable/some_icon"
android:title="#string/some_title"/>
<item
android:id="#+id/item1"
app.polar:showAsAction="always"
android:icon="#drawable/some_icon"
android:title="#string/some_title">
<menu
android:id="#+id/submenu1">
<item
android:id="#+id/item11"
app.polar:showAsAction="never"
android:title="#string/some_submenu_title"/>
<item
android:id="#+id/item12"
app.polar:showAsAction="never"
android:title="#string/some_submenu_title"/>
<item
android:id="#+id/item13"
app.polar:showAsAction="never"
android:title="#string/some_submenu_title"/>
</menu>
</item>
</menu>
In your activity do something like:
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.yourmenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.item0:{
//do something
return true;
}
case R.id.item11:{
//do something
return true;
}
case R.id.item12:{
//do something
return true;
}
case R.id.item13:{
//do something
return true;
}
}
return super.onOptionsItemSelected(item);
}
if you do it like this, the style of your submenu will always be the same as the normal actionbar overflow menu.
I have a nearly full screen dialog fragment in my app, containing a toolbar on top. I want to style this toolbar like in the Material design guidelines:
Toolbar XML:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:layout_width="match_parent"
android:title="#string/new_folder"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:background="#color/primary_dark"/>
menu 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/action_done"
android:title="#string/action_save"
android:icon="#drawable/ic_done_white_48dp"
android:orderInCategory="100"
app:showAsAction="always|withText" />
</menu>
In my Dialog Fragment, code relevant to the toolbar:
private Toolbar.OnMenuItemClickListener mMenuItemListener = new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
dismiss();
return true;
case R.id.action_done:
dismiss();
return true;
}
return false;
}
...
mToolbar.setNavigationIcon(R.drawable.ic_close_white_48dp);
mToolbar.inflateMenu(R.menu.menu_folder);
mToolbar.setOnMenuItemClickListener(mMenuItemListener);
My toolbar looks like this:
I have four problems:
The 'Save' text is not displayed next to the checkmark, even though there is ample room for it
The X icon is too large (maybe use the 36dip version?)
The X icon is not clickable
The toolbar does not display the title ('New Folder')
What am I doing wrong?
Note that my toolbar is not set as the action bar! It's just a toolbar in a fragment.
The 'Save' text is not displayed next to the checkmark, even though
there is ample room for it
This is not possible. You can show only text or only icon.
The X icon is too large (maybe use the 36dip version?)
You should use 24dp version.
The toolbar does not display the title ('New Folder')
Because you are in Fragment so you need to set title explicitly into Toolbar and from you code i don't see source code that doing this. You need to do it programatically (via xml it seems it's broken) and i prefer to you use support library.
The X icon is not clickable
You need to use OnNavigationClickListener and not OnMenuClickListener, your X icon is navigation icon not menu icon.
I don't think that you should but texts in the menu side (right of the toolbar). Looking at the guidelines, you can see that is not a good practice, and all the examples show menus only with icons
From the guidelines, I think you should use 24dp (http://www.google.com/design/spec/style/icons.html#icons-system-icons in the section 'Clearance')
Use
toolbar.setNavigationOnClickListener();
you need to set the title like
toolbar.setTitle("'New Folder")
I followed this tutorial to create a menu
but my menu looks differently:
How can I create a menu with images?
This is my code:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/preferences"
android:icon="#drawable/preferences"
android:title="Preferences" />
<item android:id="#+id/help"
android:title="Help"
android:icon="#drawable/ic_action_search" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/*menu.add(Menu.NONE, PREF_ID, Menu.NONE, "Preferences")
.setIcon(R.drawable.preferences).setAlphabeticShortcut('e');
return (super.onCreateOptionsMenu(menu));*/
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_ygo_main, menu);
return true;
}
First of all I want to say : Say Goodbye to the Menu Button
Your code have no problem, and it should be showing the icons if the drawables are there in correct folder,Working fine on Android 2.2.
The Menu features says :
1. Context menus: Do not support item shortcuts and item icons.
2. Options menus: The icon menus do not support item check marks and only show the item's condensed title. The expanded menus (only available if six or more menu items are visible, reached via the 'More' item in the icon menu) do not show item icons, and item check marks are discouraged.
3. Sub menus: Do not support item icons, or nested sub menus.
No problem with your code, Problem may be with the API level you are using, but still want to suggest that don't use Menu anymore.
Android no longer requires a dedicated Menu button, some devices don’t have one, and you should migrate away from using it.
If you use some following attribute in manifest file then it's will be show your icon....
<activity android:name=".ui.CategoryActivity"
android:label="#string/app_name"
**android:theme="#android:style/Theme.NoTitleBar"**></activity>
It's work fine for me...:)
**must be enter.