Keep pressed a button in onClick() - android

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

Related

Repeatedly performing an action while button is clicked

I have a button that runs a code that changes the background to a random color on each click. I would also like to give the user the ability to keep on changing the background color as long as the button is clicked. I believe that the onTouchListener will be my best bet. However, I do not now how to implement the code correctly.
I tried on the onLongClickListener but found out that onLongClickListener doesn't work that way.
Incomplete code for the onTouchListener (randomize is the name of my button):
randomize.setOnTouchListener(new Button.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
// start the thread
return true;
} else if(event.getAction() == MotionEvent.ACTION_UP){
// stop the thread
return true;
}
return false;
}
});
So, what I aim to be able to do is to keep on pressing the button and having the background continuously change while still preserving the onclick method of the button. So, onclick changes the background once and continuous click changes the background continuously.
Thank you so much folks :)
Ps. I'm just a beginner to android so I'm sorry if I do not know much. :)
try this:
btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
count++;
Toast.makeText(getApplicationContext(),Integer.toString(count) , Toast.LENGTH_SHORT).show();
return true;
}
});

button clickable only with a stylus

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

Click, Press and Release event on Button

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

While loop in setOnTouchListener

I want to do some action when button is pressed and stop those action when button is releasd. I have used the following code but it does not work. you may check my code here-
ok = (Button)findViewById(R.id.button1);
ok.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
my_var=true;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
while(my_var) do_something();
}else if (event.getAction() == MotionEvent.ACTION_UP) {
my_var=false;
}
return true;
}
});
Is there any alternative solution for the above. Thanx in advance.
I think what you want is an AutoRepeatButton
Basically it's a class that extends Button and adds some scheduled callbacks (you can adjust the interval) that happen as long as the ACTION_DOWN MotionEvent is occurring.
There are a few implementations floating around but I like the following one the best:
https://stackoverflow.com/a/8463940/833647
To achieve so, you have to set my_var to false somewhere in the do_something method.

How can I change the images on an ImageButton in Android when using a OnTouchListener?

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.

Categories

Resources