Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SDK: full-screen floating context menu since Android 8.0 (API 26+)

Tags:

android

I've searched lots of sites but they are all out-of-date. How to create full-screen floating context menu in Android 8.0?

As you can see here, Google gives us instructions how to do it in its guides but I guess they are out-of-date as well. Why? Here's an example:

This is how it looks like on API 23 (source, his github codes say it's API 23 = Android 6.0) enter image description here

And this is how it looks like on API 26 (Android 8.0):

enter image description here

There must be some changes between these APIs but I can't really get an answer.

Any solutions would be appreciated.

like image 358
adek111 Avatar asked Oct 17 '22 21:10

adek111


1 Answers

It's actually a version specific behaivor.

Workaround for the project that you linked:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);

    registerForContextMenu(button);

    button.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            button.showContextMenu();
            return true;
        }
    });
}

if read the documentation about performLongClick() of the View class:

/**
 * Calls this view's OnLongClickListener, if it is defined. Invokes the
 * context menu if the OnLongClickListener did not consume the event,
 * anchoring it to an (x,y) coordinate.
 *
 * @param x x coordinate of the anchoring touch event, or {@link Float#NaN}
 *          to disable anchoring
 * @param y y coordinate of the anchoring touch event, or {@link Float#NaN}
 *          to disable anchoring
 * @return {@code true} if one of the above receivers consumed the event,
 *         {@code false} otherwise
 */
public boolean performLongClick(float x, float y) 

so it's a standard realisation of showing context menu (if you register listener context menu on view, it will processed with coordinates). So to avoid showing menu with coordinates, just show directly on long click of the view by showContextMenu()

like image 108
HeyAlex Avatar answered Oct 20 '22 17:10

HeyAlex