You can be forgiven for thinking; what's up with Karl? Has he won the lottery? Has somebody recently got hitched?
Or, maybe, being the foodie he claims to be; he wants to introduce us to his latest variety of toast?
I can assure you within this article, there will be no mention of jellied, buttered, cheese, alba, cinnamon or peanut butter toast. However, for the food artisans amongst you; this one is for you!
Right! The Toast I refer to is one of a programming kind; Android programming to be precise.
I would like to spend the rest of this article introducing you to the basics of this programming feature and roundup with your very own custom Toast.
I Never Knew Android Made Toast
Android Toasts are short message prompts displayed to users about a current operation. They are typically presented at the bottom of the screen for a couple of seconds prior to fading away. A toast's display position can also be changed by altering its center of gravity; which I'll explain further, later in the article.
This tutorial has been carried out using the following tools:
- Mac OS X 10.9 (Mavericks)
- JDK 1.8
- Android Studio 2.1.1
- LGE LG-D855 Android 5.0, API 21
Fig. 1: Toast
1. Let's Make a Toast
Toasts are typically created by calling the makeText() method; which instantiates a Toast object.
Toast toast = Toast.makeText(MainActivity.this, "I'll like to make a toast!", Toast.LENGTH_LONG);
The following parameters are accepted by the method.
- (Context) - The application context.
- (CharSequence) - The message to be displayed.
- (int) - How long to display the message; e.g; Toast.LENGTH_LONG
The show() method is called to display the Toast message.
toast.show();
Once executed, you should see the same toast message as shown in fig. 1; which is displayed whenever the Toast button is pressed. A complete listing of the code is provided at the end of this tutorial.
Fig. 2: Gravity
2. Toast's Center of Gravity
As I previously mentioned you can also customize the display location of your Toast by setting the setGravity() method; as shown below.
toast.setGravity(Gravity.CENTER,0,0);
The following parameters are accepted by the method.
- (int) - A Gravity constant; specifying a new screen location
- (int) - X-coordinate offset relative to gravity constant.
- (int) - Y-coordinate offset relative to gravity constant.
Once executed, your new toast message will be centered and displayed in the middle of the screen, with no offsets.; as shown in fig.2.
Fig. 3: Custom
3. Custom Toast
If the default toast message is not frilly enough for you, Android have kindly provided us with the ability to spice things up a little and create your own look and feel.
You would have to create your own custom layout XML file; which in turn will be inflated within your code into a View object; which will then be passed into the setView() method of your Toast object.
Let's create a new layout file called frilly_toast_layout.xml.
As described above, you would then inflate the above layout file within your code and pass the instantiated View object into the Toast.setView() method; as shown below.
LayoutInflater inflater = getLayoutInflater(); View toastLayout = inflater.inflate(R.layout.frilly_toast_layout, (ViewGroup) findViewById(R.id.custom_toast_layout)); Toast toast = new Toast(getApplicationContext()); toast.setDuration(Toast.LENGTH_LONG); toast.setView(toastLayout); oast.show();
Once compiled and executed; your new frilly toast prompt; should look like the one shown in Fig.3.
A Final Toast
I would like to make a final toast to You! For not only making it to the end of this tutorial but hopefully, you are now a little more knowledgeable about all things Toast (the Android Programming kind, that is)!
- Key methods to remember are Toast.makeText() for instantiating a new Toast object.
- Once relevant Toast object variables have been set; the message can be displayed by calling the Toast.show() method.
- Unlike John Mayer's Gravity song, Toast.setGravity() works with you and not only can it raise your toast up; but it can also bring it down.
- If you are feeling adventurous; jazz things up a little by creating your very own customized toast layout; inflate it and then display it!
The three main files used throughout this tutorial have been listed below.
That's all for now, as I sign off to enjoy my toasties (I'll leave you to work out which one).