wiki
wiki

Reputation: 289

Methods of alert dialog with Android API level 8

Can anyone tell me why if I declare an AlertDialog then the compiler will let me use methods like setMessage() or setTitle()?

Dialog dialog = new Dialog(this);

The target is API level 15 and minimun is 8, so the code should work with API 8.

Thank you very much!

Upvotes: 0

Views: 1305

Answers (3)

VegeOSplash
VegeOSplash

Reputation: 204

  1. android:minSdkVersion="8"
  2. android:targetSdkVersion="15"

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("Alert Builder");
    alertDialogBuilder.setMessage("AlertDialog in API level 8");
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
    

    It works for me Happy Coding.

Upvotes: 1

Shlublu
Shlublu

Reputation: 11027

  • setMessage() is not a method of the class Dialog, regardless the API level. This is a method of AlertDialog.

  • Dialog(Context) and setTitle() work under Android 8.

More generally, I am using the following code with API 8 with no problem. This is an AlertDialog-based piece of code but it sounds in accordance with your use case as you were speaking about setMessage().

    AlertDialog dlg = null;

    if (!activity.isFinishing()) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(activity);

        builder.setTitle(resTitle)
               .setMessage(text)
               .setCancelable(false)
               .setPositiveButton(resOk, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       if (notify != null) { // 'notify' is a Handler
                           notify.sendEmptyMessage(MODAL_ALERT_SEEN);
                       }
                       dialog.dismiss();
                   }
               }
        );

        dlg = builder.create();
        dlg.show();
    }

For further details, you can check the official API documentation. There's an API level selector that grays out the methods not applicable to the API level you wish to be compatible with:

Compatibility level on the API documentation

Upvotes: 2

Gintas_
Gintas_

Reputation: 5030

Because Dialog doesn't have these methods, these are AlertDialog methods. Use AlertDialog instead. Usage example can be found here.

Upvotes: 1

Related Questions