I have a button that I would like clickable only by a stylus.
I used the method setClickable to enable or disable the click on the button, but how can I do that is clickable only with a pen?
the button should be clickable only when MotionEvent.TOOL_TYPE_STYLUS
how do i can?
you could override the Button's onTouchListener, and return immediately it the touch event is not performed through TOOL_TYPE_STYLUS. To retrieve this information, you can use
getToolType(int pointerX)
From the documentation
Gets the tool type of a pointer for the given pointer index
if it returns TOOL_TYPE_STYLUS, then you can simply check the MotionEvent for the ACTION_DOWN/ACTION_UP, and call performClick(). E.g.
b.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS) {
if (event.getAction() == MotionEvent.ACTION_UP ) {
performClick();
return true;
}
}
return false;
}
});
Related
OnTouchEvent is being fired after the second time touch; yet, I want it to be fired after the first touch itself.
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouchEvent(MotionEvent event) {
//too avoid touch being detected continuously
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (Function.system[5] == "Gravity: on") {
//some action...
}
}
return super.onTouchEvent(event)
}
You need to tell Android to continue to respond to touch events. Returning true let's you capture events from action down , action move and then action up.
Please add the return true for each event
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouchEvent(MotionEvent event) {
//too avoid touch being detected continuously
if (event.getAction() == MotionEvent.ACTION_DOWN) {
}
return true;
}
Please try and seee if this works for you
How can I detect clicked, pressed and released states of a Button. I want to perform different functions on these states. On click I want to call function1, on press I want to call function2 and on receive I want to call function3.
We can detect click state using View.OnClickListener. We can detect Pressed and Released states of a Button using View.OnTouchListener and handling ACTION_DOWN and ACTION_UP. I am able to detect these states individually, however, not together.
Below is code for OnCLickListener.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(" clicked ");
}
});
Below is code for OnTouchListener.
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(" pressed ");
return true;
case MotionEvent.ACTION_UP:
System.out.println(" released ");
return true;
}
return false;
}
});
When I set click and touch listeners on a Button, Click event never gets called. Instead I receive pressed and released state.
How can I handle these three states together?
EDIT:
I added the OnClickListener and OnTouchListener code I have used.
Easy since Button is a View:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Pressed
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// Released
}
return true;
}
});
Change the return true; inside case MotionEvent.ACTION_DOWN: and case MotionEvent.ACTION_UP:to return false; or break;
See this link.
You can handle click manually in MotionEvent.ACTION_UP event by
button.performClick();
clicked event include pressed and released state,if you want to fire clicked event,put method after ACTION_UP
I want to keep a button displayed pressed. I want to use another way instead of onPressed() in onTouch(). I add setPressed(), setSelected() but not worked. when i add these methods in onTouch() the program is good but my animation is very slow.
Can i use these method in onClick() method but works in this?
Please explain for me
Instead of using the onClick-event which returns the button to the non-pressed state, you can set the pressed state in the onTouch-event:
yourbutton.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
((Button) v).setPressed(true);
//TODO: Add the code of your onClick-event here
}
return true;//Return true, so there will be no onClick-event
}
});
If this affects any animation, you should look for the problems there.
This piece of code makes the button work as a toggle button. It sets the pressed property to its opposite.
If the button is pressed, it's set as unpressed.
If the button is unpressed, it's set as pressed.
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Button b = ((Button) v);
b.setPressed(!b.isPressed());//if pressed, unpress; if unpressed, press
}
return true;
}
What you are describing seems to be a toggle button. Maybe you should check this out, instead of doing a hackish solution :
Toggle Buttons
Extend ToggleButton or:
Button button = (Button) view.findViewById(R.id.button1);
button.setTag(false);
button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP)
if ((boolean) v.getTag()) {
v.setPressed(false);
v.setTag(false);
return false;
} else {
v.setPressed(true);
v.setTag(true);
return true;
}
return false;
}
});
I have a custom listview in my android application. When a user press an item in the list I want to change the background color of the pressed item. This is the code for that behaviour:
tempView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isDown = false;
tempView.setBackgroundColor(Color.parseColor("#f47920"));
}
if (event.getAction() == MotionEvent.ACTION_UP) {
tempView.setBackgroundResource(R.drawable.list_selector_focused);
}
if(event.getAction() == MotionEvent.ACTION_MOVE) {
tempView.setBackgroundResource(R.drawable.list_selector_focused);
}
return false;
}
});
But when I "Fling" my finger over the screen to scroll the listview, an item is also marked, and the "pressed" color will be static. How can I avoid this?
MotionEvent.ACTION_UP will not be caught unless you return true from onTouch() to inform Android system that you handled the whole touch event.
I have the following code which creates an ImageButton and plays a sound when clicked:
ImageButton SoundButton1 = (ImageButton)findViewById(R.id.sound1);
SoundButton1.setImageResource(R.drawable.my_button);
SoundButton1.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
mSoundManager.playSound(1);
return true;
}
return false;
}
});
The problem is that I want the image on the ImageButton to change when you press it. The OnTouchListener appears to be overriding the touch and not allowing the images to change. As soon as I remove the OnTouchListener, the ImageButton swaps to a different image when pressed. Any ideas on how I can have the images change on the ImageButton while still using the OnTouchListener? Thank you very much!
SoundButton1.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
mSoundManager.playSound(1);
btnmode.setImageResource(R.drawable.modeitempressed);
}
elseif (event.getAction() == MotionEvent.ACTION_UP ) {
btnmode.setImageResource(R.drawable.modeitemnormal);
}
return false;
}
});
I think the solution is simple: remove the return true from the ontouchlistener. Since that blocks all further operations that respond to touch and input. Make it return false too.
This way it will allow other actions to also respond to the touch.