Recipe Using Toast to Show a Brief Message on the Screen

The Toast method has been introduced in the previous chapter in a compact form: Toast.makeText(this, "text", Toast.LENGTH_SHORT).show(); It can also be written as a multiline command:

Toast tst = Toast.makeText(this, "text", Toast.LENGTH_SHORT); tst.show();

This form is useful when the text needs to be shown multiple times, as the instance in the first line can be reused.

Two other uses for the multiline Toast command are to reposition the text location or to add an image. To reposition the text location, or to center the Toast in the screen display, use setGravity before calling the show() method:

tst.setGravity(Gravity.CENTER, tst.getXOffset() / 2, tst.getYOffset() / 2);

To add an image to a Toast, use the following:

Toast tst = Toast.makeText(this, "text", Toast.LENGTH_LONG);

ImageView view = new ImageView(this);

view.setImageResource(R.drawable.my_figure);

tst.setView(view);

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment