Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (2014) Testing and Debugging Toast Notifications

Toast same as popup?

Am i correct to say Toast is same as popup in Java? I mean if I want to show a popup message in android....

thanks

2 Answers

Kevin Faust
Kevin Faust
15,353 Points

it's actually a bit complicated and it's something you will do later on in the track. I can show you quickly show you how a basic pop up is formed.

Let's look at this:

PopUp.java
//this popup box class is called AlertDialogFragment

public class AlertDialogFragment extends DialogFragment { // must extend this class

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) { //when we make a dialog, run this
        Context context = getActivity();

          //create a new AlertDialog.Builder which is what we call to create a dialog box

        AlertDialog.Builder builder = new AlertDialog.Builder(context) 
                .setTitle("title here") //set a title
                .setMessage("some message text") //set a message

                  //set the button text. the null means that when the popup box is closed, nothing will happen

                .setPositiveButton("button text", null); 

        return  builder.create(); //create the dialog box
    }
}

I added some comments above to give a brief breakdown. this'll generally be what you need when creating a basic pop up box. This will be created in a separate class

In our main, we would use something like this. Usually in a method so we can call popup boxes easily anywhere in our code:

Main.java
        AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.show(getFragmentManager(), "error_dialog");

We create the new popup box object. and then we run the show() method.

We pass in two things: getFragmentManager() and just some text to describe the popup box. this will not be shown

Those are just the two parameters we generally will always pass in

The android site has a nice little tutorial on popup boxes if you wanna learn more:

http://developer.android.com/guide/topics/ui/dialogs.html

Hope that helped!,

Kevin

thanks :)

Kevin Faust
Kevin Faust
15,353 Points

Nope a toast is different. It's just a temporary message that shows on your screen which goes away after a few seconds. Toasts also have no buttons or anything unlike pop up boxes.

Does that answer your question?

So what is the popup for android? How can I do that?

Thanks