Setting same animation to many Buttons - android

I am using custom animations at ACTION_DOWN and ACTION_UP events of my button.
However, right now, I am manually setting OnTouchListener to each Button manually with code like this:
myButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View yourButton, MotionEvent theMotion) {
Animation animation;
switch (theMotion.getAction()) {
case MotionEvent.ACTION_DOWN:
animation = AnimationUtils.loadAnimation(NickNameScreenActivity.this, R.anim.button_down);
myButton.startAnimation(animation);
break;
case MotionEvent.ACTION_UP:
animation = AnimationUtils.loadAnimation(NickNameScreenActivity.this, R.anim.button_up);
myButton.startAnimation(animation);
break;
}
return false;
}
});
This is cumbersome as I have to repeat this for all Buttons.
Is there a way to set it automatically for all buttons or a tidy XML way of doing this?

You can write a custom view extending Button and use it in xml. Maybe you can replace NickNameScreenActivity.this with getContext().

Related

Continuous touchListener (Piano like interface)

So I am trying to implement a piano like interface but with textviews instead of keys. When I slide from textView to textView it should trigger something. I tried using an onTouchListener instead of an onClickListener, but it doesn't seem to work. It only triggers the first textView that I touch. Anyone know how to do this?
tv.setOnTouchListener(new CustomTouchListener());
public class CustomTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
// Action you you want on finger down.
Toast.makeText(this,"onTouch",Toast.LENGTH_LONG).show();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
// Action you you want on finger up
break;
}
return false;
}
}
Is this exact what you need?? Please share some code of you which will make more easier to fix problem.

Android - OnTouchListener not firing if already pressing screen

I have a RelativeLayout with a single ImageView inside of it. The ImageView has an OnTouchListener attached to it which is working perfectly fine if I just press the ImageView. The problem I'm having though is that if I press and hold in the empty space outside of the ImageView and then try to press the ImageView the touch listener doesn't fire. It seems like the layout is absorbing the touch events somehow. Any ideas on how I could fix this?
The action defined by you here is a Multi-touch gesture. Go through this doc to know how to handle this type of actions.
Also take a look at the ACTION_POINTER_DOWN action define in MotionEvent. This is the event that will get called when additional fingers come down.
you mout use this code:
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//your code
break;
case MotionEvent.ACTION_UP:
//your code
break;
case MotionEvent.ACTION_MOVE:
//your code
break;
default:
//your code
break;
}
return true;
}
});

Android onTouch Listener Background Color Change

I'm using onTouchListener to change the background color when the user's finger is down and change back to default when the user's finger is up. The problem is that when the user's finger is down the background color changes but if the user doesn't takes the finger up and starts scrolling the background color doesn't change to default. Please help.
Below is my code:
V.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
V.setBackgroundColor(Color.BLUE);
break;
case MotionEvent.ACTION_UP:
V.setBackgroundColor(Color.BLACK);
break;
}
return true;
}
});
I've also tried to return false but the result is the same
There are a few different approaches you can use. You can look at MotionEvent.ACTION_MOVE and act when you receive that in your onTouch. You can look at MotionEvent.ACTION_OUTSIDE
and see if they have left the region your are checking.
You can also put a listener on your scroll View and change the background when it moves. Synchronise ScrollView scroll positions - android
Alternately, you could use a gestureListener and detect that.

On pressed button block the other buttons

How could I make my button to do the on click action even if the button is pressed and the finger is still on the screen (the button is not released). Or I would accept the solution that when a button is pressed and is not release, the other buttons on the layout to not be blocked.
Instead of using an OnClickListener consider using an OnTouchListener. You can then detect when the user's finger touches:ACTION_DOWN and releases ACTION_UP the button.
A method like this would probably work fine:
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//DISABLE THE OTHER BUTTONS
break;
case MotionEvent.ACTION_UP:
// RE-ENABLE THE OTHER BUTTONS
break;
}
return true;
}
One option would be to use the onmousedown event rather than the onclick event in your javascript:
myButton.onmousedown = myOnClickFunction;

Custom view with button like behavior on click/touch

I have created a custom View which I usually attach an onClickListener to. I'd like to have some button like behavior: if it is pressed, it should alter its appearance which is defined in the onDraw() method. However, this code does not work:
//In my custom View:
#Override
protected void onDraw(Canvas canvas)
{
boolean pressed = isPressed();
//draw depending on the value of pressed
}
//when creating the view:
MyView.setClickable(true);
pressed always has the value false. What's wrong?
Thanks a lot!
hey buddy,your fault is you are not implementing click or touch evnt for ur custom view.thr is no click evnt for view.you can use touch event instead of this:so below code work 4 u:
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
});
in this code use action_up for click and you get it worked for you
have you considered just using a button to do what you want? you could use ToggleButton and write a short selector in xml that will allow you to specify an image to use when pressed or not. this question may be of some help to you.
To make your new custom button draw on clicks don't forget to invalidate the form as necessary. This is a little gotcha. E.g.
#Override
public boolean onTouch(View v, MotionEvent event)
{
/* Parse Event */
this.invalidate();
return true;
}

Categories

Resources