There is a gButton button and there is an onTouch listener:
gButton.setOnTouchListener (new View.OnTouchListener () {
public boolean onTouch (View v, MotionEvent event) {
if (event.getAction () == MotionEvent.ACTION_DOWN) {
...
} else if (event.getAction () == MotionEvent.ACTION_UP) {
...
}
return false;
}
});
Something is done with ACTION_DOWN and ACTION_UP (pressing the button / releasing), but the pressing event occurs only when the button is touched, and if the button was pressed and you take your finger away, nothing will happen (there is no finger tap)
I tried to add below, after ACTION_DOWN, other "else if" conditions like ACTION_OUTSIDE (I took everything from the page: https://developer.android.com/reference/android/view/MotionEvent.html), but nothing happened. And yes, I used If, not Switch
I need to make sure that when I touch the button (if the finger is in the button area at all, even if the first touch was somewhere in the empty area and the finger was moved to the button), some actions were performed (for example, a Toast method with the text "On "), when you release your finger from the button area or when you move your finger from the button area, other actions were performed (Toast with the value of Off)
I tried to do this with one of the answers to a similar question by correcting it in my code: Cancel button press if user moves finger off button, however, an error prntscr.com/oghnm2 is displayed, and I do not know where and how to get L, R, T, B, except getTop();
I hope this might help,here i had checked mouse pointer location with the btn coordinates, if its location within range printed logs otherwise printed out of area.
btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(v.getLeft()<=event.getX() && v.getRight()>=event.getX() && v.getTop()<=event.getY() && v.getBottom()>=event.getY()) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
Log.d("LOG", "pressed");
if (event.getAction() == MotionEvent.ACTION_UP)
Log.d("LOG", "released");
if (event.getAction() == MotionEvent.ACTION_MOVE)
Log.d("LOG", "moving");
}
else
{
Log.d("LOG", "out of area");
}
return false;
}
});
Related
I have a TextView, with a onLongClickListener and OnClick event, on holding TextView, its color changes to red, and on releasing, its color is supposed to change to white.
Problem:
When I hold the TextView and move my finger outside of it while holding, and then leave my finger, its color does not change to white.
XML
<TextView
android:layout_width="match_parent"
android:text="hello"
android:textColor="#ffff"
android:id="#+id/timer"
android:layout_height="wrap_content"
/>
Java
final TextView t1 = (TextView) findViewById(R.id.timer);
t1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t1.setTextColor(Color.WHITE);
}
});
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
t1.setTextColor(Color.RED);
return false;
}
});
View.OnClickListener - Interface definition for a callback to be invoked when a view is clicked.
View.OnLongClickListener - Interface definition for a callback to be invoked when a view has been clicked AND held.
So what you are saying is 100% true.It should be red because its being been clicked and held as the way you did.
But when i hold the text view and move my finger outside the text view
while holding , and then leave my finger , it not changes its color to
white
You have given color white to text view when it gets only clicked !! If you want to get that white like you said(when clicked and held), you need to set the white color in OnLongClickListener
To the point if you want to detect your views touch and release and change colors related to that then you need to use OnTouchListener instead of clickListeners
View.OnTouchListener - Interface definition for a callback to be invoked when a touch event is dispatched to this view. The callback will be invoked before the touch event is given to the view
t1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
t1.setTextColor(Color.RED); // pressed state
break;
case MotionEvent.ACTION_UP:
t1.setTextColor(Color.WHITE); // Released state
break;
}
return true;
}
});
Assign a onTouch listener and look for MotionEvent.ACTION_DOWN and MotionEvent.ACTION_MOVE:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Construct a rect of the view's bounds
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
// User moved outside bounds
t1.setTextColor(Color.WHITE);
}
}
return false;
}
Use OnTouchListener that way you can register touch down and up events. MotionEvent case MotionEvent.ACTION_DOWN: will set the color to red when the user touches down on your TextView, and case MotionEvent.ACTION_UP: will set the color to white when the user lifts their finger off the TextView.
final TextView t1 = (TextView) findViewById(R.id.timer);
t1.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
t1.setTextColor(Color.RED);
break;
case MotionEvent.ACTION_UP:
t1.setTextColor(Color.WHITE);
break;
}
return true;
}
});
I am having this problem when I try to change the source/resource of a image(a button) when is pressed and released.
Btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
/*if(event.getAction() == MotionEvent.ACTION_DOWN) {
ImageBtn.setImageResource(R.drawable.btn1);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
ImageBtn.setImageResource(R.drawable.btn0);
}*/
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
ImageBtn.setBackgroundResource(R.drawable.btn1);
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
ImageBtn.setBackgroundResource(R.drawable.btn1);
return true; // if you want to handle the touch event
}
return false;
}
});
This is the code, I tried what is in the comment too, but no result. I do have to mention that the button changes its resource just when touching it, but after releasing it, it doesn't change.
The button listener is on a relative layout(also defined as a relative layout in the xml code), in java however is defined as a button.
The image of the button doesn't change from when you touch it to when you release it because you are setting the same background resource.
Both cases contain:
ImageBtn.setBackgroundResource(R.drawable.btn1);
Set one of them to a different image.
So, basically I have a clickable ImageView with a listener:
imageView1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//do something while pressed
}
if(event.getAction() == MotionEvent.ACTION_UP){
//when finger is lifted
}
return true;
}
});
It works, but the problem is that there is an annoying delay between the moment when I press the ImageView and when the code is actually executed.
Is there any way to fix it? I've read about a prepressed state that may fix it, can anyone explain it to me?
I'm coding a music app with buttons. I want to make music sound while a button is pressed but stop it when it's released. Also I want to play the music in a constant loop without separation between loop times.
Now when I press the button music starts to play but when I release the button it stills playing untill the end of the file.
This is the code:
#Override
public boolean onTouch(View v, MotionEvent event) {
MediaPlayer do2n = MediaPlayer.create(this, R.raw.do_leg);
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if(v.getId()==R.id.dor){
do2n.start();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if(v.getId()==R.id.dor){
if (do2n != null)
do2n.release();
}
}
return true;
}
Well, you have actually created 2 instances of MediaPlayer since onTouch is called twice: once for key down and once for key up. So the first time you're creating the player and then you lose the reference. The second time onTouch is called, for ACTION_UP you're creating a new MediaPlayer object, but that object is a different one than the previous created with ACTION_DOWN so calling stop on this newly created object has no effect.
So you could instantiate the player as a class variable. Something like below:
private MediaPlayer do2n;
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (v.getId() == R.id.dor) {
do2n = MediaPlayer.create(this, R.raw.do_leg);
do2n.start();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (v.getId() == R.id.dor) {
if (do2n != null) {
do2n.stop();
do2n.release();
do2n = null;
}
}
}
return true;
}
#Override
protected void onDestroy() {
super.onDestroy();
/**
* The activity may be destroyed if you receive a long phone call while
* keeping the button pressed so it's safe to do this
*/
if (do2n != null) {
do2n.stop();
do2n.release();
}
}
I am looking for a way to call a function when a button is pressed and held for one second. When the button is released another function should be called.
I was thinking about an onLongClickedListener but this won't work well for me since the text that is going to be displayed would stay too long or short.
I am thinking a TouchListener could help me because the Action_Up event would give me the option to let the text dissapear when the button isn't pressed anymore. The Action_down event gives me when the button is pressed and I thought I could start a timer when the button is pressed, wait a second, check again if the button still is pressed and then call the function (show the text).
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
// the button. I set bFertig.setOnTouchListener(this); in onCreate
case R.id.bFertig:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// everything works up to here
waitTimerNotif = new CountDownTimer(1000, 500) {
#Override
public void onTick(long arg0) {}
#Override
public void onFinish() {
// TODO Auto-generated method stub
// here im checking if the button still is pressed
if (bFertig.isPressed()) {
// It never goes into here
ShowNotifBox("Fertig", "fertig", false, false,false);
}
}
}.start();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
DissapearNotifBox();
Log.d("Debug", "Button released");
}
break;
}
return true;
}
For the Button in xml:
<Button
android:id="#+id/bFertig"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/fertigbutton"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="18.5dp"
android:clickable="true"/> <!--Googleing suggested I need this for isPressed() to work but it didnt help
Any ideas what I did wrong or why this isnt working? Thanks!
You're returning true, which tells the system that you are handling the touch events, which means the button is not, which means it's probably not updating its pressed state. Try returning false.
Why not use the setOnLongClickListener function?
This should solve your issue.
An example can be find here: setOnLongClickListener