Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning AndEngine

You're reading from   Learning AndEngine Design and create Android games with the simple but powerful tool AndEngine

Arrow left icon
Product type Paperback
Published in Sep 2014
Publisher
ISBN-13 9781783985968
Length 286 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Martin Varga Martin Varga
Author Profile Icon Martin Varga
Martin Varga
Arrow right icon
View More author details
Toc

Working with toasts


Another way to output text on the screen is using toasts. A toast is a simple message widget that can provide feedback about an operation in progress. It's a core Android widget, not an AndEngine component, but we can nevertheless use it in our game.

The easiest way to show a toast is to use a static method provided by the Toast class. In a general Android app, we can simply call this as follows:

Toast.makeText(activity, "Hello World", Toast.LENGTH_LONG).show();

We can also call a toast inside the activity itself. This can be done as follows:

Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();

The makeText() method creates the Toast object. It takes three parameters: the context (activity), the text to show, and the length of the toast, which can be one of the following two types:

  • Toast.LENGTH_LONG

  • Toast.LENGTH_SHORT

Finally, we call the show() method on the newly created Toast object.

However, if we try to call this object inside the game, it won't work. This is...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image