Repeatedly performing an action while button is clicked - android

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

Related

Android Button animation starting after click handler finished

I could see that the animation when tapping on a button starts after the stuff in the onClick method finished and was wondering if this is an Android bug or normal behaviour?. I could find a few posts (this one e.g.) about people searching how to start something after the animation ended, so I am not alone I suppose, but I don't think this is logic at all?
YouButton. setOnTouchListener(
new View.OnTouchListener() {
public boolean onTouch(View myView,
MotionEvent event) {
int action = event.getAction();
if (action==MotionEvent.ACTION_UP)
{
//after animation
}
if (action==MotionEvent.ACTION_DOWN)
{
//your animation
}
return true;
}
}

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.

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 - Button OnTouch event change focus of Button

I have an App with 10 Buttons. Everytime the User presses on one Button A TextView should change. And if the User changes the focus and ,oves its finger to right, to the next button(without taking the finger off the screen) the seond button should be focused.
I tried it with setting an OnTouchListner to all buttons, but once the finger is moved the focus still stays on the first button:
btn1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
txt1.settext("1");
return false;
}
});
I hope you understood what I mean and can help me with this.
Thanks
Edit:
I found an Application which does that, here is a Video, so you can visualise what I mean.
Notice how I move the mouse(the finger in this case) and the Boxes change its focus:
http://www.youtube.com/watch?v=MRVFpNrBmsA&feature=youtu.be
To start in your onTouch() you should handle the events...
Something like
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
txt1.settext("1");
} else if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT){
txt1.settext("0");
}
return true;
}
Not sure if those are the proper actions to check against but it is just to give the idea.
Also I believe you should return true otherwise it means that the event wasn't processed and it gets stuck.

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.

Categories

Resources