Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, November 3, 2016

How to place an icon into the title of a custom dialog

How to place an icon into the title of a custom dialog


I'd like to place a drawable into a dialogs title bar. I tried the following:

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

The icon doesn't show up but the title moves a little to the right. It seems the dialog reserves space for the drawable but doesn't draw it. I tried several different icons (also from the android resources) but non of them worked.

Answer by Reuben Scratton for How to place an icon into the title of a custom dialog


Call setFeatureDrawableResource() after show().

No idea why this works. :)

Answer by Kirit Vaghela for How to place an icon into the title of a custom dialog


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.drawable.some_icon);  dialog.show();   

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

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

Answer by Sma?l Hammour for How to place an icon into the title of a custom dialog


setIcon(R.drawable.image_name)  

Answer by PeteH for How to place an icon into the title of a custom dialog


Here is THE solution. Follow the recipe and you shall have your icon! Note: order is very important...

        final Dialog yourDialog = new Dialog(YourClass.this);              yourDialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);  //must come BEFORE setContentView              yourDialog.setContentView(R.layout.yourDialog_layout);              yourDialog.setTitle("Your Title");              yourDialog.setCancelable(true);                yourDialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.your_icon);  //must come AFTER setContentView  

Answer by Sergio Abreu for How to place an icon into the title of a custom dialog


I got it to work in a different way, thanks to the Sma?l Hammour post.

Place this static method in your preferred tool class:

public static void msgBox( String msg, String title, int type, final Context c){        int theIcon = drawable.ic_dialog_alert;        switch(type){      case YourToolClass.CONFIRMATION:          theIcon = drawable.ic_menu_help;          break;            case YourToolClass.INFO:          theIcon = drawable.ic_dialog_info;          break;      case YourToolClass.ALERT:      default:      }    AlertDialog.Builder builder = new AlertDialog.Builder(c);        /* Here enters the .setIcon: */  builder.setMessage(msg) .setTitle (title) .setIcon(theIcon);    builder.setPositiveButton( "OK", new DialogInterface.OnClickListener() {      public void onClick(DialogInterface dialog, int id) {          /*  */      }  });       AlertDialog dialog = builder.create();   dialog.show();    }  

To invoke:

YourToolClass.msgBox("the main message goes here", "Test", getBaseContext());  

Answer by Carlos for How to place an icon into the title of a custom dialog


You can also extend the Dialog class like so:

public class CustomDialog extends Dialog {        public CustomDialog(Context context) {          super(context);          setTitle("Some Title");          requestWindowFeature(Window.FEATURE_LEFT_ICON);          setContentView(R.layout.my_layout);      }        @Override      protected void onStart() {          setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.my_icon);          super.onStart();      }  

i.e. you prepare your window feature in constructor and then set concrete resource in onStart.

So, in you main code you can simply use:

    CustomDialog cd = new CustomDialog(getActivity());      rd.show();  

Answer by Priyanka.Chouhan for How to place an icon into the title of a custom dialog


calling setFeatureDrawableResource llike this

 dialog.show();   dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.logo_1x);  

i.e after calling dialog.show() worked perfectly in my case .. thanks .. :)


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.