2

In Android SDK overriden method toString use actionToString method as a have public static modificators. If you open source code you must see :

 @Override
    public String toString() {
        StringBuilder msg = new StringBuilder();
        msg.append("MotionEvent { action=").append(actionToString(getAction()));

        final int pointerCount = getPointerCount();
        for (int i = 0; i < pointerCount; i++) {
            msg.append(", id[").append(i).append("]=").append(getPointerId(i));
            msg.append(", x[").append(i).append("]=").append(getX(i));
            msg.append(", y[").append(i).append("]=").append(getY(i));
            msg.append(", toolType[").append(i).append("]=").append(
                    toolTypeToString(getToolType(i)));
        }

        msg.append(", buttonState=").append(MotionEvent.buttonStateToString(getButtonState()));
        msg.append(", metaState=").append(KeyEvent.metaStateToString(getMetaState()));
        msg.append(", flags=0x").append(Integer.toHexString(getFlags()));
        msg.append(", edgeFlags=0x").append(Integer.toHexString(getEdgeFlags()));
        msg.append(", pointerCount=").append(pointerCount);
        msg.append(", historySize=").append(getHistorySize());
        msg.append(", eventTime=").append(getEventTime());
        msg.append(", downTime=").append(getDownTime());
        msg.append(", deviceId=").append(getDeviceId());
        msg.append(", source=0x").append(Integer.toHexString(getSource()));
        msg.append(" }");
        return msg.toString();
    }

And if you open actionToString method in the SAME class:

public static String actionToString(int action) {
    switch (action) {
        case ACTION_DOWN:
            return "ACTION_DOWN";
        case ACTION_UP:
            return "ACTION_UP";
        case ACTION_CANCEL:
            return "ACTION_CANCEL";
        case ACTION_OUTSIDE:
            return "ACTION_OUTSIDE";
        case ACTION_MOVE:
            return "ACTION_MOVE";
        case ACTION_HOVER_MOVE:
            return "ACTION_HOVER_MOVE";
        case ACTION_SCROLL:
            return "ACTION_SCROLL";
        case ACTION_HOVER_ENTER:
            return "ACTION_HOVER_ENTER";
        case ACTION_HOVER_EXIT:
            return "ACTION_HOVER_EXIT";
    }
    int index = (action & ACTION_POINTER_INDEX_MASK) >> ACTION_POINTER_INDEX_SHIFT;
    switch (action & ACTION_MASK) {
        case ACTION_POINTER_DOWN:
            return "ACTION_POINTER_DOWN(" + index + ")";
        case ACTION_POINTER_UP:
            return "ACTION_POINTER_UP(" + index + ")";
        default:
            return Integer.toString(action);
    }
}

But, when i try to use this method

MotionEvent.actionToString(event.getAction);

IDE tell me about error.

Cannot resolve method actionToString(int);

Why i getting this error?


Link to the class methods :

actionToString method

toString method

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119

2 Answers2

1

This method existed in the source of platforms older than API 19, but was hidden.

See the source from JellyBean:

https://android.googlesource.com/platform/frameworks/base/+/jb-mr2.0.0-release/core/java/android/view/MotionEvent.java

/**
 * Returns a string that represents the symbolic name of the specified action
 * such as "ACTION_DOWN", "ACTION_POINTER_DOWN(3)" or an equivalent numeric constant
 * such as "35" if unknown.
 *
 * @param action The action.
 * @return The symbolic name of the specified action.
 * @hide
 */
public static String actionToString(int action) {

Note the @hide annotation in the JavaDoc.

In KitKat, the annotation is no longer present:

https://android.googlesource.com/platform/frameworks/base/+/kitkat-release/core/java/android/view/MotionEvent.java

Natix
  • 14,017
  • 7
  • 54
  • 69
  • I have a question. hide annotation located in JavaDoc. It seems she related as annotation to this method? – Sergey Shustikov Jan 23 '14 at 13:24
  • I'm not sure what you meant, but have a look at this: http://stackoverflow.com/questions/17035271/what-does-hide-mean-in-the-android-source-code – Natix Jan 23 '14 at 13:27
0

This method was added with Android SDK 19 - 4.4 : http://developer.android.com/reference/android/view/MotionEvent.html#actionToString%28int%29

you have to set your targetSdkVersion to 19 minimum to use it. Be sure to use

@SuppressLint("NewApi")

    if (android.os.Build.VERSION.SDK_INT >= 19) 
    {
         //...  
    }

if your min sdk is set below 19

Edit: it seems to be secretly added in the later versions of JellyBean: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/android/view/MotionEvent.java#MotionEvent.actionToString%28int%29

Simon Meyer
  • 1,946
  • 12
  • 23