How to reference inverse text color - android

I've a normal TextView:
<TextView
android:id="#+id/item"
android:layout_width="wrap_content"
android:layout_heigh="wrap_content"
android:textColor="#color/text_on_background"/>
It references #color/text_on_background. I've a few build flavors of my app, each with different color scheme. So I need to change the color of the text sometimes. But sometimes not, and I want to use default inverse text color from theme.
So i tried these:
<color name="text_on_background">?android:textColorPrimaryInverse</color>
<color name="text_on_background">?android:attr/textColorPrimaryInverse</color>
And it always ended with error about inflating the TextView. And I couldn't find any #color/…inverse… or #android:color/…inverse….
Any clue how to do that?

If you want to inverse the Color means , you can do (MAX - COLOR_VAL) in all (R,G,B)
for example
I Assume F3FAB6 - this is your color code means you can do following way
Color.rgb(255-243, 255-250,255-182);
You can get Inverse Color.

Related

Seeking explanation for discrepancy between hardcoded and referenced colour in Android Studio

I'm having a play with Android Studio.
I'm trying to change the background colour. I've found how to change it but what's puzzling me is that if I substitute the definition (ie the actual hex code) of the colorAccent property I don't see the same colour.
This is the original, which uses #color/colorAccent, which ultimately is a hex code defined in the class R.color:
This is what I get when I substitute the hex code that colorAccent references:
As you can see, I get a shade of grey instead of a shade of red. Why is this?
Thanks.
[Android Studio 3.2]
It is not shade of red beacuse depend on your select.
You can change color in XML if you hardcode it.
click on side color on left of code :
then choose your color:
But if you set it like android:background="#color/colorPrimary"
you should change it in color XML
The hex value for colorAccent in the R.java doesn't refer to the color, but rather the automatically generated unique id for that XML attribute in colors.xml. As you can see, the hex values for the variables are all in-order and increasing by 1 each time.
The actual hex code for the color denoted by colorAccent is defined in colors.xml, and substituting that directly in the android:background attribute will work as you expect it to.

Font color in string.xml file in Android crashes on older OS

I've created an app in which release is very close. However, I've found that in older Androids (I know for sure on versions 4.1 and older) the color tag in the strings.xml file causes crashes. The tag is changing the font to red.
<font fgcolor="red">Text goes here</font>
There is a lot of text in the app, and some of the strings of red words are embedded in a longer string. Is there any way to avoid this crash? I know it works great on Android 4.4 systems...not 100% sure about 4.2 and 4.3. Any ideas on alternate ways to create red text? Thanks.
Here are some ways to create red text on a TextView. The implementation should be the same (or very similar) for other views.
You can set the text color programatically in the code:
TextView yourTextView = (TextView)findViewById(R.id.yourTextViewId);
yourTextView.setText("Your text here");
yourTextView.setTextColor(Color.parseColor("#FF000000"));
If you want to set this text color in the XML do so like this:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/holo_red_light"
android:text="Your text here"/>
If you want to set your own color instead of using androids pre-defined colors, you can do this by creating a color resource XML file and adding your colors as follows:
<resources>
<color name="red">#FF000000</color>
<!-- Add other colors here -->
</resources>

Setting #null for textColor dynamically in android

I need an EditText to appear as a TextView for a form that will change from being write to read only. I have found a useful snippet of code to do this in a layout:
<EditText android:id="#+id/title"
android:layout_width="fill_parent"
style="?android:attr/textViewStyle"
android:background="#null"
android:textColor="#null" />
I need to do this dynamically (not in the layout). I know that the background can be set to #null by using setBackgroundResource(null). Because setTextColor(int color) takes an int, I assume that a specific color must be selected. What is the correct color to choose that would be the equivalent to #null? A color from the default theme? Or is there a better way to do this?
You can create color in your string.xml like
<color name="transparent">#00000000</color>
Then you can assign it to like
txtTitle.setTextColor(getResources().getColor(R.color.transparent));
#00000000 is equal to null.
You can call these methods on any View to disable editing or clicking. It will effectively make the view read only. Set them to true to re-enable them.
view.setFocusable(false);
view.setClickable(false);
view.setLongClickable(false);

Android XML Color shows up as gray?

I'm trying to use Paint.setColor() with a color from res/values/colors.xml but it keeps coming out "grayish". If I use a string literal instead and use Paint.parseColor() it shows up correctly. What's going on?
onDraw()
p.setColor (Color.parseColor ("#82ef82")); // <- this works
p.setColor (R.color.PeaGreen); // <- this is gray
colors.xml
<color name="PeaGreen">#82ef82</color>
R.color.PeaGreen is not a color, it's a resource id for a color resource. But since colors are represented by integers and so are resource id's, it's not throwing any warning or error. You need to do a little bit of work to get the actual color from a resource id:
p.setColor(context.getResources().getColor(R.color.PeaGreen));
There are also a few color constants defined in the Color class and you can use them like so:
p.setColor(Color.RED);

Setting alpha for text color for Button

I tried setting textColor to have alpha, but it doesn't seem to work.
<Button
android:id="#+id/multi_player"
android:text="#string/multi_player"
android:textColor="#AAFFFFFF" />
No matter what alpha level I give it, the text still shows up as fully opaque white.
This appears to be an issue on (some versions of) Android, where alpha is not considered when set using #rgb or #argb formats.
Instead, move it to a resource file:
<color name="blah">#aaffffff</color>
Then, call it using:
android:textColor="#color/blah"

Categories

Resources