Reputation: 135
I want to show an menu item only if the device supports the stylus for input.
Unfortunately i've found nothing check if the device or the display supports the stylus/Spen for input.
Edit: I can distinguish between Stylus and Finger after a MotionEvent is triggered using event.getToolType(). If the tooltype is TOOL_TYPE_STYLUS i can be sure that it supports the stylus. If not i can guess if there is a pressure > 0 (relating to how to detect if the screen is capacitive or resistive in an Android device?)
But i would like to know it in the onCreate method of my activity.
Following is somehow not supported and does not work for me.
Configuration config = getResources().getConfiguration();
if (config.touchscreen == Configuration.TOUCHSCREEN_STYLUS)
Toast.makeText(this, "TOUCHSCREEN_STYLUS", Toast.LENGTH_SHORT).show();
Upvotes: 1
Views: 8142
Reputation: 476
The answer of @Aaron Gillion is looking to be working, but trusting detection of a function to a name looks non-100%-reliable.
I checked out the standard Android InputDevice
and found out some an interesting function supportsSource
which allows if the device supports e.g. SOURCE_STYLUS
. Here an example:
if (Build.VERSION_CODES.JELLY_BEAN <= Build.VERSION.SDK_INT) {
InputManager im = (InputManager) context.getSystemService(INPUT_SERVICE);
for (int id : im.getInputDeviceIds()) {
InputDevice inputDevice = im.getInputDevice(id);
if (
inputDevice.supportsSource(InputDevice.SOURCE_STYLUS) ||
inputDevice.supportsSource(InputDevice.SOURCE_BLUETOOTH_STYLUS)
)
return true;
}
}
return false;
If you pursue lower SDK you can inline supportsSource
function, which is ridiculously simple:
public boolean supportsSource(int source) {
return (mSources & source) == source;
}
And constants:
added in 23 - SOURCE_BLUETOOTH_STYLUS = 0x00008000 | SOURCE_STYLUS
added in 14 - SOURCE_STYLUS = 0x00004000 | SOURCE_CLASS_POINTER
added in 9 - SOURCE_CLASS_POINTER = 0x00000002 from 9
I've tested the code on a couple of devices and it is detecting the stylus correctly. I would appreciated if you can share your results of detecting the stylus on non-Samsung devices.
Upvotes: 2
Reputation: 2265
You can detect S-Pen and other stylus's pretty reliably via InputManager:
boolean sPen = false;
if(Build.VERSION.SDK_INT > 15) {
InputManager inptmgr = (InputManager)getSystemService(INPUT_SERVICE);
int[] inputs = inptmgr.getInputDeviceIds();
for(int i = 0;i<inputs.length;i++) {
if(inptmgr.getInputDevice(inputs[i]).getName().toLowerCase().contains("pen")) sPen = true;
}
}
Usually devices will register with their appropriate names contained in them, for example, "Bluetooth Mouse", "Logitech USB Keyboard" or "E_Pen"
Upvotes: 4
Reputation: 1356
Here you go (from the Android documentation) - seems to only be supported in 4.0 and up though. http://developer.android.com/about/versions/android-4.0.html
Android now provides APIs for receiving input from a stylus input device such as a digitizer tablet peripheral or a stylus-enabled touch screen.
Stylus input operates in a similar manner to touch or mouse input. When the stylus is in contact with the digitizer, applications receive touch events just like they would when a finger is used to touch the display. When the stylus is hovering above the digitizer, applications receive hover events just like they would when a mouse pointer was being moved across the display when no buttons are pressed.
Your application can distinguish between finger, mouse, stylus and eraser input by querying the “tool type" associated with each pointer in a MotionEvent using getToolType(). The currently defined tool types are: TOOL_TYPE_UNKNOWN, TOOL_TYPE_FINGER, TOOL_TYPE_MOUSE, TOOL_TYPE_STYLUS, and TOOL_TYPE_ERASER. By querying the tool type, your application can choose to handle stylus input in different ways from finger or mouse input.
Your application can also query which mouse or stylus buttons are pressed by querying the “button state" of a MotionEvent using getButtonState(). The currently defined button states are: BUTTON_PRIMARY, BUTTON_SECONDARY, BUTTON_TERTIARY, BUTTON_BACK, and BUTTON_FORWARD. For convenience, the back and forward mouse buttons are automatically mapped to the KEYCODE_BACK and KEYCODE_FORWARD keys. Your application can handle these keys to support mouse button based back and forward navigation.
In addition to precisely measuring the position and pressure of a contact, some stylus input devices also report the distance between the stylus tip and the digitizer, the stylus tilt angle, and the stylus orientation angle. Your application can query this information using getAxisValue() with the axis codes AXIS_DISTANCE, AXIS_TILT, and AXIS_ORIENTATION.
Upvotes: 3
Reputation: 9117
I don't think there is any such thing as to detect stylus input. I would assume that if the device has touch capability, it can also respond to a stylus. Android doesn't specifically support stylus input, as far as I know.
Upvotes: 0