Im not Getting the Button Release Toast in OnTouchListners - java

The issue is im not getting the Button release Toast.
i've a simple view in xml on which im performing onTouch.
hidenBtn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
firstTime=System.currentTimeMillis();
Toast.makeText(MainActivity.this, "Pressed", Toast.LENGTH_SHORT).show();
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
Toast.makeText(MainActivity.this, "Released", Toast.LENGTH_SHORT).show();
secondTime=System.currentTimeMillis();
if(secondTime-firstTime>=5000){
//do your actions here,prev,curr are fields in a class
ShowDialog();
}
else{
firstTime=0;
secondTime=0;
}
}
// TODO Auto-generated method stub
return false;
}
});

Change your OnTouchListener like this:
hidenBtn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
firstTime=System.currentTimeMillis();
Toast.makeText(MainActivity.this, "Pressed", Toast.LENGTH_SHORT).show();
return true;
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
Toast.makeText(MainActivity.this, "Released", Toast.LENGTH_SHORT).show();
secondTime=System.currentTimeMillis();
if(secondTime-firstTime>=5000){
//do your actions here,prev,curr are fields in a class
ShowDialog();
}
else{
firstTime=0;
secondTime=0;
}
}
// TODO Auto-generated method stub
return false;
}
});
You are returning false at ACTION_DOWN which means you are not consuming your touch event, so no other steps of the same event are triggered. Therefore you need to return true in the ACTION_DOWN so you can intercept ACTION_UP.

Related

How to implement onKeyDown in Fragment

I change all my activitys to Fragment. I got stuck in the onkeydown part.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (!sector.getText().toString().equals("Sector") && (media.isChecked() || completa.isChecked())) {
siguiente.setEnabled(true);
} else {
siguiente.setEnabled(false);
}
return super.onKeyDown(keyCode, event);
}
You can do this by defining method in your fragment class. For example:
public void onMyKeyDown(int key, KeyEvent event){
//define your statement like
if (Integer.parseInt(android.os.Build.VERSION.SDK) > 5
&& key == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you really want to Exit?")
.setIcon(R.drawable.logo)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}})
.setNegativeButton(android.R.string.no, null).show();
}
}
Call onMyKeyDown method whenever a key-down event is raised in your Activity class. example:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//call fragment method onMyKeyDown(keyCode, event)
return super.onKeyDown(keyCode, event);
}

Android - How do I make a button have a different function after being pressed?

So, In my code the user must answer a quick math question and press a button to answer but for the next question it does not check for the second answer only the first. How could you make it where it then checks for the next answer and not the previous one?
primaryText.setText(Questions[0]);
enter.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v)
{
if(numberVal.getText().toString().equals("14")) {
primaryText.setText(Questions[1]);
}
if(numberVal.getText().toString().equals("5")) {
primaryText.setText(Questions[2]);
}
}
}
);
instead of
OnClickListener
try :
MyBtn1.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_UP){
//do stuff
}
else if(event.getAction() == MotionEvent.ACTION_DOWN){
//do stuff
}
else if(event.getAction() == MotionEvent.ACTION_CANCEL){
//do stuff
}
return true;
}
});
boolean questionComplete = false;
enter.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v)
{
if(!(questionComplete)){
if(numberVal.getText().toString().equals("14")) {
questionComplete = true;
primaryText.setText(Questions[1]);
}
}
else {
if(numberVal.getText().toString().equals("5")) {
primaryText.setText(Questions[2]);
}
}
}
}
);
This would make it so that after the first question is complete, it would display the second question. It would only work for two questions though

How to use an onTouchListener to do two different tasks from the same button

