Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Alert Dialog Background Issue API 11+

I create a AlertDialog with the code below. For some reason I'm getting an extra background (see pic) on Honeycomb and above. The code crashes fine for anything below honeycomb. MyCustomDialog is simply Theme.Dialog for < API-11 and Theme.Holo.Dialog for API-11 and up.

  1. Any idea why I'm getting the extra background?
  2. Any idea why it crashes for API < 11? It works fine if I remove the Theme.

Update figured out the answer to Question #2. Seems the constructor AlertDialog.Builder(Context context, int theme) was introduced in API 11. My fix was simply to change the line to:

final AlertDialog.Builder builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(this) : new AlertDialog.Builder(this,R.style.JumpDialog);

I still need help with Question #1

enter image description here

private Dialog setupKeyBoardDialog() {
    if (mContact.getLocaleId() != -1) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyCustomDialog);
        builder.setTitle("Keyboards");

        mKeyboardLayouts = new KeyboardLayoutGroup();
        mKeyboardLayouts.layoutNames = new CharSequence[(int) jni.getNumKeyLayouts()];
        mKeyboardLayouts.layoutValue = new ArrayList<Integer>();

        for (int i = 0; i < jni.getNumKeyLayouts(); i++) {
            mKeyboardLayouts.layoutNames[i] = jni.LayoutInfoForIndex(i).getName();
            mKeyboardLayouts.layoutValue.add(i, (int) jni.LayoutInfoForIndex(i).getLocale_id());
        }

        final int selectedItem = mKeyboardLayouts.layoutValue.indexOf(mContact.getLocaleId());

        builder.setSingleChoiceItems(mKeyboardLayouts.layoutNames, selectedItem, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                mContact.setLocaleId(mKeyboardLayouts.layoutValue.get(item));
                mContactsDB.saveContact(mContact, true);

                dialog.dismiss();
                initializeSettingsList();
            }
        });

        final AlertDialog dialog = builder.create();
        dialog.setButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogBox, int arg1) {
                dialogBox.cancel();
            }
        });

        return dialog;
    }

    return null;
}
like image 666
Ali Avatar asked Dec 31 '11 00:12

Ali


1 Answers

Figured out the answers

  1. AlertDialog has it's on static constants for each theme in the AlertDialog class and it does not take the standard theme. when I replaced R.style.MyTheme or android.R.style.Theme_Holo_Dialog with AlertDialog.THEME_HOLO_LIGHT the code worked just fine.
  2. Seems the constructor AlertDialog.Builder(Context context, int theme) was introduced in API 11. My fix was simply to change the line to:

    final AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        builder = new AlertDialog.Builder(this);
    } else {
        builder = new AlertDialog.Builder(this,R.style.JumpDialog);
    }
    
like image 88
Ali Avatar answered Nov 07 '22 00:11

Ali