Home > Software design >  Android dialog with icon in subtitle
Android dialog with icon in subtitle

Time:12-15

I am trying to use android dialog , with icon set to subtitle. I am using the below code, but getting icon in title. I am trying to get something like the attached image. enter image description here

dialogBox = new MaterialAlertDialogBuilder(dialogContext)
                        .setTitle("Power Off")// getting image in title
                        .setMessage("Shutting Down")// Need image here
                        .setIcon(R.mipmap.loader)
                        .setCancelable(false)
                        .show();

CodePudding user response:

Here is solution

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setTitle(R.string.my_dialog_title);
dialog.setContentView(R.layout.my_dialog_layout);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.mipmap.loader);
dialog.show(); 

If you want your dialog to look like activity then add a theme to the dialog as follow

final Dialog dialog = new Dialog(this,AlertDialog.THEME_HOLO_LIGHT);  

Call setFeatureDrawableResource() after show().

No idea why this works. :)

CodePudding user response:

Pls try with following code.


dialogBox = new MaterialAlertDialogBuilder(dialogContext)
                        .setTitle("Power Off")// getting image in title
                        .setMessage("Shutting Down")// Need image here
                        .setIcon(dialogContext.getResources().getDrawable(R.mipmap.loader))
                        .setCancelable(false)
                        .show();
  • Related