Set colour to text in Android activity? - android

I would like to set a red colour text in my app, but I don't know how.
Please provide references. Thanks.

If you want it to set in XML layout then use:
<TextView
...
...
android:textColor="#FF0000" >
</TextView>
If you want to set programmatically then use:
textview.setTextColor(Color.RED);
//textview must be defined in your class

Read the docs for TextView, specifically for textColor.

Use the following in your xml where you want to change the color to red and by using the other hexacode of color you can change to any other color. This is an example to set the color to red:
android:textColor="#FF0000"

Use this in your layout:
<TextView>
....
textColor="red"
....
</TextView>

Create a custom style resource which uses an Android Theme as a parent then override the text colors as defined in the Android theme.xml. Reference this new style in your AndroidManifest.xml's application tag.

Related

How to get and set style attribute in code?

Suppose the following code:
<MyCustomComponent
style="#style/Base.TextAppearance.AppCompat.Headline"
android:padding="#dimen/padding_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
In the custom component I have an EditText which I need to set its style attribute to #style/Base.TextAppearance.AppCompat.Headline in java code, according to the style attribute in the layout. How can I do that?
I am creating a custom component therefore I also need to know which style has been selected in layout by user of my component.
Is it available through AttributeSet? If yes please let me know how?
For styling TextViews programmatically, you must have a style inheriting a TextAppearance style, and you can apply it using the following code:
if (Build.VERSION.SDK_INT < 23) { textView.setTextAppearance(context,android.R.style.TextAppearance_Small); } else { textView.setTextAppearance(android.R.style.TextAppearance_Small); }
Define a custom style in style.xml like this:
<style name="MyHeadlineTextAppearance" parent="Base.TextAppearance.AppCompat.Headline"/>
Then set it to your TextView as below:
textView.setTextAppearance(context, R.style.MyHeadlineTextAppearance);

Programmatically changing underline color of EditText

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

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);

How to give colors for autocompletetextview in android?

How to give colors for autocompletetextview in android.
I am using these two colors for background and text-color
android:background = "#000000"
android:textclolor = "#ffffff".
But the problem is if i type some texts on autocompletetextview then the listable values are showing in white color and same to the text-colors too.
I can't able to fix only a color for the whole autocompletetextview. How to do this?
suggestions please!..
thanks in advance
you can use android:popupBackground attributes in AutoCompleteTextView in layout xml. it works for your problem.
As for example like this ...
<?xml version="1.0" encoding="utf-8"?>
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/autocomplete_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:popupBackground="#ffffff" />
you can do it at runtime like
AutoCompleteTextView searchView = ...
searchView.setDropDownBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.colorId)));
and if you want to hange it in your xml file then you have to give
android:popupBackground property to your Autocomplete view.
enjoy your code:)
To set the popup background use popupBackground xml property for AutoCompleteTextView. I also advice you to use Themes for text colors, sizes etc. When you want to style a list on the AutoCompleTextView popup (for example change a divider) you can do it from theme settings like that:
<item name="android:dropDownListViewStyle">#style/MyListViewStyle</item>

TextView setTextColor() not working

I programmatically create a list (no a ListView, just adding them to the parent) of such elements:
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:layout_weight="1">
<TextView android:id="#+id/filiale_name"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="#+id/lagerstand_text"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="10sp" android:textColor="#color/red"/>
</LinearLayout>
Also, I have defined some colors in values/colors.xml. As you see, the TextView with id "lagerstand_text" has set it's color to red by default. That works.
When creating the elements in Java, I do
lagerstandText.setText("bla");
and for some elements also I do
lagerstandText.setTextColor(R.color.red);
and other colors. While the elements on which I don't call setTextColor() are red, all others are grey, no matter which color I chose (even if it's the same red again).
Why is that?
The documentation is not very verbose about this, but you cannot use just the R.color integer when calling setTextColor. You need to call getResources().getColor(R.color.YOURCOLOR) to set a color properly.
Use the following to set color of your text programmatically:
textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));
Starting with the support library 23 you have to use the following code, because getColor is deprecated:
textView.setTextColor(ContextCompat.getColor(context, R.color.YOURCOLOR));
So, there are many ways to achieve this task.
1.
int color = Integer.parseInt("bdbdbd", 16)+0xFF000000;
textview.setTextColor(color);
2.
textView.setTextColor(getResources().getColor(R.color.some_color));
3.
textView.setTextColor(0xffbdbdbd);
4.
textView.setTextColor(Color.parseColor("#bdbdbd"));
5.
textView.setTextColor(Color.argb(a_int, r_int, g_int, b_int));
1.standard color u prefer please go with below .
textview.setTextColor(Color.select_color)
2.here want to use custwom color add it in color.xml file
textview.setTextColor(getResources().getColor(R.color.textbody));
or
textView.setTextColor(Color.parseColor("#000000"));
or
subText.setTextColor(Color.rgb(255,192,0));
For future reference, you can use the follow:
String color = getString(Integer.parseInt(String.valueOf(R.color.my_color)));
my_textView.setTextColor(Color.parseColor(color));
This way you can make use of your Color Resources.
textView.setTextColor(Color.RED);
The integer id for a particular color(defined in xml layout) defined in R class cannot be passed as a parameter to setTextColor() method of View class.
You must obtain the parameter of the setTextColor() by the following line of code :
int para=getResources().getColor(R.color.your_color,null);
view.setTextColor(para,null);
The method getColor(int id) has been depreciated...instead use getColor(int id,Resources.Theme theme) as in the line of code above.
The `second parameter( theme )` can be null

Categories

Resources