

Notice that we have to first pull out a LinearLayout because that is the view inside our Toast. Unfortunately, unless we know the exact size of our toast text all the time, it’s pretty likely we’ll run into a situation (like this one) where it doesn’t look good.Ĭentering the text is pretty simple and requires us to pull out the TextView that is actually being used to display the text and then set it’s gravity like this: If there was a little less text, this might look fine as it would almost appear centered. This results in a toast which looks like this: Finally we call show() on the returned Toast object. Lastly we have the length of time to display the toast (which we’re using the Toast.LENGTH_SHORT constant for). the Activity), then followed by the text to display. Here’s the code I can use to show a toast from an Android Activity: Thankfully these are all easy tasks to accomplish.Ĭreating a toast is a very easy thing to accomplish. Then I decided I wanted the toast centered on the screen. What I wanted was for the text to be centered in the toast. This wasn’t really the experience I wanted the user to have. By default, a toast will appear at the bottom of the screen with no special formatting consideration given to the text (i.e. This has been fixed thanks to was recently working on an Android sample where I wanted to display a toast in the app. Update: I was not originally checking to make sure there were any child views before getting the TextView.
