Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do margin values for a Toast do?

The Android Toast class provides methods to get and set margins. I'm pretty sure they refer to the outside margins for the whole toast message. Since Toast messages float over the UI, why exactly are these Margins necessary?

I tried looking over the SDK reference as well as searching the Internet. The closest thing to a solution I found was a one line suggestion that both margins and offsets allowed control over the positioning of a Toast. Why would I need two methods (albeit conceptually different, since margins allow specification in terms of percentage container width), to control the positioning of the Toast?

Just to be sure, these margins don't work like padding for other layouts does it? That would not make sense, but I'd like to be clear.

In sum, I want to know why margins are needed, what margins do, and the use-cases for margins versus offsets, that is, when should I use margins, when should I use offsets, and why?

Update:

I haven't managed to find any answers yet. I've tried using margins versus using offsets in code and found that they seem to offer two different paradigms of positioning the Toast. The design intent (why two methods), when I should use one method versus the other (or at least examples of when one was found more useful than the other by other programmers/UI designers), and even the exact operation (do margins "center" the toast inside them? are margins applied against the closest container edges?) of these methods remain unclear.

Update 2:

I looked at the docs closely, and also at some code for Toast.java that Google pointed me to. What became apparent is that the Toast is contained within a Window (Activity window?) and that it might be an overlay. The WindowManager.LayoutParams class has also provided further clues. I've decided to play a bit more with using Toasts, offsets and margins, as well as look at the code from the AOSP to get a clearer understanding.

I'll update here as I discover more.

like image 657
batbrat Avatar asked Jan 11 '23 04:01

batbrat


1 Answers

I believe the margins determine how far the toast appears from the screen edge. You can also call setGravity() to change which side of the screen it appears on, and then use the margins to control how far away it is from the side of the screen. So for example:

myToast.setMargin(10, 20);

Will create a toast that has 10% of the containers width between the edge and the container, and 20% of the containers height between the toast and the containers edge

To create a toast that is in the top left corner of the container, with a 10 pixel margin on the left and 20 pixel margin from the top:

myToast.setGravity(Gravity.LEFT| Gravity.TOP, 10, 20)
like image 151
CurlyPaul Avatar answered Jan 21 '23 09:01

CurlyPaul