0

I am getting below error while sending color resource id from main app to library app (both is handled by me).

In main app, Theme is being changed dynamically so, I am passing the resource id of main app theme's primary color by adding it in intent extra as below

EDIT -- I am retrieving the theme's primary color as below in main app and sending it to libraray app.

private int getThemeColor() {
        TypedValue typedValue = new TypedValue();
        Resources.Theme theme = getActivity().getTheme();
        theme.resolveAttribute(R.attr.colorPrimary, typedValue, true);
        @ColorInt int color = typedValue.data;
        return color;
    }

//passing primary color to library app activity via intent 
intent.putExtra("PRIMARY_COLOR",themeColor);

In library app I am retrieving the resource id as below

primaryColor = intent.getIntExtra("PRIMARY_COLOR", 0)
val res: Resources =
            packageManager.getResourcesForApplication(callerPackageName)
        primaryColor = res.getColor(primaryColor) //This line throws error
Nishant
  • 32,082
  • 5
  • 39
  • 53
  • I don't know if I understand correctly: You have two modules and you want to pass one resource from one module to another module . is it true? and both module resources have your color? – Sobhan Mar 19 '23 at 06:07
  • @Sobhan, No resource is in main app. I need to send the main app theme primary color to library app to apply color on library UI component – Nishant Mar 19 '23 at 06:17
  • i think you don't access resources id. So you can send the color hex and set it on the library. – Sobhan Mar 19 '23 at 06:22
  • @Sobhan, color hex value is changing dynamically on main app. So I am retrieving the color from main app using the code in the EDIT section – Nishant Mar 19 '23 at 06:29

1 Answers1

1

Based on your code and the value in the exception, you are already retrieving color int from the intent, so you don't need to get it from resources, you can just use it as is. What you are instead doing right now, is trying to interpret the color in as color id which obviously doesn't exist, so you get an exception

Sergei Kozelko
  • 691
  • 4
  • 17