How to get Android system Colors? - android

I'm searching way to get Android system colors - color theme used in device.
Using android:color/ I didn't get correct colors.
For example: background color in my device is BLACK, menu background color is DARKGREY.
Values from android:color/ in device is: only BLACK and WHITE backgrounds.

You can get android system color using
android:textColor="#android:color/black"
And this is working fine

Thanks for rekire :)
Finally I have the answer:
Android: How to get background color of Activity in Java?
TypedArray array = getTheme().obtainStyledAttributes(new int[]{android.R.attr.colorBackground,});
int backgroundcolor = array.getColor(0, 0xFF00FF);

It might just be setting a color filter on whatever the base color of the device is. That may be why you're not getting the base color you were expecting.
This is just a theory, I haven't verified it yet. Either that, or the base color for the theme you're looking is stored in a slightly different location. That's also a possibility.

Related

What is the Material design grey color of icons in Android?

I'm looking for the default Material design grey color of icons in Android. I use two icons but for some reason one is black and one is grey (although they original should be both black) and they have the exact same code:
I want to set the default grey color. Where can I find it? Even the hex value should solve this problem.
Using an eyedropper tool I found the hex code for the grey envelope, it's #737373.
If you're doing design work installing a tool like Color helps when it comes to finding an element's hex color. It includes an eyedropper tool that lets you click an element and get it's hex color.
For changing icon color try:
<ImageButton
android:layout_width="your value"
android:layout_height="your value"
...
android:tint="YourColor"
/>

Get Android system accent color (Android 10 System color accent)

This question should be simple, but i didn't find an answer. I have an app with selectable accent, and i'm trying to add an option to use the android system accent (apps like Lawnchair have such an option). In the style for the system accent, i tried to get this accent in every way possible:
?android:colorAccent
?android:attr/colorAccent
?attr/colorAccent
And this is the style:
<style name="AppTheme.systemAccent" parent="AppTheme">
<item name="colorAccent">???</item>
</style>
Nothing seems to work and the app crashes, but i'm certain that this is possible. The accent selection, when i use normal colors, works fine. Where am i wrong?
EDIT: Just to be clear, i'm trying to get the system accent color, i.e. the system wide color used in settings, notification panel and so on. This color is now selectable in Android 10 and was selectable before in rom like the Oxygen OS. Let's say i select a red accent in settings->customization on my Android 10 device. I want to get this red accent in my app
You can get it programmatically:
TypedValue typedValue = new TypedValue();
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(this,
android.R.style.Theme_DeviceDefault);
contextThemeWrapper.getTheme().resolveAttribute(android.R.attr.colorAccent,
typedValue, true);
int color = typedValue.data;
Just to be clear I am referring to the Android Q System color accent:
Okay. I did some intensive research and trial and error. After that I found out, that I could access a private property #*android:color/accent_device_default_light. BUT this is just possible if you change your parent class for the activity from AppCompatActivity to Activity because the AppCompat can't set up the toolbar with this private property. Further it's not recommended to use private properties because they are likely to get deleted or changed in the future.
#color/colorAccent
use this ,it's default color in android
or you can customize your color in the color.xml

Get an EditText's 'default' color value from theme

I have an Activity that contains an EditText on 3.1. Based on user input, I change the color of the text in the EditText (red for an error), and then reset it to black when the text is OK.
One issue relates to changing the overall theme of the Activity. For instance, changing it to the regular dark theme from the light theme results in the black text being shown against a black background - so I need to go in and change the code, instead resetting the text to white when the data is OK.
Instead of having to change this code if I make a theme change to the Activity, I was wondering if there was a way to pull the default EditText text color for a given theme programmatically, then I can just switch the text back to the default color instead of hard-coding in the white, black, etc.
According to the Theme's docs get the colour directly using obtainStyledAttributes.
TypedArray themeArray = context.getTheme().obtainStyledAttributes(new int[] {android.R.attr.editTextColor});
try {
int index = 0;
int defaultColourValue = 0;
int editTextColour = themeArray.getColor(index, defaultColourValue);
}
finally
{
// Calling recycle() is important. Especially if you use alot of TypedArrays
// http://stackoverflow.com/a/13805641/8524
themeArray.recycle();
}
Use R.attr.
setTextColor(android.R.attr.editTextColor)
EditText.getCurrentTextColor() and EditText.getTextColors() will also provide the default colour if you retrieve them before changing the colour. Additionally this approach can be used pre 3.0 which is not possible when using android.R.attr.editTextColor.

what style/attr/drawable for inverse background?