I'm trying to make two buttons that will change add or subtract 1 from a value when tapped, and constantly add or subtract 1 ten times per second while the button is held. I can get the value to be changed when the button is tapped, or when it is held, but I can't get the behavior I want. Here's what I have:
btPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
setTempo(mTempo + 1);
mTempo=mTempo+1;
tvTempo.setText(Integer.toString(mTempo));
return true;
}
return false;
}
});
btMinus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
setTempo(mTempo - 1);
mTempo=mTempo-1;
tvTempo.setText(Integer.toString(mTempo));
return true;
}
return false;
}
});
btPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
if (getActivity() == null)
return;
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
tvTempo.setText(Integer.toString(mTempo));
mTempo++;
if (mTempo > 300)
mTempo = 300;
}
});
}
}, 100, 100);
break;
case MotionEvent.ACTION_UP:
timer.cancel();
}
return true;
}
});
btMinus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
if (getActivity() == null)
return;
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
tvTempo.setText(Integer.toString(mTempo));
mTempo--;
if (mTempo < 1)
mTempo = 1;
}
});
}
}, 100, 100);
break;
case MotionEvent.ACTION_UP:
timer.cancel();
}
return true;
}
});
return rootView;
}
Thanks for your help!
You can implement onKeyListener to determine how long button is held, like in this example:
Android long-touch event
or use onLongClickListener to perform a different function from a regular "click" like here:
On long click delete item

dispatchKeyEvent calling method twice

I've implemented dispatchKeyEvent in my activity to listen to the Enter key being pressed.
The problem is that when i click enter,it calls my method twice ? How can i fix this ?
Thanks,have a nice day !
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
enter();
return true;
}
return super.dispatchKeyEvent(e);
};
Fixed it,done this :
At first i was doing ACTION_DOWN but that was triggering an older problem of mine.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP){
enter();
return true;
}}
return super.dispatchKeyEvent(event);
};

Multiple listeners in Android

How do I set a button to listen for a long press event, where the button is on a view which is already using gesture detector to listen to onFling event?
I have added the code for reference. Please explain how I can implement multiple listeners in an activity. Also, can multiple listeners be implemented for a single view? One of which is a long press listener while the other is a onfling listener
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.knowledge);
summary =(TextView)findViewById(R.id.textSummary);
buckview = (View) findViewById(R.id.bucketView);
ball = (View)findViewById(R.id.meatBall);
gestureDetector = new GestureDetector(new MyGestureDetector());
View mainview = (View) findViewById(R.id.mainView);
/*
//Set touch release listener for bucket view
mainview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP)
{
buckview.setVisibility(RelativeLayout.GONE);
}
return false;
}
});
*/
// Set the touch listener for the main view to be our custom gesture listener
mainview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
ball.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
buckview.setVisibility(ImageView.VISIBLE);
return false;
}
});
ball.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_UP)
{
buckview.setVisibility(ImageView.GONE);
return false;
}
else
return true;
}
});
}
class MyGestureDetector extends SimpleOnGestureListener {
public void onShowPress(MotionEvent event){
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Intent intent = new Intent(Knowledge.this.getBaseContext(), Knowledge.class);
/* If only horizontal allowed
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
*/
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
startActivity(intent);
Knowledge.this.overridePendingTransition(
R.anim.slide_in_right,
R.anim.slide_out_left
);
return false;// left to right swipe
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
startActivity(intent);
Knowledge.this.overridePendingTransition(
R.anim.slide_in_left,
R.anim.slide_out_right
);
return false;// top to bottom swipe
} else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
startActivity(intent);
Knowledge.this.overridePendingTransition(
R.anim.slide_in_top,
R.anim.slide_out_bottom
);
return false;// bottom to top swipe
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
startActivity(intent);
Knowledge.this.overridePendingTransition(
R.anim.slide_in_bottom,
R.anim.slide_out_top
);
return false;
}else
return true;
}
// It is necessary to return true from onDown for the onFling event to register
#Override
public boolean onDown(MotionEvent event) {
return true;
}
}
}
Simply try
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
return false;
}
})
It will work perfectly untill you return true from onLongClick. If you return ture from here then setOnLongClickListener only get invoked. If you return false all Listeners belong to this button will get invoked. In your case both Listensers will get invoked whether you made setOnLongClickListener or setOnTouchListener.
I hope this will help you.

Categories