I have researched before asking this but suggested answers have not worked for me.
I have a bunch of colors listed in my colors.xml
file like below:
<color name="material_red">#F44336</color>
<color name="material_orange">#FF5722</color>
<color name="material_yellow">#FFC107</color>
and in my main layout, have applied one to the background as below:
<RelativeLayout
….
android:background="@color/material_blue"/>
I need to get the exact type of color that the layout is programmatically, this is what I've tried so far:
RelativeLayout relativeLayout = findViewById(…);
Drawable layoutDrawable = relativeLayout.getBackground();
String layoutColor;
if(layoutDrawable instanceOf ColorDrawable){
int color = ((ColorDrawable)layoutDrawable).getColor();
layoutColor = Integer.toHexString(color);
}
However the result comes to something like this: ff2196f3
.
How can I get the color in exact HEX value or possibly the color resource that was applied?