I try to make a ExpandableListView where the group headers are drawn inverse. There is no problem with changing the text color, size etc. via XML. I even found out how to use the system defaults like
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#android:style/TextAppearance.Medium.Inverse"
/>
But for the background color?! I don't understand why these styles don't include background color information?!
Of course I could use a direct color, but I look for good default background attributes or styles. Like "style/Background.For.TextAppearance.Medium.Inverse" ;-)
What would be a good solution? So that for the dark themed devices I get white/gray, and for the white themed I get black?
Or should I simply use R.color.background_light?
Greetings, Joerg
PS: First question here ;-) Thanx to all the people answering here the last months and years: You great people made it much more easier for me to find a re-entrance in programming after 12 years break ;-)
As you observe, the styles with "TextAppearance" in their name only affect the foreground text attributes of the view. They are appropriate for the android:textAppearance attribute. The styles with "Widget" in their names define all the UI properties and will work in a style attribute, but Android doesn't define a "Widget.TextView.Inverse" style.
When I wanted to display an console-like log as an inverse text view, I used the following XML:
<TextView
android:textAppearance="#style/TextAppearance.AppCompat.Small.Inverse"
android:background="?android:colorForeground"
... />
It uses the theme's foreground color as the background for the view. With a dark theme it displays dark text on white, and in a light theme it displays light text on black.

What is default color for text in textview?

I set the color to red , and after that I want to set the color again back to default, but I do not know what is default color, does anyone knows ?
Actually the color TextView is:
android:textColor="#android:color/tab_indicator_text"
or
#808080
You can save old color and then use it to restore the original value. Here is an example:
ColorStateList oldColors = textView.getTextColors(); //save original colors
textView.setTextColor(Color.RED);
....
textView.setTextColor(oldColors);//restore original colors
But in general default TextView text color is determined from current Theme applied to your Activity.
There are some default colors defined in android.R.color
int c = getResources().getColor(android.R.color.primary_text_dark);
Get these values from attributes:
int[] attrs = new int[] { android.R.attr.textColorSecondary };
TypedArray a = getTheme().obtainStyledAttributes(R.style.AppTheme, attrs);
DEFAULT_TEXT_COLOR = a.getColor(0, Color.RED);
a.recycle();
There are defaults in the theme that Android uses if you don't specifiy a text color. It may be different colors in various Android UIs (e.g. HTC Sense, Samsung TouchWiz, etc). Android has a _dark and _light theme, so the defaults are different for these (but nearly black in both of them in vanilla android). It is however good practice to define your primary text color yourself for to provide a consistent style throughout the devices.
In code:
getResources().getColor(android.R.color.primary_text_dark);
getResources().getColor(android.R.color.primary_text_light);
In xml:
android:color="#android:color/primary_text_dark"
android:color="#android:color/primary_text_light"
As reference in vanilla Android the dark theme text color is #060001 and the in the light theme it's #060003 since API v1. See the android style class here
I know it is old but according to my own theme editor with default light theme, default
textPrimaryColor = #000000
and
textColorPrimaryDark = #757575
I used a color picker on the textview and got this #757575
It may not be possible in all situations, but why not simply use the value of a different random TextView that exists in the same Activity and that carries the colour you are looking for?
txtOk.setTextColor(txtSomeOtherText.getCurrentTextColor());
The color of text inside a TextView is totally dependent on your theme.
The easiest way to know it:
Add a TextView to any xml file
Select the TextView
Click on Split view
Open the Attributes tab and scroll to the color section.
As you can see, according to my theme it is: #android:color/secondary_text_material_light
I believe the default color integer value is 16711935 (0x00FF00FF).
hey you can try this
ColorStateList colorStateList = textView.getTextColors();
String hexColor = String.format("#%06X", (0xFFFFFF & colorStateList.getDefaultColor()));
I found that android:textColor="#android:color/secondary_text_dark" provides a closer result to the default TextView color than android:textColor="#android:color/tab_indicator_text".
I suppose you have to switch between secondary_text_dark/light depending on the Theme you are using
You could use TextView.setTag/getTag to store original color before making changes. I would suggest to create an unique id resource in ids.xml to differentiate other tags if you have.
before setting to other colors:
if (textView.getTag(R.id.txt_default_color) == null) {
textView.setTag(R.id.txt_default_color, textView.currentTextColor)
}
Changing back:
textView.getTag(R.id.txt_default_color) as? Int then {
textView.setTextColor(this)
}
There are some default colours which get defined in the Themes of app. Below is the code snippet which you can use to get the current default color programmatically.
protected int getDefaultTextColor(){
TextView textView = new TextView(getContext());
return textView.getCurrentTextColor();
}
There is no default color. It means that every device can have own.

Categories

Resources