Tuesday, July 17, 2012

Display Android toast at the top of screen

Android offers an easy way to notify the users of your app of important messages in what is called toast.

It is easy to use too.  Just a few line and you are good to go.
Toast toast = Toast.makeText(context, "text to show", Toast.LENGTH_SHORT);
toast.show();
By default, it will show around the lower end of the screen.  However, you can show it anywhere on screen with a bit of code.  One of my toast shows in the upper half of the screen.  The code to do just that looks like this:
Toast toast = Toast.makeText(context, "text to show", Toast.LENGTH_SHORT);
Display display = getWindowManager().getDefaultDisplay();
int height = display.getHeight();
toast.setGravity(Gravity.CENTER, 0, -(height/4));
toast.show();

By the way, be aware that the user will be able to click though the toasts.  The control under that toast will get that click.

No comments:

Post a Comment