How to control the contrast of text? - android

How do you change the color contrast of text when the background changes? for example If I was to have a black background, black text would not be visible.

This might be helpful: http://developer.android.com/guide/topics/resources/color-list-resource.html
It is a way you can set a color that will change based on certain circumstances.
For example, say you have a TextView that you want to have white text while it is enabled and black text when it is disabled. You can set that up in a xml file using the references in the link above, and then in your xml layout where you define the TextView set the android:textColor to #color/my_text_color. (my_text_color being the xml color list file you created)
Then, as the TextView changes from enabled to disabled (or whatever you end up setting up in the xml file) the color will change automatically as well.
That's one way to do it. However, you might want to try to clarify what you are looking for as it isn't perfectly clear in your question.
Update
After Matt's comment, here is a method you could use to get an inverted color value. There is probably a better way but this should work.
private int getInverseColor(int color){
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
int alpha = Color.alpha(color);
return Color.argb(alpha, 255-red, 255-green, 255-blue);
}
You could programmaticly get the color int from a view such as a TextView using one of the getTextColor() methods. You may have to tinker with a Color State List like I linked to above to get the color you want. Then pass that color to the method above to get the inverted color int and set it with one of the setTextColor() methods.

Related

Change background colour of view programatically

I am trying to change the background colour of my View programatically, using a colour that I defined in my resources. Other SO posts explain to do it like this:
int color = ContextCompat.GetColor(this.context, Resource.Color.my_color_background);
this.myView.SetBackgroundColor(color);
But this doesn't work for me, because SetBackgroundColor takes a Color, not an int... what am I doing wrong here?
I solved it by just creating the Color object myself and setting it with that:
int color = ContextCompat.GetColor(context, Resource.Color.my_color);
this.view.SetBackgroundColor(new Color(color));
Not sure why the constructor is different in Xamarin though...

changing textcolor in android edittext programmatically

I have to create the background color for edittext means am getting the background correctly.
TestPost.this.findViewById(R.id.revieew)
.setBackgroundColor(color);
But how can i get the textcolor for these edittext .
TestPost.this.findViewById(R.id.revieew)
.setTextColor(color);
Please give me a solution ???
i have to pick the green color means have to enter the text is green....here i have done these for background color.i have to set the background color is green means have to pick the color green from color picker means the bexkground is successfully displaying...how can i set the textcolor from color picker ???
EDIT:
reviewEdit.setTextColor(color);
means am getting the color is successfully...But i didn't change the whole text.
I want to change the color for selected text alone...
For EG:
The text is : The customer is waiting for your reply means
Have to pick a color green, have to write the The customer is waiting have to display green color for these text alone.after that have to pick a color pink means have to display the for your reply as pink color.
This is exactly i need ...how can i implement these ???
((TextView)TestPost.this.findViewById(R.id.revieew)).setTextColor(color);
See the docs here
What you are doing is almost correct. The method findViewById() returns a View which you need to cast into a TextView/EditText (depending on how you have the view with that id defined in your xml) and then the method will be available for use.
EditText text = (EditText) findViewById(R.id.revieew);
text.setTextColor(color);
Try this TestPost.this.findViewById(R.id.revieew).getCurrentTextColor();
Found here: https://stackoverflow.com/a/6746131/2065418
Your method was correct for getting the color use below code
int color =editText.getCurrentTextColor();
Log.d("color", String.valueOf(color));

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 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.

How to set background color of a View

