While loop in setOnTouchListener - android

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.

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

Keep pressed a button in onClick()

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

Android :: OnTouchListener && OnClickListener combination issue

Problem description:
I have a TextView on a RelativeLayout and I want to color it red when the user touches it, and go on another page when he clicks on it.
So I tried to set an OnClickListener to do the click, and an OnTouchListener to implement the touch function (MotionEvent.ACTION_DOWN) but this combination doesn't work, because OnTouchListener makes OnClickListener non-functional (don't know why).
On forums people say that we can implement the OnClick by the OnTouch MotionEvent.ACTION_UP, but this one can be triggered out of my TextView layout (the TextView gonna be clicked if you press it and drag your finger out of him to release) and this is not the desired behavior because I want:
click = press + release on the TextView.
Can someone give me a solution for this please?
you may call View.performClick() when action_up. Hope it helps.
your_txtView.setOnClickListener(new TextView.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
your_txtView.setOnTouchListener(new TextView.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
} else if (MotionEvent.ACTION_UP == event.getAction()) {
v.performClick();
}
return true;
}
});
Adel, is the problem with the first click, or you don't get any click at all?
There is this issue if you have multiple clickable layout you don't get any click events for the first. That's because it makes it first selected and then you get the click event, try the below code.
private class CustomTouchListener implements OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
TextView tv = (TextView) v.findViewById(R.id.single_line_text);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
tv.setTextColor(COLOR_WHEN_PRESSED);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
tv.setTextColor(COLOR_WHEN_RELEASED);
// Action of click goes here
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
tv.setTextColor(COLOR_WHEN_RELEASED);
// To handle release outside the layout region
}
return false;
}
}
This is working in my current implementation if you set the touch listener for your layout.
You also need to set below on your layout
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
Hope it helps!!!
EDIT: Additionally, there should be a flag in both DOWN and UP. Set it in DOWN and check if its set in UP. This will avoid a bug where user might tap anywhere in the screen and then hover on your textview and release it.
Had the same problem. Solved it by returning false from ACTION_MOVE. I've been fighting with it for few hours, trying various things, but seems like i've kept overlooking this little issue... And now it makes sense. When you return true from onTouch, futher processing is stopped, so that OnClickListener is not aware of any movements and triggers onClick even after pointer have moved outside of view.

how do i make different touch and click behaviours for buttons?

Im working on an app with a friend and i want the button to visually press by changing the background and vibrating and then when released it does whatever that button is supposed to do.
the buttons i have right now only have an onclick method but i want it vibrate when touched and execute their function when clicked
i know of the ontouch and onclick methods but i cant seem to use them togetherand have already implemented both onclicklistener and ontouchlistener
how might i manage this.
You could do this by only using the OnTouchListener:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
/* Change your button background and vibrate */
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
/* Button's functionality */
}
return true;
}
Hope it helps.

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