18

Is some possible way how to hide the toast after long-press on the ActionBar item? I didn't setup a title for the item but it is still there - empty toast.

<item
    android:id="@+id/ab_main_menu_dots"
    android:icon="@drawable/action_icons_dots"
    android:showAsAction="always">
    <menu>
        <item
            android:id="@+id/ab_main_menu_my_profile"
            android:showAsAction="never"
            android:title="@string/ab_my_profile">
        </item>
        <item
            android:id="@+id/ab_main_menu_settings"
            android:showAsAction="never"
            android:title="@string/menu_settings">
        </item>
        <item
            android:id="@+id/ab_main_menu_help"
            android:showAsAction="never"
            android:title="@string/tv_help_login">
        </item>
        <item
            android:id="@+id/ab_main_menu_about_us"
            android:showAsAction="never"
            android:title="@string/ab_about_us">
        </item>
        <item
            android:id="@+id/ab_main_menu_logout"
            android:showAsAction="never"
            android:title="@string/bt_logout_main">
        </item>
    </menu>
</item>

AdaMoOo
  • 427
  • 2
  • 5
  • 12
  • 7
    He's talking about the toast that pops up automatically when you long-press an item on the ActionBar. This is something the ActionBar does, it's not part of his code. – Karakuri Jul 01 '13 at 15:49
  • But I mean Toast when you long-press the ActionBar item. I know that you can cancel ordinary Toast, this is not my problem. – AdaMoOo Jul 01 '13 at 15:49
  • It looks like you are trying to mimic the overflow menu. Why not just use the actual overflow menu? – Karakuri Jul 01 '13 at 15:50
  • It is because I need "three dots" picture for all phones (overflow menu eveytime on action bar). In the case that you have hard context button the "three dots" picture isn't there. – AdaMoOo Jul 01 '13 at 15:55
  • If you are referring to older devices, two things worth mentioning: 1) ActionBarSherlock library makes ActionBar accessible on older devices, and you will still get your 3 dots menu. 2) If the user has an older device and they are accustomed to using the hardware menu key, let them. – Karakuri Jul 01 '13 at 15:59
  • Yes, I am using ActionBarSherlock but the "three dots" item has to be like this - it is in my project description from customer. But still there is a problem with empty toast after long-press and it seems that there is no solution :/ – AdaMoOo Jul 02 '13 at 07:02
  • 1
    This is a feature of the native action bar. It cannot be removed. – coder Jul 17 '13 at 10:58
  • 4
    @AdaMoOo don't forgot guidelines. If you do this it will violate the action bar design guidelines – Dinesh T A Jul 18 '13 at 07:00
  • 1
    The better way to solve is: not solve, give title for items and let Android deal with the items, they can be displayed as a overflow in some devices and a title label does help the user to do things. – Marcos Vasconcelos Jul 18 '13 at 23:06
  • have a look at [this](https://stackoverflow.com/questions/11274174/the-best-way-to-create-drop-down-menu-in-android-2-x-like-in-ics) question, I am pretty sure you want to achieve that. – Rupesh Jul 23 '13 at 22:45
  • This is like welding your car doors shut: you welded the doors shut but ruined the usability of the car in doing so (devaluing it a lot). Just add the text, let it show, it is there for a reason (hinting as to what the purpose of the action is). – straya Oct 22 '19 at 07:30

9 Answers9

6

The only way to hide the toast is when you set the ActionBar menu item to be displayed with text. android:showAsAction="withText". Otherwise the toast adds clarification of what each action item represents even if there is no title set for menu item.

hardartcore
  • 16,886
  • 12
  • 75
  • 101
5

Probably the cleanest way to go about this is to assign a custom action view to your menu item that mimics the look of a regular one.

Since you mentioned you're using ActionBarSherlock, here's a simple example.

Imagine the following menu.xml, which gets inflated in an Activity.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/ab_main_menu_dots"
        android:actionLayout="@layout/ab_main_menu_dots_layout"
        android:showAsAction="always"/>
</menu>

You can define ab_main_menu_dots_layout.xml to mimic the overflow button like this:

<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Widget.Sherlock.ActionButton.Overflow"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

The result is a menu item that looks like an overflow button and does not display a Toast message when you long-press it, regardless of whether the native ActionBar is used or ABS. Up to you to take it from here. You want to reconsider and abide by the guidelines instead.

MH.
  • 45,303
  • 10
  • 103
  • 116
  • 1
    Hey @MH... My requirement is just opposite it. I want toast on my custom action layout item. How to achieve this. – Mahendra Chhimwal Dec 23 '15 at 06:47
  • @MahendraChhimwal: I guess you could just show a toast in the action item's on(long)click. Toasts can be fully customised and displayed anywhere on the screen, so you should be able to just replicate the default toast behaviour (hint: just take a peak in the framework's source code). – MH. Dec 23 '15 at 08:29
  • :Thanks for suggestion, and yeah(hint: just take a peak in the framework's source code) is a great hint (& tip as well) for newbie developers like me.Thanks again. – Mahendra Chhimwal Dec 23 '15 at 11:50
5

You can modify onLongClickin ActionMenuItemView Class to stop Toasting on long click.
but be careful, It's only working on devices with API less than 11, because sherlockactionbar library checking your device API level by Build.VERSION.SDK_INT and if you have newer device it just use default system actionbar which you're not modifying.

Sadegh
  • 2,669
  • 1
  • 23
  • 26
5

In onCreateOptionsMenu schedule a task to disable long click on desired menu item. Here is the sample

    @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    inflater.inflate(R.menu.my_menu, menu);
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            final View v = getActivity().findViewById(R.id.your_menu_item);
            if (v != null) {
                v.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        return false;
                    }
                });
            }
        }
    });
}
mallaudin
  • 4,744
  • 3
  • 36
  • 68