I'm trying to set the background color of a View (in this case a Button).
I use this code:
// set the background to green
v.setBackgroundColor(0x0000FF00 );
v.invalidate();
It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View?
Thanks.
You made your button transparent. The first byte is the alpha.
Try v.setBackgroundColor(0xFF00FF00);
When you call setBackgoundColor it overwrites/removes any existing background resource, including any borders, corners, padding, etc.
What you want to do is change the color of the existing background resource...
View v;
v.getBackground().setColorFilter(Color.parseColor("#00ff00"), PorterDuff.Mode.DARKEN);
Experiment with PorterDuff.Mode.* for different effects.
Several choices to do this...
Set background to green:
v.setBackgroundColor(0x00FF00);
Set background to green with Alpha:
v.setBackgroundColor(0xFF00FF00);
Set background to green with Color.GREEN constant:
v.setBackgroundColor(Color.GREEN);
Set background to green defining in Colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="myGreen">#00FF00</color>
<color name="myGreenWithAlpha">#FF00FF00</color>
</resources>
and using:
v.setBackgroundResource(R.color.myGreen);
and:
v.setBackgroundResource(R.color.myGreenWithAlpha);
or the longer winded:
v.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreen));
and:
v.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreenWithAlpha));
You can set the hex-color to any resource with:
View.setBackgroundColor(Color.parseColor("#e7eecc"));
// set the background to green
v.setBackgroundColor(0x0000FF00 );
v.invalidate();
The code does not set the button to green. Instead, it makes the button totally invisible.
Explanation: the hex value of the color is wrong. With an Alpha value of zero, the color will be invisible.
The correct hex value is 0xFF00FF00 for full opacity green. Any Alpha value between 00 and FF would cause transparency.
For setting the first color to be seen on screen, you can also do it in the relevant layout.xml (better design) by adding this property to the relevant View:
android:background="#FF00FF00"
and what is the correct way to change
the background color on any View?
On any View? What you have is correct, though you should drop the invalidate() call.
However, some Views already have backgrounds. A Button, for example, already has a background: the face of the button itself. This background is a StateListDrawable, which you can find in android-2.1/data/res/drawable/btn_default.xml in your Android SDK installation. That, in turn, refers to a bunch of nine-patch bitmap images, available in multiple densities. You would need to clone and modify all of that to accomplish your green goals.
In short, you will be better served finding another UI pattern rather than attempting to change the background of a Button.
try to add:
setBackgroundColor(Color.parseColor("#FF0000"));
I use at API min 16 , target 23
Button WeekDoneButton = (Button) viewWeeklyTimetable.findViewById(R.id.week_done_button);
WeekDoneButton.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorAccent));
mButton.setBackgroundColor(getResources().getColor(R.color.myColor));
You can simple use :
view.setBackgroundColor(Color.parseColor("#FFFFFF"));
You can simple use :
view.setBackgroundColor(Color.rgb(0, 198, 255));
This question talks about changing the background color of a view. In one of the answers, the person explains how to change the background color during runtime. Obviously you are going to look into how to modify other objects on the screen, but this should give you a good start by at least allowing you to modify the background color of the view on button click.
Stating with Android 6 use ContextCompact
view.setBackgroundColor( ContextCompat.getColor(this, R.color.your_color));
This works for me
v.getBackground().setTint(Color.parseColor("#212121"));
That way only changes the color of the background without change the background itself. This is usefull for example if you have a background with rounded corners.
In kotlin you could do it like this:
val backgroundColor = R.color.whatever_color_you_like
view.setBackgroundColor(getColorCompat(backgroundColor))
Where getColorCompat() is an extension function:
/**
* Extension method to provide simpler access to {#link ContextCompat#getColor(int)}.
*/
fun Context.getColorCompat(color: Int) = ContextCompat.getColor(this, color)
view.setBackgroundColor(R.color.primaryColor);
Adds color to previous color value, so i have a different color.
What works for me is :
view.setBackgroundResource(R.color.primaryColor);
Let suppose we have a primary color in values=>colors.xml as:
<resources>
<color name="primary">#FDD835</color>
</resources>
so if we want to use our custom color into setBackgroundColor(#ColorInt int Color) then we just need an annotation #SuppressLint("ResourceAsColor") with constructor/method which will be used as:
#SuppressLint("ResourceAsColor")
public _LinearLayout(Context context) {
super(context);
// Formatting our layout : )
super.setBackgroundColor(R.color.primary);
....
}
You must pass an int in the argument.
First Example:
view.setBackgroundColor(-500136)
Second Example:
int colorId = R.color.green;
view.setBackgroundResource(colorId);
This should work fine: v.setBackgroundColor(0xFF00FF00);
I tried all the above ways. But I havent achieve what i need. Here is my try.
If you are using hexcode for color and want to set the color as background of image, then this is the kotlin code.
val bitmap = Bitmap.createBitmap(30, 30, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val colorCode = "#ffffff"
canvas.drawColor(Color.parseColor(colorCode))
mImageViewLogo.setImageBitmap(bitmap)
When calling setBackgroundColor on a view you need to set the alpha value to a non-zero value (e.g. 0xFF), otherwise the color will not show up.
TextView tv = (TextView)findViewById(R.id.myTextview);
int rgb = 0xF05922; // Orange
tv.setBackgroundColor(0xFF000000|rgb); // Use bitwise OR to add alpha to RGB value

Categories

Resources