Action Item acts as drop down menu - android

Is it possible to add Action Item in ActionBar, which show drop down menu on click?
paint example:
P.S. ActionBar already contains navigation drawer toggle button, title and overflow menu.
Where do I initialize that button, in which xml?
How do I set such drop down operations to Action Item?
How to set the content of such drop down menu?
And how to get access to specific item click action?
Some working code examples would be great!
Thanks in advance. Appreciate any help.

So I found solution by myself.
You need to inflatean Action Item in onCreateOptionsMenu(Menu menu):
getMenuInflater().inflate(R.menu.global_filters, menu);
global_filters.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=".NavigationActivity">
<item android:id="#+id/action_filters"
android:title="Фильтры"
android:icon="#drawable/ic_filter_white"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
</menu>
...which is that downside arrow:
Then create a PopupMenu. I did it in onOptionsItemSelected:
View menuItemView = findViewById(R.id.action_filters); // SAME ID AS MENU ID
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.popup_filters_user);
popupMenu.show();
and here you set .xml file with items of drop down menu in file popup_filters_user.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/filter_bought_user"
android:title="Купленые"/>
<item
android:id="#+id/filter_price_user"
android:title="Цена"/>
<item
android:id="#+id/filter_author_user"
android:title="Автор"/>
</menu>
and hooray! Here's the result:

If your menu is in a Fragment then you might use the following to get the View
View menuItemView = MainActivity.getInstance().getWindow().findViewById(R.id.action_filters);

Related

Android Toolbar Change Menu action items dynamically

I have a Toolbar (android.support.v7.widget.Toolbar) and one Action Menu Item on it. I want to programmatically populate submenu for the menu item.
I could not find a way to do it until I set the toolbar as ActionBar by setSupportActionBar(toolbar). In this case I can modify menu items in onPrepareOptionsMenu(). Is there a way to change menu items programmatically without setSupportActionBar(toolbar)?
The only action item is inflated by toolbar.inflateMenu(R.menu.menu_main)
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" tools:context=".MainActivity">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="1"
app:showAsAction="always">
</item>
</menu>
You can get the menu of the Toolbar with the Toolbar's getMenu() method. This method returns a Menu object to which you can add or modify menu items, or submenus.
Here you can check out the documentation of the getMenu() method.

about android toolbar, how to hide defualt setting menu in toolbar

about android Toolbar, how to hide default setting menu in toolbar
in the this activity i hava set a single icon menu,like this –
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title=""
android:id="#+id/menu_add"
android:icon="#drawable/actionbar_add_icon"
android:showAsAction="ifRoom">
</item>
</menu>
it is menu/main_home.xml
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
return super.onCreateOptionsMenu(menu);
}
but it show a three dots menu. i want know why
and how to show sub menu icon image ,it is normal in the Actionbar, but hide in the Toolbar
If you want to empty the contents of the menu, go to the res/menu folder and delete the item tags from the menu_main.xml file. You will also need to delete references to the items in onOptionsItemSelected(MenuItem item). The menu dots will disappear if there are no items.
If you want to completely remove the three dots menu, then go to the onCreateOptions(...) method in your code and delete it, or simply remove the getMenuInflater().inflate(...); portion of it. You can safely remove the onOptionsItemSelected(MenuItem item) method as well because it will have no purpose.
The simplest way to make the menu disappear is to have the onCreateOptions(...) method return false.
this question is solved use app:showAsAction ="ifRoom" not android:showAsAction ="ifRoom"
<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:title=""
android:id="#+id/menu_button"
app:actionLayout="#layout/menu_single_button"
app:showAsAction = "always"
/></menu>

Action bar menu item is duplicating in android studio

Menu item showing on both never and always states
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:apptivo="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_create"
android:icon="#drawable/ic_action_new_white"
android:orderInCategory="2"
android:title="#string/create_string"
apptivo:showAsAction="always" />
</menu>
check if your menu is being created more than once, if have Activity or fragment that creates the menu, and class that it extend is creating the same menu you would have this. more over if you are creating the menu in the fragment and the fragment is being add more than once you would see this

How to show overflow menu in action bar

I want to display an overflow menu in the action bar , I tried to use a menu in my activity but I'm only getting a popup menu when I click on the menu button on my phone. Is there any way to force that overflow menu for phone with physical buttons ?
Here is my menu file :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/phone"
android:title="phone"
android:icon="#drawable/phone"
android:showAsAction="ifRoom"/>
<item
android:id="#+id/computer"
android:title="computer"
android:icon="#drawable/computer"
android:showAsAction="ifRoom"/>
<item
android:id="#+id/gamepad"
android:title="gamepad"
android:icon="#drawable/gamepad"
android:showAsAction="ifRoom"/>
</menu>
You need to go into the menu xml file and make sure it has
android:showAsAction="ifRoom"
and for the icon you use
android:icon="#drawable/yourIcon"
Edit: To make sure you are using the right menu xml, check your activity class that is being run and look for the
onCreateOptionsMenu(Menu menu)
Once you have found that check that the line
getMenuInflater().inflate(R.menu.{0}, menu);
matches your menu xml file, where {0} = your xml file name

how to provide buttons in action bar in android?

I want to provide a image button in place of action bar and on clicking that I want to display list of options and again on clicking any particular option I want to pass Intent.
How can I achieve this?Does anybody know
If so help me out.
Thank you...
In order to display the imagebutton in Actionbar. First add an item in main.xml.Which is Under MENU Folder.
for ex:
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/stopwatch"
android:orderInCategory="50"
android:icon="#drawable/ic_action_alarms"
android:showAsAction="always|withText"
android:title="Stopwatch"/>
</menu>
then add the following code in your mainActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case (R.id.stopwatch):
Toast.makeText(getApplicationContext(), "Stopwatch", Toast.LENGTH_SHORT).show();
Intent i=new Intent(getApplicationContext(), Stopwatch.class);
startActivity(i);
break;
return super.onOptionsItemSelected(item);
}
and Don't forget to create another java class as Stopwatch.java
Go through the following link it will help you
https://developer.android.com/training/basics/actionbar/adding-buttons.html
You can set a custom view on your action bar, here is the link
How to display custom view in ActionBar?
It can be done in two ways
Options 1: Create an layout XML with your image button, and set that layout to action bar.
For example: In you activity copy the following lines.
ActionBar actionBar = getSupportActionBar();
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.search, null);
actionBar.setCustomView(v);
Now display a PopUp menu when the user clicks a ImageButton.
Option 2:
Instead of setting the view for the ActionBar , you can use sub-Menu's in the action bar. For this you need to create sub-Menu.
for example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/file"
android:icon="#drawable/ic_new_game"
android:title="#string/file" >
<!-- "file" submenu -->
<menu>
<item android:id="#+id/create_new"
android:title="#string/create_new" />
<item android:id="#+id/open"
android:title="#string/open" />
</menu>
</item>
</menu>
Set an icon for the main `MenuItem` so it looks like an `ImageButton`, when the user click the first MenuItem it displays the its sub-`Menu`

Categories

Resources