JAVA / Android - Ontouchstart instead of onClick - java

We have an onClick function on our relativeLayout but the method triggers when we release our finger from the screen (onTouchEnd). We want our function to trigger on the moment my finger touches the screen.
android:clickable="true"
android:onClick="click"
public void click(View v){
releaseItem();
}

Use View.setOnToucListener(listener)
yourRelativeLayout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Your stuff here
return true;
}
return false;
}
});

Related

How to check for hold click Android studio/java

I want to make in my app when I hold click it does something and when I release the button it does something else. A variable will change to true when I hold and false when I release. How do I do that
(Android Studio)
Thanks
You have to use TouchListener and MotionEvent class. The MotionEvent class has Fields ACTION_UP, ACTION_DOWN, and ACTION_MOVE. You can use the getAction() method of the MotionEvent class.
Something like this
findViewById(R.id.btn).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// button pressed
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// button relased.
}
return false;
}
});
You can use the onTouchListener for your use case:
boolean isClicking = false;
myView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// the view is pressed
isClicking = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// the view is released.
isClicking = true;
}
return false;
}
});

View setVisibility with GestureDetector in Android

I have a GestureListener with a view. I want to change view visibility based on the GestureListener results. On double tap show the view, on single tap hide it. And I want to show the vie when I'll hold my finger on the display (snapchat like feature).
mDetector = new GestureDetectorCompat(this, this);
mDetector.setOnDoubleTapListener(this);
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mDetector.onTouchEvent(event);
return true;
}
});
Here's my gesture listeners.. On double tap it'll show my view but it keeps the view visible
#Override
public boolean onSingleTapUp(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
text.setVisibility(View.INVISIBLE);
return true;
}
#Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
text.setVisibility(View.VISIBLE);
return true;
}
I had a similar problem and I added
ImageView imgView11 = (ImageView)findViewById(R.id.imagek11d);
just before imgView11.setVisibility(View.INVISIBLE);
and before imgView11.setVisibility(View.VISIBLE);

Using OnLongTouch to move image to string

I'm trying to use this code to set an "ontouchlistener". It says there are no errors in the code but when I try to run it I get a force quit... what is going wrong? any ideas?
final Handler handler = new Handler();
Runnable mLongPressed = new Runnable() {
public void run() {
}
};
#Override
public boolean onTouchEvent(MotionEvent event, View v){
if(event.getAction() == MotionEvent.ACTION_DOWN)
handler.postDelayed(mLongPressed, 1000);
if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() ==
MotionEvent.ACTION_UP))
handler.removeCallbacks(mLongPressed);
return false;
}
In OnTouchListener the event function is:
public abstract boolean onTouch (View v, MotionEvent event)
which is called when a touch event is dispatched to a view. But you are using this one?:
public boolean onTouchEvent(MotionEvent event, View v)
To implement the listener, we can easily do this:
myImgView.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
// put your code here
return false;
}
});

Animation Starts when touch and Stops when UnTouched

So my question is how can I make an animation Starts when touch and Once it's not Touched it plays another animation ..
(sorry for my English..)
main.java :
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView is = (ImageView) findViewById(R.id.img1);
is.setBackgroundResource(R.animator.animup);
final AnimationDrawable animup = (AnimationDrawable) is.getBackground();
final ImageView iv = (ImageView) findViewById(R.id.img1);
iv.setBackgroundResource(R.animator.anim);
final AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
is.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
anim.setVisible(true, true);
anim.start();
//perform your animation when button is touched and held
} else if (event.getAction() == MotionEvent.ACTION_UP) {
anim.setVisible(true, true);
animup.start();
//perform your animation when button is released
}
return false;
}
});
};
;
}
+Another Problem......
EDITED
I reckon this link will help you.
Edit
For implementing touch and release you have to use onTouchListener rather than onClickListener.
Below is the code-
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//perform your animation when button is touched and held
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//perform your animation when button is released
}
}
};
Edit 2
In the else if block you are not setting the visibility of anim to false. I feel that is the problem. Rewrite your code as this-
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
anim.setVisible(true,true);
anim.start();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
anim.stop(); //perform your animation when button is released
animup.setVisible(true,true);
animup.start();
}
}
};

How to set up an onTouchListener that changes an ImageView image resource?

I want to setup up an onTouchListener that changes an ImageViews image resource on ACTION_UP and ACTION_DOWN. Here is my code:
// Setup a basic upDownListener that registers UP and DOWN actions and changes the
// view image resource accordingly
View.OnTouchListener upDownListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// Button was pressed, change button background
v.setImageResource(R.drawable.button_white_pressure);
return true;
} else if(event.getAction() == MotionEvent.ACTION_UP) {
// Button was released, reset button background
v.setImageResource(R.drawable.button_white);
return true;
}
return false;
}
};
This code does not compile because View does not have the method setImageResource, only ImageView. But how do I tell the listener that an ImageView is being used?
Cast the incoming View to an ImageView.
Sample code:
#Override
public boolean onTouch(View v, MotionEvent event) {
ImageView iv = (ImageView) v;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
iv.setImageResource(R.drawable.button_white_pressure);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
iv.setImageResource(R.drawable.button_white);
return true;
}
return false;
}
View.OnTouchListener upDownListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ImageView v = (ImageView) v;
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// Button was pressed, change button background
v.setImageResource(R.drawable.button_white_pressure);
return true;
} else if(event.getAction() == MotionEvent.ACTION_UP) {
// Button was released, reset button background
v.setImageResource(R.drawable.button_white);
return true;
}
return false;
}
};

Categories

Resources