When I'm adding textviews to xml layout files I get textviews with black text color by default;
When I'm generating this textviews programmatically I get textviews with a kind of gray text color.
I want this text views have the same style, and I'm not sure whether simple setting black color for dynamically generated textviews will help, because I'm not sure it will look the same on all versions of android.
What will you recommend me? What style resource is default?
Isn't there any possibilities of using default styles?
Set the colors your self either in the xml
android:textColor="#000000"
or dynamically
mText.setTextColor(Color.parseColor("#000000"));
I never use the default colors
You need to create a default style in styles.xml. Create an item with text color black or the default color you want to use.
You can also refer this site:
http://androidplus.org
I guess what your looking for is android.R.color.primary_text_light which is the default color for dark text in a light theme like in your case.
int c = getResources().getColor(android.R.color.primary_text_light);
textView.setTextColor(textColor);
It may be even better to use android.R.attr.textColorPrimary as it returns the appropriate default color for both themes:
int[] attrs = new int[] {android.R.attr.textColorPrimary};
TypedArray ta = context.obtainStyledAttributes(attrs);
int textColor = ta.getColor(0, Color.BLACK);
ta.recycle();
textView.setTextColor(textColor);
Related
I am making a messaging application in Android, and I want to make an emoji keyboard. I have the layout set up and now I'm going to start coding the layout. However, my light and dark theme is conflicting with this.
The way my dark theme system works is I have an attrs.xml file, and this file declares all of the colors I will use for layouts, and styles.xml defines them for both themes, so whenever I want to use a color, I will use ?attr/iconTint for example and then it will look correct on both themes.
Whenever someone selects a category in the emoji keyboard, it should set the color to an attr attribute so I can change the tint programmatically, and so that the colors will fit the currently selected theme.
The problem with this is that it isn't showing the correct color, and someone suggested that I need to use a colorstate list, but I don't know how.
How do I create a colorstate list that detects what theme I am in, and then sets the tints of imageviews based on the current theme?
I've always used these methods when I want to change the color tint of the ImageView
public static void setTintColor(ImageView img, int color) {
ImageViewCompat.setImageTintList(img, ColorStateList.valueOf(color));
}
public static void clearTintColor(ImageView img) {
ImageViewCompat.setImageTintList(img, null);
}
I have an ordinary EditText field that I would like to programmatically change the underline color for.
<EditText
android:id="#+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
Other answers suggest changing the background color filter like so:
editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
However, I don't see any change when I run the app. Changing the background itself:
editText.setBackground(color)
changes the entire EditText to color - not what I want!
How do I programmatically change the underline color for an EditText, AppCompatEditText or TextInputEditText? I am using version 25.0.1 of the Support Library.
#degs's answer is correct. But just add one little note: the AppCompatEditText#setSupportBackgroundTintList is now annotated with #RestrictTo(LIBRARY_GROUP) which means:
Restrict usage to code within the same group of libraries
Instead use ViewCompat#setBackgroundTintList. So in your example, it should look like this:
ColorStateList colorStateList = ColorStateList.valueOf(color);
ViewCompat.setBackgroundTintList(editText, colorStateList);
You need to set the backgroundTintList (or supportBackgroundTintList) on the EditText to an instance of ColorStateList containing only the color you wish to change the tint to. An easy way to do this in a backwards-compatible way looks like this:
ColorStateList colorStateList = ColorStateList.valueOf(color);
editText.setSupportBackgroundTintList(colorStateList);
This will give the EditText the desired underline color.
Changing the color of EditText underline programmatically can be done using ColorFilter class and other ways but you will have an API level problem. The best way to resolve these type of issues is by using a 9-Patch image.
https://romannurik.github.io/AndroidAssetStudio/nine-patches.html
go here download a set of drawables and put them in your drawable folder and change the background of EditText programmatically( et_joker.setBackground(your_drawable);
) This will work irrespective to API levels.
I couldn't make it working with the above solution when the edit text is in focus.
I had to add colorControlActivated in the theme.
<style name="StyledTilEditTextTheme">
<item name="colorControlNormal">#color/greyLight</item>
<item name="colorControlActivated">#color/gray_ccc</item>
</style>
This worked for me
The default white background of CardView is problematic or am I missing something? When I fill the CardView with normal unstyled Android UI the white text of TextView is not readable e.g.
Has someone an idea what a good fix for that would be? I use the default "Theme.AppCompat" theme and the other background colors look correct. Is that a missing attribute in the Theme.AppCompat? Or am I doing something wrong? The default colors without setting any values manually should be always working or not?
Edit:
I now apply the default background color for the current style to the cardview like this:
TypedArray array = context.getTheme().obtainStyledAttributes(
new int[] { android.R.attr.colorBackground });
card.setCardBackgroundColor(array.getColor(0, 0xFF00FF));
I think its a quite save "default" fix to have at least no text color problems like in the screenshot but the question remains what should be the best practice here and why the cardview has alsways white as the default background color no matter what theme is used..
By this way you can change Cardview background color,
RoundRectDrawable backgroundDrawable = new RoundRectDrawable(backgroundColor, cardView.getRadius());
cardView.setBackgroundDrawable(backgroundDrawable);
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.
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.