0

I am trying to create an AlertDialog with a single text field prompt. Here is the code I am using to create it:

final EditText url = new EditText(this);
new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK)
  .setTitle(R.string.mirror_title)
  .setView(url)
  .setPositiveButton(...)
  .setNegativeButton(...)
  .show();

When I run that against API level 22, the buttons style using Material as expected, but the EditText does not:

Dialog with old-style text input

What do I need to do to get the new style EditText here?

Sionide21
  • 2,202
  • 2
  • 21
  • 30
  • [How to change style of a default EditText](http://stackoverflow.com/a/17449332/1160282) || [Using styles and themes in Android](http://stackoverflow.com/a/17449332/1160282) – SilentKiller Mar 17 '15 at 04:57
  • EditText elements I put in a layout are the new style as expected, only this one in the dialog is the old kind. That leads me to believe there something wring with the way I've configured the alert rather than a need to add a custom style to my app. – Sionide21 Mar 17 '15 at 11:26

1 Answers1

4

You are specifying @android:style/Theme.DeviceDefault as your alert dialog theme, but your EditText is using whatever theme was set on the Context represented by this.

To ensure consistency between your alert dialog's decor and contents, you should always create the contents using the alert dialog's themed context.

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, ...)
    .setTitle(R.string.mirror_title)
    .setPositiveButton(...)
    .setNegativeButton(...);

Context dialogContext = dialogBuilder.getContext();
EditText url = new EditText(dialogContext);

dialogBuilder.setView(url).show();

As a side note, it looks like you may need to specify an activity theme in your AndroidManifest.xml and check that your targetSdkVersion is specified correctly. You shouldn't be seeing Gingerbread-styled widgets unless you are explicitly targeting the Gingerbread themes (ex. Theme or Theme.Light).

alanv
  • 23,966
  • 4
  • 93
  • 80