logo logo

How to Create QuickAction Dialog in Android

Home » Information Technology » Programming » Android » How to Create QuickAction Dialog in Android




[Update 15/07/2011]

I have updated the quickaction implementation code so it can be used more efficient and the dialog will be automatically dismissed after pressing the action item. All the source codes now available via github so you can track the changes.

Official Twitter application for Android  has introduced new Android UI features and behavior patterns such as Dashboard, Search Bar, QuickAction and Action Bar. One of the interesting pattern is QuickActions that displays contextual actions in a list view. This pattern actually already exists in QuickContact dialog/bar in default Contact application (since Android 2.0).

QuickContact QuickContact

The QuickActions dialog is not included in standard Android SDK, so we have to create it manually. At first, i had no idea on how to create it so i decided to download and read the Contact app source code from  Android git. I found that the QuickContact dialog  uses private API call (com.android.internal.policy.PolicyManager) that does not exists in standard SDK. After posting question about it on google groups and stack overflow, i got the solution for it from Qberticus (thanx Qberticus!).

Qberticus’s QuickActions uses simple/plain layout so i have to create a custom layout so it will look like QuickContact in Contact app or QuickActions in Twitter app. Based on QuickContact source code, i made a slight modification on Qberticus’s BetterPopupWindow class and extended it to implement custom layout. I also made it customizeable, so the icon and text in action list can be customized.

Here are the screenshoots of QuickActions demo:

QuickContact / Twitter-like QuickActions

Code snippet
Create action items

//Add action item
ActionItem addAction = new ActionItem();

addAction.setTitle("Add");
addAction.setIcon(getResources().getDrawable(R.drawable.ic_add));

//Accept action item
ActionItem accAction = new ActionItem();

accAction.setTitle("Accept");
accAction.setIcon(getResources().getDrawable(R.drawable.ic_accept));

//Upload action item
ActionItem upAction = new ActionItem();

upAction.setTitle("Upload");
upAction.setIcon(getResources().getDrawable(R.drawable.ic_up));

Line 02: Create new action item
Line 04: Set action title
Line 05: Set action icon

Create quickaction instance and setup listener

final QuickAction mQuickAction 	= new QuickAction(this);

mQuickAction.addActionItem(addAction);
mQuickAction.addActionItem(accAction);
mQuickAction.addActionItem(upAction);

//setup the action item click listener
mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
	@Override
        public void onItemClick(int pos) {
		if (pos == 0) { //Add item selected
		   Toast.makeText(Example1Activity.this, "Add item selected", Toast.LENGTH_SHORT).show();
		} else if (pos == 1) { //Accept item selected
		   Toast.makeText(Example1Activity.this, "Accept item selected", Toast.LENGTH_SHORT).show();
		} else if (pos == 2) { //Upload item selected
		   Toast.makeText(Example1Activity.this, "Upload items selected", Toast.LENGTH_SHORT).show();
		}
	}
});

Line 1: Create quickaction instance
Line 3-5: Add the action items into quikaction
Line 8: Setup listener for action item clicked, the pos argument on onItemClick method shows which action item is clicked.

Show quickAction dialog

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
	mQuickAction.show(v);
}
});

Line 04: Show quickaction dialog, the v argument used as the anchor where the quickaction displayed.

Gallery3D-like QuickActions

Implementation on My Application

BlitzDroid

Minapolitan (Prototype)

Buy me a coffee

If you found this stuff useful to your work, please consider a donation.
By doing this you will be helping me to keep improving this stuff and to continue to help anyone who needs, and maybe keep on helping you in the future.


Thanks for your support, will do my best to keep the info fresh and useful.

Share
Related post:
bottom

