[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).

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

<br />//Add action item<br />ActionItem addAction = new ActionItem();<br /><br />addAction.setTitle("Add");<br />addAction.setIcon(getResources().getDrawable(R.drawable.ic_add));<br /><br />//Accept action item<br />ActionItem accAction = new ActionItem();<br /><br />accAction.setTitle("Accept");<br />accAction.setIcon(getResources().getDrawable(R.drawable.ic_accept));<br /><br />//Upload action item<br />ActionItem upAction = new ActionItem();<br /><br />upAction.setTitle("Upload");<br />upAction.setIcon(getResources().getDrawable(R.drawable.ic_up));<br /> Line 02: Create new action item

Line 04: Set action title

Line 05: Set action icon Create quickaction instance and setup listener <br />final QuickAction mQuickAction = new QuickAction(this);<br /><br />mQuickAction.addActionItem(addAction);<br />mQuickAction.addActionItem(accAction);<br />mQuickAction.addActionItem(upAction);<br /><br />//setup the action item click listener<br />mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {<br /><%%KEEPWHITESPACE%%> @Override<br /><%%KEEPWHITESPACE%%> public void onItemClick(int pos) {<br /><%%KEEPWHITESPACE%%> if (pos == 0) { //Add item selected<br /><%%KEEPWHITESPACE%%> Toast.makeText(Example1Activity.this, "Add item selected", Toast.LENGTH_SHORT).show();<br /><%%KEEPWHITESPACE%%> } else if (pos == 1) { //Accept item selected<br /><%%KEEPWHITESPACE%%> Toast.makeText(Example1Activity.this, "Accept item selected", Toast.LENGTH_SHORT).show();<br /><%%KEEPWHITESPACE%%> } else if (pos == 2) { //Upload item selected<br /><%%KEEPWHITESPACE%%> Toast.makeText(Example1Activity.this, "Upload items selected", Toast.LENGTH_SHORT).show();<br /><%%KEEPWHITESPACE%%> }<br /><%%KEEPWHITESPACE%%> }<br />});<br /> 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 <br />btn1.setOnClickListener(new View.OnClickListener() {<br />@Override<br />public void onClick(View v) {<br /><%%KEEPWHITESPACE%%> mQuickAction.show(v);<br />}<br />});<br /> Line 04: Show quickaction dialog, the v argument used as the anchor where the quickaction displayed. Download source code (Github) Gallery3D-like QuickActions Download source code (Github) 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.

by