How to set Icon to the Alert Dialog Programatically in Android
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
3 Answers
3
Use this
builder.setIcon(ContextCompat.getDrawable(MyActivity.this,R.drawable.video_downloader));
instead of this
builder.setIcon(this.getDrawable(R.drawable.video_downloader));
FYI
getDrawable()
method was deprecated in API level 22.
getDrawable()
try this,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(getResources().getDrawable(R.drawable.video_downloader));
builder.setTitle("Congratulations !!!");
builder.show();
It works for me, Thanku so much. Please mark my Question as real if you think it's genuine.
– Numan Gillani
1 min ago
your error occured here builder.setIcon(this.getDrawable(R.drawable.video_downloader));
builder.setIcon(this.getDrawable(R.drawable.video_downloader));
just try this
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.video_downloader);
refer this
A pretty simple solution worked for me.
– Numan Gillani
25 secs ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of stackoverflow.com/questions/28669868/…
– prashant17
8 mins ago