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.
Call setFeatureDrawableResource()
after show()
.
No idea why this works. :)
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);
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();
setIcon(R.drawable.image_name)
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
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());
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 .. :)
精彩评论