Display Toast messages at different positions

We can apply Gravity to the toast message to display it at various positions on the screen. Below are codes provided for displaying a Toast message at TOP, BOTTOM, RIGHT, LEFT, CENTER, TOP LEFT, and BOTTOM RIGHT positions.

// Display Toast at TOP
Toast toast1 = Toast.makeText(MainActivity.this, "This is my toast message.", Toast.LENGTH_SHORT);
toast1.setGravity(Gravity.TOP, 0, 0);
toast1.show();

// Display Toast at BOTTOM
Toast toast2 = Toast.makeText(MainActivity.this, "This is my toast message.", Toast.LENGTH_SHORT);
toast2.setGravity(Gravity.BOTTOM, 0, 0);
toast2.show();

// Display Toast on RIGHT side
Toast toast3 = Toast.makeText(MainActivity.this, "This is my toast message.", Toast.LENGTH_SHORT);
toast3.setGravity(Gravity.RIGHT, 0, 0);
toast3.show();

// Display Toast on LEFT side
Toast toast4 = Toast.makeText(MainActivity.this, "This is my toast message.", Toast.LENGTH_SHORT);
toast4.setGravity(Gravity.LEFT, 0, 0);
toast4.show();

// Display Toast in CENTER
Toast toast5 = Toast.makeText(MainActivity.this, "This is my toast message.", Toast.LENGTH_SHORT);
toast5.setGravity(Gravity.CENTER, 0, 0);
toast5.show();

// Display Toast on TOP LEFT
Toast toast6 = Toast.makeText(MainActivity.this, "This is my toast message.", Toast.LENGTH_SHORT);
toast6.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast6.show();

// Display Toast at BOTTOM RIGHT
Toast toast7 = Toast.makeText(MainActivity.this, "This is my toast message.", Toast.LENGTH_SHORT);
toast7.setGravity(Gravity.BOTTOM|Gravity.RIGHT, 0, 0);
toast7.show();

The method used for displaying Toast at different positions is .setGravity(int gravity, int xOffset, int yOffset).

9 thoughts on “Display Toast messages at different positions”

  1. Simply want to say your article is as amazing. The clarity in your post is just excellent and i can assume you are an expert on this subject.

    Fine with your permission let me to grab your feed to keep up to
    date with forthcoming post. Thanks a million and please carry on the
    enjoyable work.

  2. I believe that is one of the so much important information for me.
    And i’m satisfied studying your article. However should statement on few normal things, The web site style is great, the articles is
    actually great : D. Excellent process, cheers

  3. Hello there! Quick question that’s entirely off topic.
    Do you know how to make your site mobile friendly?
    My blog looks weird when viewing from my iphone. I’m trying
    to find a template or plugin that might be able to correct this
    problem. If you have any recommendations, please share.
    Cheers!

  4. My programmer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the costs.

    But he’s tryiong none the less. I’ve been using Movable-type on several websites for about
    a year and am anxious about switching to another platform.

    I have heard great things about blogengine.net. Is there
    a way I can transfer all my wordpress content into it? Any help would be really appreciated!

Leave a Reply