In Android, I Have created an action bar and added few menus item to it. When I click menu items, I am performing some action.
On long click I find a empty toast message. My Question is how to disable the toast on long click?
Instead trying to disable the empty "toast like" view, you may add the "android:title" attribute to describe what's the action actually do.
Example:
<item android:id="#+id/action_websearch"
android:icon="#drawable/action_search"
android:title="#string/action_websearch"
android:showAsAction="ifRoom|withText" />
in menu_add_key layout refer to a custom layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tumblr="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menuAddKey"
android:actionLayout="#layout/item_addkey"
android:showAsAction="always"
android:title=""/>
</menu>
define your item_addkey layout as you want
and in java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_add_key, menu);
menu.findItem(R.id.menuAddKey).getActionView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showAddKeyDialog("");
}
});
return true;
}
Disable the long click on a menu item:
menu.findItem(R.id.menuAddKey).getActionView() return null
Related
well the problem is that my icon of my option add item on the action bar does not appear(it has space for show the icon but it does not). my option just appear on the dropdown menu (the 3 points XD) thanks you. here is my code xml then my code java:
<?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/menuAddItem"
android:title="Add Item"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="ifRoom"
/>
</menu>
java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
MenuInflater infladorMenu = new MenuInflater(this);
infladorMenu.inflate(R.menu.menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menuAddItem:
this.listaDatos.add("Dato Añadido");
this.adaptador.notifyDataSetChanged();
break;
}
return super.onOptionsItemSelected(item);
}
As you can see the icon does not appear but the actionbar has space
As you can see the icon does not appear but the actionbar has space, my option is just showed in the dropdown menu. thank you for your help.
Hello guys I m trying to set an Icon in a menuItem. I read the Android Developers Blog and it says:
public abstract MenuItem setIcon (int iconRes)
Change the icon associated with this item. This icon will not always be shown, so the title should be sufficient in describing this item. See Menu for the menu types that support icons.
This method will set the resource ID of the icon which will be used to lazily get the Drawable when this item is being shown.
Parameters :iconRes The new icon (as a resource ID) to be displayed.
Returns : This Item so additional setters can be called.
I should put as parameter an int. In particoulare the ID of my icon. But I can't figure out where I have to find this ID. I simply put the icon named "badIcon.ico" inside the drawable folder. Now should I proceed?
Thanks guys
this is so simple....
See this tutorial..here
Create menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/next"
android:icon="#drawable/ic_next"
android:title="#string/next" />
<item android:id="#+id/previous"
android:icon="#drawable/ic_previous"
android:title="#string/previous" />
<item android:id="#+id/list"
android:icon="#drawable/ic_list"
android:title="#string/list" />
</menu>
And now you will be able to set ICON on menu
Now in CreateOptionMenu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
And to get that menu..
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.next:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.next) + " menu option",
Toast.LENGTH_SHORT).show();
return true;
…
default:
return super.onOptionsItemSelected(item);
}
If you want to add icons to your menu item in static way you can write
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/yourId"
android:icon="#drawable/badIcon"
android:title="#string/yourTitle" />
</menu>
You can add icon programmatically like this
menu.add(0, MENU_TITLE, 0, "title").setIcon(R.drawable.badIcon);
I seen some of the applications like gmail and other apps in which where there's
an editText and textView while clicking on text long press i can see in the actionbar there's menu appear say cut, copy, select all ,share and lookup option appears.
This same behavior as a developer wanted to include in my application.
How can I achieve this?
Check this tutorial which will help you to create contextual actionbar
Reference : Tutorial : Contextual Action Bar (CAB) - Android
Code : GitHub : Contextual Action Bar (CAB)
Edit
In your EditText layout, add below property to show standard Android contextual menu for copy/paste.
android:textIsSelectable.
Check this tutorial as well.
If you want only copy you can set following tag in your xml of textview
android:textIsSelectable="true"
If you want search ,share options as means you have to create Contextual ActionBar as follows
add following in your activity
ActionMode mActionMode;
and you have to create an ActionMondeCallback interface
class ActionBarCallback implements ActionMode.Callback
{
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
int id = item.getItemId();
if(id == R.id.item_delete)
{
tv.setText("");
Toast.makeText(MainActivity.this,"option deleted",Toast.LENGTH_LONG);
}
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
}
where contextual_menu.xml is as follows with required icons
<?xml version="1.0" encoding="utf-8"?>
<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.example.letschat"
>
<item
android:id="#+id/item_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="ifRoom|withText"
android:title="Delete"
android:titleCondensed="Delete">
</item>
<item
android:id="#+id/item_delete"
android:icon="#android:drawable/ic_menu_delete"
app:showAsAction="ifRoom|withText"
android:title="Delete"
android:titleCondensed="Delete">
</item>
<item
android:id="#+id/item_share"
android:icon="#android:drawable/ic_menu_share"
app:showAsAction="ifRoom|withText"
android:title="Delete"
android:titleCondensed="Delete">
</item>
</menu>
Now Enable your Contextual ActionBar(CAB) As follows as for example here am are enabling on long click of a textview
yourtextView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mActionMode = MainActivity.this.startActionMode(new ActionBarCallback());
return true;
}
});
then you have to write your own action on click on each action event on CAB
For Details follow the link
I need to show the submenu below the bar, not on top of the bar itself.
Copying my actionbar xml below
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="#+id/action_pages"
android:orderInCategory="1"
android:showAsAction="withText|always"
android:icon="#drawable/ic_action_pages"
android:title="">
<menu>
<item android:id="#+id/item1" android:title="Placeholder"></item>
</menu>
</item>
</menu>
In the activity (App also has a navigation drawer)
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
Simply.
<style name="AppTheme" parent="AppBaseTheme">
<item name="actionOverflowMenuStyle">#style/OverflowMenu</item>
</style>
<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<!-- Required for pre-Lollipop. -->
<item name="overlapAnchor">false</item>
<!-- Required for Lollipop. -->
<item name="android:overlapAnchor">false</item>
</style>
Preamble
As usual, I faced a strange problem while developing an Android app, tried to find a solution and landed to this question. As it was in many cases before, there is no an answer. So I was compelled to solve the problem from scratch and now posting the answer with my workaround.
Input
I have an Android app with action bar and some menu items, which have to be extended with dropdown submenu. First attempt was to implement it as suggested by Android documentation. So I added new menu item menu_sort into existing action bar menu and sub-menu container into it:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/id1" android:icon="#drawable/ic_1"
android:title="#string/id1" android:showAsAction="withText|always"/>
...
<item
android:id="#+id/menu_sort"
android:icon="#drawable/ic_menu_sort_selector"
android:title="▾"
android:titleCondensed="▾"
android:showAsAction="withText|always">
<menu>
<item
android:id="#+id/menu_sort_by_name"
android:showAsAction="never"
android:checkable="true"
android:checked="true"
android:title="#string/sort_by_name"/>
<item
android:id="#+id/menu_sort_by_priority"
android:showAsAction="never"
android:checkable="true"
android:checked="false"
android:title="#string/sort_by_priority"/>
<item
android:id="#+id/menu_sort_by_memory"
android:showAsAction="never"
android:checkable="true"
android:checked="false"
android:title="#string/sort_by_memory"/>
</menu>
</item>
</menu>
Result
The effect was exactly as described in the question: the submenu is displayed on top of the action bar. Here is the screenshot taken on Android 5.1.1:
I played with many options and code snippets - nothing helped. Finally I came to the following
Solution
First, move all the submenu into a separate menu layout, say, menu/sorting.xml, and remove it from menu_sort item of the main menu (shown above).
Second, modify or create onPrepareOptionsMenu event handler with the following code:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
// as solution utilizes PopupMenu,
// take care about older Android versions if necessry
// if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
// here goes most crazy part: we use menu id
// to retrieve corresponding view, automatically created by OS;
// imho, this is a hack, and menu item should have getView() method or similar;
View menuItemView = findViewById(R.id.menu_sort);
// by the way, menuItemView could probably be null under some circumstances
// create a popup anchored to the view (menu item)
final PopupMenu popupMenu = new PopupMenu(this, menuItemView);
// API 14
// popupMenu.inflate(R.menu.sorting);
// API 11 (HONEYCOMB)
popupMenu.getMenuInflater().inflate(R.menu.sorting, popupMenu.getMenu());
// process popup clicks as appropriate
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch(item.getItemId())
{
// ... place some code
}
return true;
}
});
// bind the popup to the item menu
menu.findItem(R.id.menu_sort).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
popupMenu.show();
return true;
}
});
return super.onPrepareOptionsMenu(menu);
}
Here is the result:
Now the dropdown is displayed as expected from very beginning.
#Stan's solution doesn't work for me, so here's my way to implement sub-menu on top of ActionBar (but below the main-menu of course):
I've created 2 xml files: menu_main.xml and menu_more.xml located in res/menu directory
The first one 'menu_main.xml' contain the menu:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- our addMenu doesn't have sub-items-->
<item
android:id="#+id/action_add"
android:icon="#drawable/ic_note_add_white_24dp"
android:title="#string/action_add"
app:showAsAction="ifRoom"/>
<!-- our moreMenu which show drop-down menu when clicked-->
<item
android:id="#+id/action_more"
android:icon="#drawable/ic_more_vert_white_24dp"
android:title="#string/action_more" <!--in text: "more"-->
app:showAsAction="always"/>
</menu>
And the second one 'menu_more.xml' contain the drop-down menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- This menu will be hidden by default-->
<!-- But will be visible when moreMenu with '#+id/action_more' is clicked-->
<item
android:id="#+id/action_settings"
app:showAsAction="ifRoom|withText"
android:title="#string/action_settings" <!-- In text: "Settings"-->
android:visible="true"/>
</menu>
Here is what previous menus look like:
result-after-add-2-xmls (i have not enough 10 reputation to display image)
In the activity, i've overridden this method:
public boolean onPrepareOptionsMenu(Menu menu)
In the previous method, i get reference to the main menuItem (in this case is menu with #+id/action_more located in menu_main.xml file), then set setOnMenuItemClickListener on it and finally, declare and set up a PopupMenu instance to manage and display sub-menu items:
// show popup menu when menuMore clicked
menu.findItem(R.id.action_more).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
// get reference to menuMore item
View menuMore = findViewById(item.getItemId());
// create a popup anchored to the view (menuMore)
// notes: if declare and set up PopupMenu Outside of this onMenuItemClick()
// then it'll not work!
// Because: the view you put into PopupMenu() could be null
final PopupMenu popupMenu = new PopupMenu(getApplicationContext(), menuMore);
// inflate 'menu_more.xml' layout file
// which contain all sub-items of menu
popupMenu.getMenuInflater().inflate(R.menu.menu_more, popupMenu.getMenu());
// process popup clicks on sub-items
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch(item.getItemId()){
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "showing SettingsActivity..",
Toast.LENGTH_SHORT).show();
break;
// more items go here
}
return true;
}
});
popupMenu.show();
return true;
}
});
return super.onPrepareOptionsMenu(menu);
And here is final result:
final-look-drop-down-menu
I'm coming back to my main activity from a fragment and for some logic I have to change the appearence of an icon on the action bar menu.
This is the menu on the action bar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="it.gn.sfa.Main">
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:title="Search" />
<item
android:id="#+id/action_filter"
android:icon="#drawable/ic_action_filter_empty"
android:showAsAction="ifRoom"
android:title="Filter" />
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
android:showAsAction="ifRoom"
android:title="New" />
</menu>
I have to change the sencond item (the one with id = action_filter).
I've tried different solutions, found on different post. The most rated is
mOptionsMenu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_action_filter));
but seems not to work.
On the other side getActionBar().setIcon(getResources().getDrawable(R.drawable.ic_action_filter)); changes the logo, and I don't want so.
How can i change only the second item on menu?
try this one
mOptionsMenu.findItem(R.id.action_filter).setIcon(R.drawable.ic_action_filter);
Assuming you have it all set up for mOptionsMenu in
private Menu mOptionsMenu;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
// inflating your menu here
mOptionsMenu = menu;
return super.onCreateOptionsMenu(menu);
}
Hope it helps :)
I hope it will be help for you
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>" + "Messages" + "</font>"));
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.messagebar_color)));
getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_arrow_black);
You have to modify your onCreateOptionsMenu(Menu menu)
I changed the color of my search bar programmatically. I am posting the code here. Hope it helps.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
menu.getItem(0).setIcon(getTintedDrawable(R.drawable.search, R.color.blue));
return super.onCreateOptionsMenu(menu);
}
Where getTintedDrawable() is a function that i created which returns a drawable. So all you need to do is replace getTintedDrawable(R.drawable.search, R.color.blue) by your drawable.
NOTE: I have used menu.getItem(0) my code since I had only 1 item defined in menu/items.xml. If you have multiple try different values (from 0 to one less than number of menu items). My guess would be that its the number at which the item is defined but I'm not too sure.
I manage to rotate/change the icon this way:
MenuItem item = getToolbar().getMenu().findItem(Menu.FIRST);
<prepare the image view from drawable here>
item.setActionView(imageView);
Seems to work OK.
You could also simply use the item.setIcon() instead.