3

This is how I did it:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem item = menu.findItem(R.id.no_toast);
    item.setActionView(R.layout.custom_view);
    item.getActionView().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //handle click (just this item)
        }
    });
    return true;
}

and this is my menu:

<?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="Never gonna see me in a toast!"
        app:showAsAction="always"
        android:id="@+id/no_toast" />
</menu>

my custom view is just an ImageButton:

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/Widget.AppCompat.Toolbar.Button.Navigation"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/icon" />

Note: don't forget to set style, Widget.AppCompat.Toolbar.Button.Navigation makes IamgeButton to be shown correctly in toolbar.

P.S: Personally I prefer the default behavior but this was my case: I disabled right to left support for my application and after that when I set default locale to a rtl language, toast was showing up in the wrong side! Honestly I was in hurry and didn't find out the reason but I'll appreciate if someone let me know the why, anyway this is how I passed through.

Farshad
  • 3,074
  • 2
  • 30
  • 44
1

You can achieve this by using custom action view as follow:

action_menu.xml
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:support="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/item1"
        support:showAsAction="always">
    </item>

</menu>

custom_action_view.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:paddingRight="5dp" >

    <ImageButton
        android:id="@+id/customActionItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="@drawable/abc_item_background_holo_dark"
        android:src="@drawable/bulb_icon" />

    </RelativeLayout>

and menu inflater code is as follow:

    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.action_menu, menu);     

        final MenuItem item1= menu.findItem(R.id.item1);
        MenuItemCompat.setActionView(item1, R.layout.custom_action_view);
        View vItem1= MenuItemCompat.getActionView(item1);

        final ImageButton customActionItem= (ImageButton) vItem1.findViewById(R.id.customActionItem);
        customActionItem.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // do something here
            }
        });

        return super.onCreateOptionsMenu(menu);
    }
0

You could try to create your own custom ActionBar. Here is a tutorial on how to do that: Custom Action Bar

BC2
  • 892
  • 1
  • 7
  • 23
0

For me the solution was using:

android.support.v4.view.MenuItemCompat

So instead of inflating the menu from the XML:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.refresh_menu, menu);
    return true;
}

I created the items programmatically using MenuItemCompat:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem refreshItem = menu.add(Menu.NONE, R.id.menu_item_refresh, Menu.NONE, R.string.general_pop_up_dialog_btn_cancel);
    MenuItemCompat.setActionView(refreshItem, R.layout.actionbar_custom_view_refresh);
    MenuItemCompat.setShowAsAction(refreshItem, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
    return true;
}
CookieMonster
  • 1,723
  • 1
  • 15
  • 15
-2

There is a way, if you're using ActionBarSherlock. Find the ActionMenuItemView.java file in library and just comment whole onLongClick method.