Show a message box on Android

281

To display a message box on Android you can use AlertDialog class. It has many options, however I'll use the most simple example:

// ...
import android.app.AlertDialog;
import android.content.DialogInterface;

public class MessageBox
{
    void show(String title, String message)
    {
        dialog = new AlertDialog.Builder(activity) // Pass a reference to your main activity here
            .setTitle(title)
            .setMessage(message)
            .setPositiveButton("OK", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialogInterface, int i)
                {
                    dialog.cancel();
                }
            })
            .show();
    }

    private AlertDialog dialog;
}

Note that .show() method doesn't block the caller thread like it does with WinAPI MessageBox or JavaScript alert functions.

Share this page:

See also how to:

How to call your native functions in Android NDK application from Java
How to use JNIEnv::FindClass on Android and what you should know about it
How to open URL in web browser from Android NDK application with Java or native code
How to get App ID for your mobile application in AdMob platform
How to find all Clang arguments that used to build native code in Android Studio