218 Responses to “How to Create QuickAction Dialog in Android”

  1. kyith says:

    this is a god send!

  2. Dragan says:

    This is huge man. I’ve been searching for Tweeter app source code all time since it was published just to find out how this QuickActions bar works.
    Thank you very much.

  3. Imrabti says:

    Thank you, I was searching for this a long time ago.

  4. akex says:

    Thank you.

    I modified a bit your code (i did it first with Qberticus.

    Replacing in CustomPopupWindow

    this :

    if (rootHeight > anchorHeight) {

    by :

    if (rootHeight > anchor.getTop() {

    Cause in Listview, i managed to have Quickactionbar over my title bar, not so nice. With getTop, no problem anymore.

  5. akex says:

    Shared your page with some other french devs. Lot of thanks for you :) and Qberticus

  6. Matt Oakes says:

    Thanks for the code. I’m guessing the code is released under the same licence as the original code (new BSD licence)?

    Thanks again. I’ll make sure to mention it at the MANGTUG meeting next week :)

  7. 5xnewx says:

    Hi Lorenz,
    If i wanna like t olearn adnroid prgramming development and contribute, how it can be started. I have done and replciated exact tools steps which you have mentioned in your blog.

    Now what are the resources where i can learn to program and develop android application.

    Appreciate your time and support on your blog.

    Thanks,

  8. Ash says:

    Thanks a lot … !!!
    I was stuck in my project without this ….
    u r an angel :) :) :)

  9. scottyab says:

    Thanks, can’t wait to try it out later. Did you look into the Action bar that’s also mentioned in the google I/O talk and in the Twitter app.

  10. Ash says:

    I am making my own Contact mgmt system & implemented the above code. i need 2 implement the further functionality now.. ie; to make a call after clicking on the “Call” Action Item.. I have the code to initiate a call from Android. But as you see in the Native Address Book, if you have 2-3 phone numbers saved for a contact, the QuickAction window grows down listing all the numbers saved & asks you to select one from the 2-3 phone numbers.. any idea how to implement this window …

    Thanx in advance..

    • lorenz says:

      Hi Ash,

      You can read the source code of Contacts app from android source tree, there is a method inside QuickContactWindow.java that implements it. Sorry i can’t give you the exact solution. :-)

  11. Clay Graham says:

    OMG YOU are awesome. I cant believe that 3 months later they still havent published the source code for this even though they say its “best practices”. Good on you man!

  12. Christoffer says:

    Hi, I’d like to make a few modifications to the above presented solution.

    First of all, if I want to use icons which are larger than the once in the example, the action item area grows below the row of action items. I’ve tried modifying the padding and such to give the icon some more space, but it didn’t work.

    Any ideas how to make the above example work with arbitrarly large icons? And/or with the text below the icon?

    • lorenz says:

      Hi Christoffer,

      In order to use more bigger action icons, you must make a modification on quickaction’s icon, make a new bigger image for it’s rail background. Or try to use Twitter’s quickaction icon (extract its apk files and copy background image from drawable-mdpi/hdpi).

  13. Phuc Nguyen says:

    Hi. Your stuff is awesome!

    But is it possible to dim the screen behind the PopupWindow, just like with the context menu?

  14. Houses are expensive and not everybody is able to buy it. Nevertheless, personal loans are created to help different people in such hard situations.

  15. achie says:

    Hi can I use the code in my application? Is there any thing else that I need to do regarding the license like referring you or something else to use this code?

    Thank you for the code. Have been looking for this since a few days.

  16. Dothis says:

    Hi, I’d like to make for than 2 items in the QuickAction. How can I do this?

    Thanks!

  17. Nice post, I will create this api. too in my mobile.

  18. Herbert says:

    Nice post,
    I hava a question, how to dismiss the quickaction when the activity don’t to change.

  19. Rui says:

    This is so cool! Great job, Lorenz. But when you use this component, you only target your application for Eclair and above? Do you know of any component that is similar to this for Cupcake and Donut? I’m not saying that it has to be exactly the same, but alike.

  20. I have modified your layout to present the label for the icon below the icon itself, much like the Twitter application. In order to do this, make the following changes to res/layout/action_item.xml:

    LinearLayout:
    android:orientation = “vertical”
    android:paddingTop = “0dip”
    android:paddingBottom = “0dip”

    ImageView:
    android:layout_height = “fill_parent”
    android:paddingLeft = “5dip”
    android:paddingRight = “5dip”

    TextView:
    android:layout_width = “wrap_content”
    android:textSize = “10sp”

    There is only one caveat to this that I can see: in order for the labels to all line up, the icons must be the same size. Since they _should_ be the same size to begin with, this isn’t much of a problem.

    I hope that helps someone! Thank you so much for the code!

  21. Barry says:

    Tnx, very nice, tnx for this :)

    @Michael, that is also a nice adjustment :)

  22. blundell says:

    That is sexy, thanks

    hopefully I can fiddle with it some more ;-)

  23. This post is a winner! Love the tips and everything you suggest.. Thanks

  24. Your tips are really great except the one suggesting to steal our competition articles haha . Nice tipps, thanks!

  25. Thank you for taking the time to publish this information very useful!

  26. Awesome work buddy. This has helped me a lot.

  27. [...] Androidに関する、はてなブックマーク新着情報です。 How to Create QuickAction Dialog in Android | All About Web & Mobile Application Development [...]

  28. Ole K says:

    You Sir, are my official hero of he week! This is simply great :) I hope Google sees this and pulls it into the official Android API ASAP.

    And thanks to Michael Herold as well, for his alternative layout making the text appear below the icons.

  29. Jamie Nordmeyer says:

    I’ll reiterate what everyone else on here as said so far. Thanks a ton to you and Qberticus for this work. I was just about to give up and try to work this myself when I finally stumbled upon your blog post. I’m hoping that Google will put something like this in the next release of the SDK, but until then, you’ve done a beautiful job with both samples. Thanks again!

  30. Waseem Sarwar says:

    Thanks really very helpfull

  31. Mike says:

    Great tutorial.

    I am fairly new to Android and Java. I implemented this in my app, but I do not know how to make the Pop-up dismiss automatically when an option is selected.

  32. VERY good job man. It’s a shame that android doesn’t provide that by default.

  33. Matthias Schippling says:

    Great job! Thanks for your work :)

  34. Joule says:

    I will try to testing in my UI enviroment

    Trims dude

  35. hadi says:

    hi. this is just what I’m looking for. thx.

    What is the best practice when dealing with PopupWindow class ?
    Is it okay if I just create and create another instance everytime I want to show a popup ?
    Can I reuse the same instance ? That is after I call dismiss, then I call showAtLocation again.

    thx

  36. [...] have been a few implementations of this by Android developers. The best I’ve found so far is available here and is developed by lorenz. I’ve talked about this in a previous post so I wont add too much [...]

  37. Muniu says:

    Lorenz,

    Good work on the widget, saved a life here!
    Quick question though, how do you implement Gallery3D-like QuickActions?
    I’ve tried
    mQuickAction.showDropDown(); with no luck,
    mQuickAction.showLikeQuickAction(0,30); with no luck either.
    Thanks.

  38. baychev says:

    awesome tutorial, thanks alot.
    one question about it: is it possible to implement quick actions using this code as well on the application icon? much like the contacts placed on the home screen pop up a context menu to call, sms, emai, etc. how would you go about it?

  39. Patrick says:

    Great job, love the look and feel of your quick action. I have found one problem while using a listadapter that has more than one size of views in the list. The quickaction window does not line up quite right like it does with a list of equal size views. Anyone have this problem or any solutions?

  40. [...] stumbled on some work that Lorenz did around QuickActions and the code was free to use, easy to understand and easy to modify which [...]

  41. DextoR says:

    thanks, amazing bunch of code. really gives a nice look to the application!

  42. Jonathan says:

    I found a bug…

    if (rootHeight > anchor.getTop()) {

    should be

    if (rootHeight > anchor.top) {

    because anchor.getTop() will give the relative and not the absolute top.

  43. Emil says:

    Hi man! thanks so much for this, i made a slight change is the CustomPopupWindow class to make dissmissing a quickaction look much better, with this code it wont become see-through when pressed.

    thanks once again!

    public void dismiss() {
    Handler mHandler = new Handler(); mHandler.postDelayed(mUpdateTimeTask,10);
    }

    Runnable mUpdateTimeTask = new Runnable(
    {
    public void run() {
    window.dismiss();
    }
    };

  44. Sebastian says:

    Fantastic work! Exactly what I was looking for.

Leave a Reply

 
bottom