Use given code on your project and Show Toast message for long or short time on your android app, So use our simple code on your android project.
How to Do
- Open Android studio app then select your project.
- Simply paste given code on MainActivity.java .
Toast toast=Toast.makeText(MainActivity.this, "Check Internet Connection", Toast.LENGTH_LONG); showMyToast(toast, 8000);
- And defined function’s code for Toast timing .
public void showMyToast(final Toast toast, final int delay) { final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { toast.show(); } }, 0, 8000); new Timer().schedule(new TimerTask() { @Override public void run() { toast.cancel(); timer.cancel(); } }, delay); }
So now see full code example –
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (isConnected()) { //// something /// } else { Toast toast=Toast.makeText(MainActivity.this, "Check Internet Connection", Toast.LENGTH_LONG); showMyToast(toast, 8000); } } public void showMyToast(final Toast toast, final int delay) { final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { toast.show(); } }, 0, 8000); new Timer().schedule(new TimerTask() { @Override public void run() { toast.cancel(); timer.cancel(); } }, delay); } }