Toast.makeText(...).show() is sometimes misaligned

39,347

Solution 1

you can try this:

Toast bread = Toast.makeText(getApplicationContext(), R.string.message, Toast.LENGTH_LONG);
bread.show();

Solution 2

bread.setGravity(5,5,5);

try playing around by changing those values till you get exact location.

But using Gravity.CENTER and other constants is the prefererrd way..

Solution 3

If you create the toast from inside an activity you should use: this or MyActivityName.this as the context parameter.

Solution 4

Try using,

<supports-screens
   android:largeScreens="true"
   android:normalScreens="true"
   android:smallScreens="true"
   android:anyDensity="true" /> 

in your manifest file.

Share:
39,347
Andrew Wyld
Author by

Andrew Wyld

Programmer, voice artist, musician, sound of the dying bass player in Han Dold City in the fourth radio series of the Hitch-Hiker's Guide To The Galaxy, sometime graphic artist and maniac.

Updated on June 14, 2020

Comments

  • Andrew Wyld
    Andrew Wyld almost 4 years

    I am using Toast.makeText to display results from dialogs and having a slightly odd problem: the text is displaying above the frame that should hold it, like this:

    The message is misaligned with the frame. Please align it better. [_________________________________________________]

    I'm generally using code looking like

    Toast bread = Toast.makeText(getContext(), R.string.message, Toast.LENGTH_LONG);
    bread.show();
    

    from a dialog. I've heard bad context can sometimes cause inflation problems but getOwnerActivity() is returning null, so that's out. In any case, I would have thought getContext() would supply the context passed in at construction time, which is the activity anyway.

    Any suggestions?

  • Andrew Wyld
    Andrew Wyld over 12 years
    Not available from within the Dialog class but would getContext().getApplicationContext() work?
  • Vito Gentile
    Vito Gentile over 12 years
    It was written as a comment of question before you post this answer
  • Andrew Wyld
    Andrew Wyld over 12 years
    @CrimsonChin Sorry, I would have marked it straight away but the thing makes you wait ten minutes and I forgot.