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;
}
}
Related
I have a RecyclerViewClickListener.RecyclerTouchListener and on that #onClick override method, I sat Intent to go another activity.
But the problem is on that RecyclerViewClickListener.RecyclerTouchListener there is one image view also and I implemented onClickListener of that InmageView so that it will open one dialog box.
Now, when I click on that ImageView, 2 things happened at same time.
1.Opened Dialog box
2.It is going to another activity also, because of Intent.
How can I fix?
Try this
holder.hamburgerMenu.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
if (menuInterface != null) {
menuInterface.onClickHamburger(position);
}
return true;
}
return false;
}
});
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;
}
});
I am currently trying to detect an ongoing touch event in my Android app.
In detail I want to recognize within a fragment whether the user touches any part of the app's screen.
Android's OnTouchListener works as expected except if the touch event lasts longer than a few seconds without moving.
For example the first 1-2 seconds of touch are being detected but everything after won't.
Is there something like an "OnPressListener" or a workaround?
If it's a well defined gesture you are trying to detect, have a look at GestureDetector.
http://developer.android.com/reference/android/view/GestureDetector.html
You can use
aButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
Toast.makeText(getApplicationContext(), "Long Clicked " ,
Toast.LENGTH_SHORT).show();
return true; // <- set to true
}
});
on your aButton and if you are using API < 19 you have to add
android:longClickable="true"
Attribute to your aButton in layout xml.
I finally found it out.
The solution is to use a simple OnTouchListener:
private boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
pressed = true;
} else if ((action == MotionEvent.ACTION_UP)
|| (action == MotionEvent.ACTION_CANCEL)) {
pressed = false;
}
return true;
}
I have to develop one apps, in that i want to fire one action, like button is vibrate contentiously during press,i leave press it stop vibration.
In onTouch - event occur only during press,but when i press button contentiously -> event occur contentiously
like i press button for 1 minute it vibrate for same time,if leave press it stop.
i don't know which method used for this, so, please any one help me to do like this.
my code is below to do that : but it not work for continuous action during press (using thread it work for vibrate only, if i join more code with that it give error see in following.
**Edit : **
i got solution for vibrate using like below but when i write code (below vibrator.vibrate(100); ) for some animation that continuous during press then i got error : Only the original thread that created a view hierarchy can touch its views. i also try with runOnUIThread also but with that it not work.
img_laser_ballpen.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
MainActivity.this.vibrating = true;
img_laser_light.setVisibility(View.VISIBLE);
new Thread(new Runnable() {
public void run() {
while (MainActivity.this.vibrating) {
// ADD CODE FOR MAKING VIBRATION
vibrator.vibrate(100); // it works properly
//Animation shake = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.shake);
//img_laser_light.startAnimation(shake); //if it open then give error
}
//
}
}).start();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
MainActivity.this.vibrating = false;
img_laser_light.setVisibility(View.INVISIBLE);
}
return vibrating;
}
});
You could like this
Button btn = (Button) findViewById(YOUR_BUTTON_ID);
btn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event){
if(event.getAction() == MotionEvent.DOWN){
// Start vibrating
}else if (event.getAction() == MotionEvent.UP){
// Stop vibrating
}
}
});
This answer has a good example of catching these events; namely - you should use the OnTouchListener instead of OnClickListener.
// this goes wherever you setup your button listener:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// START VIBRATE HERE
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// STOP VIBRATE HERE
return true;
}
}
};
Edit:
You should return true after handling your vibrate so that other events are fired for this view. From the documentation:
onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.
You could opt for the following:
Button btn = (Button) findViewById(YOUR_BUTTON_ID);
boolean vibrating = true;
btn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch (View view, MotionEvent event){
if(event.getAction() == MotionEvent.DOWN){
YOUR_CLASS_NAME.this.vibrating = true;
new Thread(
new Runnable(){
public void run(){
while(YOUR_CLASS_NAME.this.vibrating){
//ADD CODE FOR MAKING VIBRATION
}
}
}
).start();
}else if (event.getAction() == MotionEvent.UP){
YOUR_CLASS_NAME.this.vibrating = false;
}
}
});
I want to disable on touch listener when the animation is being played. I have used WebView for this and the animation is running successfully. But the problem is until and unless 1 loop is completed I don't want to enable the touch listener. After a loop is completed I want to enable the touch listener so that the loop will be played again. I am using on touch motion event but unable to do what i need.
GIFWebView view = new GIFWebView(this, "file:///android_asset/imageedit_ball.gif");
setContentView(view);
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction()==MotionEvent.ACTION_DOWN) {
// view.loadUrl("file:///android_asset/imageedit_ball.gif");
GIFView view1 = new GIFView (this, "file:///android_asset/imageedit_ball.gif");
setContentView(view1);
view1.setOnTouchListener(this);
}
return true;
}
you just need to keep a flag variable whether animation is on or it is stopped.
public boolean isAnimationOn = false;
make this variable true when animation starts and false at animation finish.
public boolean onTouch(View v, MotionEvent event){
if (isAnimationOn)
return false;
else
return true;
}
Easy way to implement that is add transparent layout over it (add it in your xml fill parent height and width).
in animation start
transaparentlayout.setClickabe(true);
in animation end
transaparentlayout.setClickabe(false);
very simple.