Hiding of the Toast for long press on actionBar item

空扰寡人 提交于 2019-12-17 16:34:13

问题


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>


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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;
                    }
                });
            }
        }
    });
}



回答5:


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.




回答6:


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);
    }



回答7:


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




回答8:


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;
}



回答9:


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



来源:https://stackoverflow.com/questions/17408365/hiding-of-the-toast-for-long-press-on-actionbar-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!