Answer the question
In order to leave comments, you need to log in
Toast - how to work with it correctly?
I hang a similar code on several buttons, changing the names of the buttons themselves accordingly. Here, if I hold down the button, it displays the text with its description, but if I hold down another button, it again displays the previous description. In general, I click on btn1 displays btn1, I click on btn2 displays btn1, I click again on bnt2 displays btn2.
boolean isDown = false;
Timer timer;
TimerTask task;
int count;
public void start(){
timer.schedule(task, 2,20);
}
public boolean onTouch(final View view, MotionEvent motionEvent) {
switch (view.getId()){
case R.id.imgBntAdd:
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
timer = new Timer();
task = new TimerTask() {
public void run() {
if(isDown){
count++;
}
runOnUiThread(new Runnable() {
public void run() {
if(count >= 100){
int[] pos = new int[2];
btnAdd.getLocationInWindow(pos);
String s = btnAdd.getContentDescription().toString();
Toast t = Toast.makeText(context, s, Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP | Gravity.LEFT, btnAdd.getRight() + 5 ,pos[1] + 20 );
t.show();
count = 0;
isDown = false;
}
}
});
}
};
this.isDown = true;
start();
btnAdd.setBackgroundColor(color1);
this.time1 = System.currentTimeMillis() / 1000;
break;
case MotionEvent.ACTION_UP:
btnAdd.setBackgroundColor(color2);
this.isDown = false;
this.count = 0;
this.time2 = System.currentTimeMillis() / 1000;
if (time1 + 1 - time2 > 0){
startActivityNote();
}
default:
break;
}
break;
Answer the question
In order to leave comments, you need to log in
@kirawa Explain why such difficulties in starting? Maybe I don’t understand something, but why the timer, counters - it’s all sort of implemented directly in the toast.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question