Java: How to Add Toast to an App

Header

Today’s tutorial is a continuation of How to Make a Genuinely Basic App.  All we’re doing is adding a method to give a new button a job. This is the coding equivalent to dipping a toe in the water so as to not become overwhelmed at the sheer horror of all the java inside a basic project in Android Studio.

The method of choice for our experiment—toast.

If you’re like me, you might need a quick review on java starting with…

What is a method?

It’s just a piece of code that’s all bundled together. Here’s what it looks like:

visibility return_type method_name ( parameter_Type parameter_Name )
{ // Stuff goes in here… }

It’s not supremely important that you have a handle on all of these pieces, but let’s skim over the highlights.

Visibility tells us the kind of access the method should have. For our purposes, we’ll only be looking at public methods.

Return type is dependent on whether your method is going to spit anything out afterwards (a boolean, integer, string, things like that). Methods that don’t return data need to have void here.

method_Name is whatever you want to call it. Just remember, upper and lower case characters are different in Java. hImYnAMEis is different from hiMyNameIs.

Parameters are things that you stuff inside your method to make it work. These are a little tricky because you have to start with the data type of the parameter and then the name of the parameter. If you have more than one of these, separate out each with a comma.

Now, let’s talk about toast.

What the heck is toast?

Toast is a little message that pops up from the bottom of the screen in your app. We’re going to attach this to a button. So, first, fire up that Riddler app, go into fragment_first.xml and drag a new button onto the layout.

Name the button, fix its hardcoded text, and give it some layout constraints (graphic below if you need a refresher on how that’s done).

This slideshow requires JavaScript.

Next, we’re going to tell Android Studio where to find the method we’ll be writing for that function. While still in fragment_first.xml, switch into Code view. Scroll down to your newly created button. It should be the last one, but just to confirm, take a look at the android:id. That’s what you named it a while ago.

This slideshow requires JavaScript.

At the very bottom, you’ll add a line that says:

android:onClick=”showToast”

Keep in mind, it doesn’t have to be showToast. This is the name of the method that’s going to run when the button gets pushed. That’s just what I’m calling it, but you can name it anything.

Once that’s in place, you may have to position your cursor right inside the method name, but eventually, a little red light bulb is going to appear on the left hand side. Click it.

Step_7

From the drop down menu, select Create onClick event handler. MainActivity should already be highlighted, so just hit okay. This will then shoot you over to the MainActivity.java file.

THIS is where we’ll actually add the method.

When you get into MainActivity.java, Android Studio will have a showToast method ready to roll at the bottom. You just have to stuff it with something to do.

So, here’s the template for making toast.

public void showToast(View view) {
Toast toastName = Toast.makeText(context, text, duration);
toast.show();
}

Context tells Android Studio where you want this to apply. As a shorthand, you can just plop in the word “this” to make it whatever context the method was triggered in. Text needs to be a string. The app will work if you hardcode the string in the method, but you can also pop the string inside the strings.xml file and reference your text as R.string.whatever_you_named_it. Duration has to be either LENGTH_SHORT or LENGTH_LONG.

For our implementation of it, I went with:

Toast toast = toast.makeText(this, R.string.toast_message, Toast.LENGTH_SHORT);
toast.show();

This slideshow requires JavaScript.

If all has gone according to plan, you’ll see this when you click your new button.

It_Works

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s