Android quick action menu


Quick actions are one of the simplest way of showing all the actions user can perform on any item displayed on screen.

 

To display a quick action, all you have to do is create an instance of android.widget.PopupWindow.PopupWindow and set the content view.

You can add animation, touch listeners, click listeners, or anything that you can perform on any of the widget supported by Android.

Here is a sample implementation of QuickAction. In this sample I am only demonstrating how to show a PopupWindow.

You can modify this sample and make it suitable for your application.

/**
 *
 */
package com.techjini.sample;

import android.content.Context;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.PopupWindow;

/**
 * @author anshu
 *
 */
public class MyCustomDialog implements OnTouchListener {

    private View triggerView;
    private PopupWindow window;
    protected final WindowManager windowManager;

    public MyCustomDialog(View triggerView) {
        this.triggerView = triggerView;
        window = new PopupWindow(triggerView.getContext());
        window.setTouchable(true);
        window.setTouchInterceptor(this);
        windowManager = (WindowManager) triggerView.getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        ImageView imageView = new ImageView(triggerView.getContext());
        imageView.setImageResource(R.drawable.icon);
        window.setContentView(imageView);
        window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        window.setTouchable(true);
        window.setFocusable(true);
        window.setOutsideTouchable(true);
    }

    public boolean onTouch(View v, MotionEvent event) {
        if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
            this.window.dismiss();
            return true;
        }
        return false;
    }

    public void show() {
        int[] location = new int[2];
        triggerView.getLocationOnScreen(location);
        window.showAtLocation(triggerView, Gravity.NO_GRAVITY,
                location[0] + 50, location[1] + (triggerView.getHeight() / 2));
    }

}

Using this class is also very simple. Suppose you have a Button buttonView and on click of buttonView you want to show quick action then your code will look something like this.

        MyCustomDialog dialog = new MyCustomDialog(buttonView);
        dialog.show();
Advertisements

One comment

  1. Thanks. I was looking for simple quick action tutorial that just shows how to implement it, without all those animations etc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s