johannilsson/android-actionbar — Issue #57: LongClick extension
Author: andrewsleeman24
Created: 2012-01-29 23:08:20+00:00
State: open

I found the ActionBar project yesterday and have found it to be wonderful.

One of the problems I have will Android application is that the purpose of icon buttons is not always clean (eg. some of the Gmail icons).

For this reason I needed adapted ActionBar of the user can use a LongClick to get a textual description of the Action's purposes.

I am not Git skilled and my Eclipse has completely reformatted the source code so I cannot easily generated a short list of differences.

If you are interested in the feature then the to ActionBar.java are quite trivial and outlined below.

1) There is an extended ActionBar.Action interface called "ActionBar.ActionPlus".
public interface ActionPlus extends Action {
public void performLongClickAction(View pView);
}

2) the class now implements "android.view.View.OnLongClickListener" and the supporting code is
@Override
public boolean onLongClick(View view) {
final Object tag = view.getTag();
if (tag instanceof ActionPlus) {
final ActionPlus action = (ActionPlus) tag;
action.performLongClickAction(view);
return true;
}
return false;
}

3) Last peice of the ActionBar code change is addition of the following 4 lines in the inflateAction() method.

labelView.setImageResource(action.getDrawable());
+ if (action instanceof ActionPlus) {
+ labelView.setLongClickable(true);
+ labelView.setOnLongClickListener(this);
+ }

A example to how I have used this is my application is :

actionBar.addAction(new ActionBar.ActionPlus() {
public void performAction(View view) {
doOpenDrawer();
}

public int getDrawable() {
return R.drawable.drawer_open;
}

public void performLongClickAction(View view) {
UtilsNotify.show(MainMenu.this, R.string.action_open_drawer_description);
}
});

I'm not sure if this is a Best Practice implementation of how to extended the ActionBar functionality.

1) A better solution may have been to use a function "public int getStringId();" instead of the long "performLongClickAction()" and then use "Toast" directly to display the text message.

2) Also if it was a private library module (where I did not care about backward compatibility), I would have just extended the definition of the "Action"/"ActionAbstract" implementations to handle the mStringId member (as is currently done for the mDrawable member.

NB. for my own local copy I have now implement both the above suggestions.