Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 409
Andrew Wyld Avatar asked Oct 14 '11 12:10

Andrew Wyld


4 Answers

you can try this:

Toast bread = Toast.makeText(getApplicationContext(), R.string.message, Toast.LENGTH_LONG);
bread.show();
like image 194
Jon Wells Avatar answered Nov 08 '22 03:11

Jon Wells


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..

like image 44
ngesh Avatar answered Nov 08 '22 02:11

ngesh


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

like image 40
kaspermoerch Avatar answered Nov 08 '22 02:11

kaspermoerch


Try using,

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

in your manifest file.

like image 1
Khawar Avatar answered Nov 08 '22 04:11

Khawar