1

What I am doing:

  • I am using this in activity
  • My activity extends ActionBarActivity
  • My minimum sdk is 8 in manifest

I am getting the error as:

Call requires API level 11 (current min is 8): new android.app.AlertDialog.Builder

CODE

public void openSettings(String custMsg){

        final AlertDialog.Builder alert = new AlertDialog.Builder(this,AlertDialog.THEME_DEVICE_DEFAULT_DARK);
        alert.setMessage(custMsg);
        alert.setCancelable(false);
        alert.setNegativeButton(getResources().getString(R.string.Cancel), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
                tryAgainId.setVisibility(View.VISIBLE);
            }
        });
        alert.setPositiveButton(getResources().getString(R.string.Ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                locationValidationDone=true;
                dialog.dismiss();
                startActivity(new Intent(Settings.ACTION_SETTINGS));
            }
        });

        alert.show();
    }

Question:

How can i resolve this

Devrath
  • 42,072
  • 54
  • 195
  • 297
  • 2
    `AlertDialog.Builder(context,theme)` is added in API level 11 but min sdk is 8. to avoid this error use `AlertDialog.Builder(context)` or @NewApi before method – ρяσѕρєя K Jan 06 '15 at 15:35
  • @ρяσѕρєя K ......... Yes that fixes ! ... so if i need to use a custom style for a dialog as i have done ... only waay s to use higher API's ? – Devrath Jan 06 '15 at 15:36
  • 1
    @Devrath : see [How to change theme for AlertDialog](http://stackoverflow.com/questions/2422562/how-to-change-theme-for-alertdialog) post to set custom theme before api level 11 – ρяσѕρєя K Jan 06 '15 at 15:37
  • 1
    @Devrath Are you going to choose an answer? Thank you! – Jared Burrows Jan 06 '15 at 15:54

3 Answers3

2

Please look at the docs:

The constructor you are using requires API 11.

public AlertDialog.Builder (Context context, int theme)

Added in API level 11
Constructor using a context and theme for this builder and the AlertDialog it creates. The actual theme that an AlertDialog uses is a private implementation, however you can here supply either the name of an attribute in the theme from which to get the dialog's style (such as alertDialogTheme or one of the constants AlertDialog.THEME_TRADITIONAL, AlertDialog.THEME_HOLO_DARK, or AlertDialog.THEME_HOLO_LIGHT.

You need to use the contructor added in API 1:

public AlertDialog.Builder (Context context)

Added in API level 1
Constructor using a context for this builder and the AlertDialog it creates.
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
0

With this method to permanently fix that:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void showAlert(int paramInt, String title, Activity act,
        String message) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        AlertDialog.Builder localBuilder = new AlertDialog.Builder(act,
                R.style.CustomDialog);
        TextView messageText = new TextView(act);
        messageText.setText(message);
        localBuilder.setTitle(title);
        messageText
                .setTextColor(act.getResources().getColor(R.color.white));
        messageText.setGravity(Gravity.CENTER);
        messageText.setTextSize(18);
        messageText.setLineSpacing(1f, 1.5f);
        localBuilder.setView(messageText);
        localBuilder.setPositiveButton("Ok", null);
        messageText.setMovementMethod(new ScrollingMovementMethod());
        AlertDialog dialog = localBuilder.show();
        dialog.show();
    } else {
        Dialog dialog = new Dialog(act);
        dialog.setTitle(title);
        TextView messageText = new TextView(act);
        messageText.setText(message);
        dialog.setContentView(messageText);
        dialog.show();
    }

}

and CustomDialog style:

<style name="CustomDialog" parent="android:Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:scrollbars">vertical</item>
</style>
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Iman Marashi
  • 5,593
  • 38
  • 51
-1

Use constructor

public AlertDialog.Builder(Context)

instead of

public AlertDialog.Builder(Context, int)

Declare theme of dialog in styles.xml.

Pang
  • 9,564
  • 146
  • 81
  • 122
Simar
  • 600
  • 4
  • 17