Friday, April 20, 2012

Quick Action in Android

Quick Action - Dialog like Twitter



Introduction
The key is a little unknown class in the android.widget namespace. PopupWindow is the key. It allows you to display any arbitrary view on the screen.
It does most of the work for you it just needs a little bit of massaging to have it do exactly what you want.

For the Popdown menu look it's really just a matter of having the correct animation set. You can look at the Animation.PopDownMenu in the styles.xml
to see how it's done.

The QuickAction look is only slightly more complicated. It needs two animations, one for when it pops out of the top of a view, and one for when it pops down bellow the view. Then it's just a matter of computing where it should display. You can see that in BetterPopupWindow#showLikeQuickAction.

I have create QuickActionExample, attached with this article.

How to use
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));

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

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

Please refer to this given link for more information.
Reference Link:-[[http://code.google.com/p/simple-quickactions/]]

No comments:

Post a Comment