Search code examples
androidtoastduration

How to set a duration between two toast?


I implemented two toast event by onClick() and onTouch(). I want to see every toast event, but it is two fast.

How can I set a duration between two toast events? My code is too short, below.

    @Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.iv_like:
            Toast.makeText(MainActivity.this, "I love Irene", Toast.LENGTH_SHORT).show();
            break;
        case R.id.iv_share:
            Toast.makeText(MainActivity.this, "Together Irene", Toast.LENGTH_SHORT).show();
            break;
        case R.id.iv_photo:
            Toast.makeText(MainActivity.this, "Click Irene", Toast.LENGTH_SHORT).show();
            break;
    }
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {

    switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Toast.makeText(MainActivity.this, "down Irene", Toast.LENGTH_SHORT).show();
            break;
        case MotionEvent.ACTION_UP:
            Toast.makeText(MainActivity.this, "up Irene", Toast.LENGTH_SHORT).show();

            break;
    }

    return false;
}

Solution

  • Your code makes me smile.

    OnTouch events are very fast. It will cause many Toast to show. Although all Toast are queued up but you will not be able to measure events matched with fired time.

    Instead You can use Logs like this.

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
    
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.d("myClass", "MotionEvent.ACTION_DOWN");
                break;
            case MotionEvent.ACTION_UP:
                Log.d("myClass", "MotionEvent.ACTION_UP");                
                break;
        }
    
        return false;
    }
    

    You will get these logs in your Logcat in Android Studio, or press alt+6