I try to display some text on the screen for a certain period of time, like a toast, but with the ability to specify the exact time that it is on the screen. I think a warning dialog may work for this, but I cannot figure out how to automatically cancel the dialog.
Can you offer an alternative to toast notifications in which I can indicate the exact time it was displayed?
Thanks!
static DialogInterface dialog = null; public void toast(String text, int duration) { final AlertDialog.Builder builder = new AlertDialog.Builder(gameplay); LayoutInflater inflater = (LayoutInflater) gameplay.getSystemService(gameplay.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.tutorial, (ViewGroup)gameplay.findViewById(R.id.layout_root)); ((TextView)layout.findViewById(R.id.message)).setText(text); builder .setView(layout); builder.show(); if (dialog!=null){ dialog.cancel(); dialog.dismiss(); } dialog = builder.create(); Handler handler = null; handler = new Handler(); handler.postDelayed(new Runnable(){ public void run(){ dialog.cancel(); dialog.dismiss(); } }, 500); }
source share