Get color value programmatically when it's a reference (theme)
This should do the job:
TypedValue typedValue = new TypedValue();
Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.theme_color, typedValue, true);
@ColorInt int color = typedValue.data;
Also make sure to apply the theme to your Activity before calling this code. Either use:
android:theme="@style/Theme.BlueTheme"
in your manifest or call (before you call setContentView(int)
):
setTheme(R.style.Theme_BlueTheme)
in onCreate()
.
I've tested it with your values and it worked perfectly.
How to get a value of color attribute programmatically
I believe instead of this:
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorControlNormal, typedValue, true);
int color = typedValue.data;
You should do this:
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorControlNormal, typedValue, true);
int color = ContextCompat.getColor(this, typedValue.resourceId)
How to get color which is defined as attribute for different themes in Android [Kotlin]
I tested this and it works for me:
val color = MaterialColors.getColor(context, R.attr.myColor, Color.GRAY)
binding.fab.backgroundTintList = ColorStateList.valueOf(color)
This ^^^ is btw from SO, specifically from this answer and its comments: Get color value programmatically when it's a reference (theme).
how to get theme color programmatically in android java?
To get the value of an attribute in your theme you can use something like:
val typedValue = TypedValue();
theme.resolveAttribute(R.attr.colorPrimary, typedValue, true);
val color = ContextCompat.getColor(this, typedValue.resourceId)
button.setBackgroundColor(color)
or in java:
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorSecondary, typedValue, true);
int color = ContextCompat.getColor(this, typedValue.resourceId);
button.setBackgroundColor(color);
How to get an ?attr/ value programmatically
As commented from @pskink:
TypedValue value = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.editTextColor, value, true);
getView().setBackgroundColor(value.data);
will pull the attribute from the theme currently assigned to the context.
Thanks @pskink!
Programmatically set a view's color to ?android:attr color attributes
You can use android.R.attr.textColorPrimary
.
Something like:
val typedValue = TypedValue();
theme.resolveAttribute(android.R.attr.textColorPrimary, typedValue, true);
val color = ContextCompat.getColor(this, typedValue.resourceId)
In Java:
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(android.R.attr.colorSecondary, typedValue, true);
int color = ContextCompat.getColor(this, typedValue.resourceId);
Access Android reference style color attribute programmatically
Does something like this help?
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.textColorHint, typedValue, true);
button.setBackgroundColor(typedValue.data);
Related Topics
CSS Reference to Phone's Emoji Font
Using Videoview for Streaming or Progressive-Download Video
How to Add Network_Security_Config.Xml to Manifest in Expo App Without Ejecting
Supportmapfragment.Getmap() Returns Null
A Correct Way to Convert Byte[] in Java to Unsigned Char* in C++, and Vice Versa
Is There a Workaround for The Android Browser Bug with CSS-Position and Clickable Areas
How to Secure My App Against Piracy
Fragment Must Be a Public Static Class to Be Properly Recreated from Instance State
Android Clickablespan Not Calling Onclick
How to Implement Zoom Effect for Image View in Android
How to Convert Date Format in Android
Receive Audio via Bluetooth in Android
Stop All Toast Messages When Going to Another Screen in Android
Emulator: Warning: Quick Boot/Snapshots Not Supported on This Machine
Eclipse Indexer Errors When Using Stl with Android Ndk
Mobile Keyboard Pushes Up Content Because of an Absolutely Positioned Drawer