question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use ContextCompat when accessing color resources

See original GitHub issue

Avoid duplicates

  • This enhancement request has not already been raised before
  • Enhancement request is specific for Android only, for general issues / questions that apply to iOS and Android please raise them in CWA-Wishlist
  • If you are proposing a new feature, please do so in CWA-Wishlist

Current Implementation

You are already using ContextCompat when accessing color resources at some locations, but Context.getColor in most others, which is not compatible with API < 23.

In our fork of the app, where I am working on Android 5 support, it is a lot of manual work for me to spot calls to Context.getColor and to replace them with a call that has the same functionality but supports API 21 (like Context.getResources().getColor).

Suggested Enhancement

Use the utility class ContextCompat when accessing color resources everywhere.

Expected Benefits

This would not change any functionality, but it would make things a lot easier for me as a maintainer of our fork, because there would be less discrepancies to keep track of.

If such a PR would get merged, I would like to open one to use this utility class every time color resources are accessed.

Thank you for your consideration.


Internal Tracking ID: EXPOSUREAPP-4419

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
fynngodaucommented, Dec 19, 2020

Hello @dsarkar, thank you for your input; I have updated the issue to follow the template instead of closing and reopening, I hope that’s okay as well.

1reaction
d4rkencommented, Dec 21, 2020

I did a quick search through the project and it looks okay to change. I’m not 100% sure how to handle this in unit tests, where the getColor calls also appear.

Mock static? Or maybe mocking an extension function Context.getColorCompat 🤔 .

What would be your preferred solution @fynngodau ?

If such a PR would get merged, I would like to open one to use this utility class every time color resources are accessed.

Should be against release/1.11.x and preferably after #1912 is merged to avoid conflicts.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I get color-int from color resource? - Stack Overflow
You can use: getResources().getColor(R.color.idname);. Check here on how to define custom colors:.
Read more >
ContextCompat - Android Developers
Helper for accessing features in android.app.Activity . ... Returns a color associated with a particular resource ID ... protected ContextCompat().
Read more >
Styling Colors & Drawables w/ Theme Attributes
W/Resources: ColorStateList color/button_text_csl has unresolved theme attributes! Consider using Resources.getColorStateList(int, Theme) or ...
Read more >
androidx.core.content.ContextCompat.getColor java ... - Tabnine
Best Java code snippets using androidx.core.content. ... @param fallbackColorResId a color resource id tto fallback to if the theme color cannot be ...
Read more >
AppCompatResources doesn't resolve nested CSLs that use ...
The same behavior is exhibited if using android:textColor or manually loading the @color/text_tertiary with AppCompatResources and using ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found