Get color int from color resource

Keywords: Android xml encoding

Is there any way to get color information from color resources? I'm trying to get the red, blue, and green components of the colors defined in the resource (R.color.myColor) so that I can set the values of the three search bars to a specific level.

#1 building

Define your color

Value / color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- color int as #AARRGGBB (alpha, red, green, blue) -->
    <color name="orange">#fff3632b</color>
    ...
    <color name="my_view_color">@color/orange</color>

</resources>

Get color int and set it

int backgroundColor = ContextCompat.getColor(context, R.color.my_view_color);
// Color backgroundColor = ... (Don't do this. The color is just an int.)

myView.setBackgroundColor(backgroundColor);

You can also have a look

#2 building

Based on the new Android support library (and this Update), you should now call:

ContextCompat.getColor(context, R.color.name.color);

according to File :

public int getColor (int id)

This method is not recommended in API level 23. Use getColor (int, Theme) instead

It is the same as the getResources().getColorStateList(id) solution:

You must change it like this:

ContextCompat.getColorStateList(getContext(),id);

Editor 2019

About the context in which ThemeOverlay uses the closest view:

val color = ContextCompat.getColor(
  closestView.context,
  R.color.name.color
)

In this way, you can get the correct color according to ThemeOverlay.

It is especially necessary to use different themes (such as dark / Light Themes) in the same activity. If you want to learn more about themes and styles, we recommend the following discussion: Using styles to develop themes

#3 building

I updated to use contextcompat.getcolor (context, r.color. Your [color]); but sometimes (on some devices / Android versions). I'm not sure) will cause NullPointerException.

So to make it work on all devices / versions, I'll use the old method with null pointers.

try {
    textView.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_grey_dark));
}
catch(NullPointerException e) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        textView.setTextColor(getContext().getColor(R.color.text_grey_dark));
    }
    else {
        textView.setTextColor(getResources().getColor(R.color.text_grey_dark));
    }
}

#4 building

Best method

As @ sat's answer, a good way to get color is

ResourcesCompat.getColor(getResources(), R.color.your_color, null);

Or use the following method when the getResources() method cannot be accessed.

Context context  = getContext(); // like Dialog class
ResourcesCompat.getColor(context.getResources(), R.color.your_color, null);

What I have to do is

public void someMethod(){
    ...
    ResourcesCompat.getColor(App.getRes(), R.color.your_color, null);
}

It's easiest to use anywhere in your application! Even in a Util class or any class where you don't have Context or getResource()

Problem (when you don't have a context)

When you don't have Context access, it's like a method in a Util class.

Suppose the following method has no context.

public void someMethod(){
    ...
    // can't use getResource() without Context.
}

Now, you will pass Context as a parameter in this method and use getResources()

public void someMethod(Context context){
    ...
    context.getResources...
}

So here's a unique Bonus solution that allows you to access Resources from anywhere like Util class. Add Resources to your Application class, or create one if it doesn't exist.

import android.app.Application;
import android.content.res.Resources;

public class App extends Application {
    private static App mInstance;
    private static Resources res;


    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        res = getResources();
    }

    public static App getInstance() {
        return mInstance;
    }

    public static Resources getResourses() {
        return res;
    }

}

Add the name field to the manifest. XML < application tag. (if not already added)

<application
        android:name=".App"
        ...
        >
        ...
    </application>

Now you're fine. Use resourcescompat. Getcolor (app. Getres(), r.color. Your color, null); anywhere in the app.

#5 building

Accessing colors from inactive classes can be difficult. One of the alternatives I found was to use enum. Enum provides a lot of flexibility.

public enum Colors
{
  COLOR0(0x26, 0x32, 0x38),    // R, G, B
  COLOR1(0xD8, 0x1B, 0x60),
  COLOR2(0xFF, 0xFF, 0x72),
  COLOR3(0x64, 0xDD, 0x17);


  private final int R;
  private final int G;
  private final int B;

  Colors(final int R, final int G, final int B)
  {
    this.R = R;
    this.G = G;
    this.B = B;
  }

  public int getColor()
  {
    return (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
  }

  public int getR()
  {
    return R;
  }

  public int getG()
  {
    return G;
  }

  public int getB()
  {
    return B;
  }
}

Posted by Serpent7 on Mon, 03 Feb 2020 01:50:53 -0800