Skip to content

How to set Icon to the Alert Dialog Programatically in Android

Em using these statements for creating an Alert Dialog and set icon to it,

       AlertDialog.Builder builder = new AlertDialog.Builder(this);

       builder.setIcon(this.getDrawable(R.drawable.video_downloader));

       builder.setTitle("Congratulations !!!");

       builder.show();

But it is crashing at setIcon, when I checked in drawable folder, the video_downloader icon is there already.

AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
    at android.view.View$1.onClick(View.java:3607)
    at android.view.View.performClick(View.java:4212)
    at android.view.View$PerformClick.run(View.java:17476)
    at android.os.Handler.handleCallback(Handler.java:800)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5371)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at android.view.View$1.onClick(View.java:3602)
    at android.view.View.performClick(View.java:4212) 
    at android.view.View$PerformClick.run(View.java:17476) 
    at android.os.Handler.handleCallback(Handler.java:800) 
    at android.os.Handler.dispatchMessage(Handler.java:100) 
    at android.os.Looper.loop(Looper.java:194) 
    at android.app.ActivityThread.main(ActivityThread.java:5371) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:525) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
    at dalvik.system.NativeStart.main(Native Method) 
 Caused by: **java.lang.NoSuchMethodError**: numan.downloader.Test.Front.getDrawable
    at numan.downloader.Test.Front.GetMedia(Front.java:115)
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:525) 
    at android.view.View$1.onClick(View.java:3602) 
    at android.view.View.performClick(View.java:4212) 
    at android.view.View$PerformClick.run(View.java:17476) 
    at android.os.Handler.handleCallback(Handler.java:800) 
    at android.os.Handler.dispatchMessage(Handler.java:100) 
    at android.os.Looper.loop(Looper.java:194) 
    at android.app.ActivityThread.main(ActivityThread.java:5371) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:525) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
    at dalvik.system.NativeStart.main(Native Method) 

07-31 08:35:51.291 500-519/? E/AppErrorDialog: Failed to get ILowStorageHandle instance

Answer

try this,

   AlertDialog.Builder builder = new AlertDialog.Builder(this);

   builder.setIcon(getResources().getDrawable(R.drawable.video_downloader));

   builder.setTitle("Congratulations !!!");

   builder.show();