I am required to pass ARGB color from 1 Activity to other activity as String. Now I am required to convert the same color string in other activity to be converted to int to pass into an argument in color method.
Here is my implementation:
Activity1
Intent i = new Intent(Activity1.java,Activity2.class);
...
i.putExtra("color1", "Color.argb(200, 69, 202, 252)");
i.putExtra("color2", "Color.argb(200, 48, 63, 159)");
startActivity(i);
Now, I am using this string in other class that's
Activity2
Intent i = getIntent();
String color1 = i.getStringExtra("color1");
String color2 = i.getStringExtra("color2");
...
tvContent.setBackground(getGradientColor(Color.parseColor(color1), Color.parseColor(color1)));
...
public static GradientDrawable getGradientColor(int color1, int color2) {
int[] colors = new int[2];
colors[0] = color1;
colors[1] = color2;
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TL_BR, colors);
gd.setGradientType(GradientDrawable.LINEAR_GRADIENT);
gd.setCornerRadius(50);
gd.setStroke(0, Color.WHITE);
return gd;
}
Error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.activity.SubListActivity}: java.lang.IllegalArgumentException: Unknown color
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.IllegalArgumentException: Unknown color
at android.graphics.Color.parseColor(Color.java:163)
at com.gstechnovos.mathsploy.activity.SubListActivity.onCreate(SubListActivity.java:101)
at android.app.Activity.performCreate(Activity.java:6942)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Thank you in advance.
You're passing invalid String
to Color.parseColor(String)
:
Supported formats are:
#RRGGBB
or#AARRGGBB
I suggest operating on color ints from the beginning. Remove quotes from putExtras to just create argb int instead:
// Activity1
i.putExtra("color1", Color.argb(200, 69, 202, 252));
i.putExtra("color2", Color.argb(200, 48, 63, 159));
startActivity(i);
Then retrieve and use the integers in second activity:
Intent i = getIntent();
int color1 = i.getIntExtra("color1");
int color2 = i.getIntExtra("color2");
...
tvContent.setBackground(getGradientColor(color1, color2));
...