How to set an icon for dialogue in Android

I want to set up a dialog on Android. I know how to set a title for a dialog:

dialog.setTitle("O message"); 

Now I want to put an icon in front of the heading. How can I achieve this?

 Dialog dialog; dialog = new Dialog(this); dialog.setContentView(R.layout.layouterror22); dialog.setTitle("O message"); 
+8
android
source share
5 answers

You can add an icon with the following code:

 Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON); dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.your_icon); dialog.setContentView(R.layout.custom_dialog); dialog.setTitle("Dialog Title"); dialog.show(); 

See Icons in the custom Android dialog box .

+23
source share

use it

 dialog.setIcon(R.drawable.ic_launcher) 

you need more customization tools, refer to this site http://www.androidhive.info/2011/09/how-to-show-alert-dialog-in-android/

+8
source share
 dialog.setIcon(Drawable icon); 

or

 dialog.setIcon(int resId); 

Hope this helps.

+4
source share

Maybe you should use AlertDialog. If you do, just

 AlertDialog.Builder b = new AlertDialog.Builder(yourContext); b.setIcon(yourIcon); /* add other properties thanks to b.set... */ b.create().show(); 

Hope this helps you.

+3
source share

If you use viewPager with fragments, you can call safeExit() from onBackPressed() in MainActivity . This is what I did, I never had a problem:

  @Override public void onBackPressed() { try { if (getFragmentManager().getBackStackEntryCount() > 1) { FragmentManager.BackStackEntry first = getFragmentManager().getBackStackEntryAt(0); getFragmentManager().popBackStack(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE); } else safeExit(); } catch (Exception e) { e.printStackTrace(); } } private void safeExit() { new Builder(this).setIcon(R.drawable.ic_launcher).setTitle("Exit!").setMessage( "Close Application?").setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { MainActivity.this.finish(); } }).setNegativeButton("No", null).show(); } 
0
source share

All Articles