How to Programmatically Change the Colour of a Textview, to a Colour Saved in an Attributes File

How to programmatically change the colour of a textView, to a colour saved in an attributes file?

First define Color you want to set on Textview via color.xml file, for example ->

<color name="colorPrimary">#3F51B5</color>

Then inside your java file, This single line will set color on textview, note that the color will be fetched from defined colors inside color.xml file. ->

textView.setTextColor(getResources().getColor(R.color.colorPrimary));

How to set the text color of TextView in code?

You should use:

holder.text.setTextColor(Color.RED);

You can use various functions from the Color class to get the same effect of course.

  • Color.parseColor (Manual) (like LEX uses)

    text.setTextColor(Color.parseColor("#FFFFFF"));
  • Color.rgb and Color.argb (Manual rgb) (Manual argb) (like Ganapathy uses)

    holder.text.setTextColor(Color.rgb(200,0,0));
    holder.text.setTextColor(Color.argb(0,200,0,0));
  • And of course, if you want to define your color in an XML file, you can do this:

    <color name="errorColor">#f00</color>

    because the getColor() function is deprecated1, you need to use it like so:

    ContextCompat.getColor(context, R.color.your_color);
  • You can also insert plain HEX, like so:

    myTextView.setTextColor(0xAARRGGBB);

    Where you have an alpha-channel first, then the color value.

Check out the complete manual of course, public class Color extends Object.


1This code used to be in here as well:

textView.setTextColor(getResources().getColor(R.color.errorColor));

This method is now deprecated in Android M. You can however use it from the contextCompat in the support library, as the example now shows.

Programmatically change the value of a color resource obtained from API response

If you take a look at the Accessing Resources document, what it says is that ...

Once you provide a resource in your application, you can apply it by referencing its resource ID. All resource IDs are defined in your project's R class, which the aapt tool automatically generates.

Furthermore,

When your application is compiled, aapt generates the R class,
which contains resource IDs for all the resources in your res/
directory. For each type of resource, there is an R subclass (for
example, R.drawable for all drawable resources), and for each
resource of that type, there is a static integer (for example,
R.drawable.icon). This integer is the resource ID that you can use
to retrieve your resource.

What this is saying, essentially, is that pretty much everything held as a resource in the res/ directory is compiled and referenced as an unchangeable constant. It is for this reason that the values of resource elements cannot be changed programmatically/at runtime, because they are compiled. As opposed to local/global variables & SharedPreferences, resource elements are represented in program memory as fixed, unchangeable objects. They are held in a special read-only region of program memory. In this regard, see also Changing value of R.String Programmatically.

What you can do is, to avoid using the same code at a thousand places in your project, create a common function that changes the value of the color in the SharedPreferences and use this method everywhere. I'm sure you knew this already, of course.

To reduce the amount of code you need to add to the project, there is an alternative. I have previously used the calligraphy library which allowed me to fix the font style & color throughout the app. This may be of some good use to you, check it out ...

How can I change the color of a part of a TextView?

Spannable is more flexible:

String text2 = text + CepVizyon.getPhoneCode() + "\n\n"
+ getText(R.string.currentversion) + CepVizyon.getLicenseText();

Spannable spannable = new SpannableString(text2);

spannable.setSpan(new ForegroundColorSpan(Color.WHITE), text.length(), (text + CepVizyon.getPhoneCode()).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

myTextView.setText(spannable, TextView.BufferType.SPANNABLE);

Get color value programmatically when it's a reference (theme)

This should do the job:

TypedValue typedValue = new TypedValue();
Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.theme_color, typedValue, true);
@ColorInt int color = typedValue.data;

Also make sure to apply the theme to your Activity before calling this code. Either use:

android:theme="@style/Theme.BlueTheme"

in your manifest or call (before you call setContentView(int)):

setTheme(R.style.Theme_BlueTheme)

in onCreate().

I've tested it with your values and it worked perfectly.

Setting the Color of a TextView Drawable

Try below solution

private void setTextViewDrawableColor(TextView textView, int color) {
for (Drawable drawable : textView.getCompoundDrawables()) {
if (drawable != null) {
drawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(textView.getContext(), color), PorterDuff.Mode.SRC_IN));
}
}
}

android TextView: setting the background color dynamically doesn't work

Use et.setBackgroundResource(R.color.white);

How do I set the border colour of my TextView programmatically?

You are changing the color of the drawable that you load but not the one used in the TextView. Add the following line to your code to set the background drawable in the TextView:

findViewById(R.id.textView1).setBackground(layerDrawable);

You can also get the background for the TextView directly with

LayerDrawable layerDrawable = (LayerDrawable) findViewById(R.id.textView1).getBackground();

Here is a set of code to change the background and the stroke color:

LayerDrawable layerDrawable = (LayerDrawable) findViewById(R.id.textView1).getBackground();
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
.findDrawableByLayerId(R.id.textbox_shape);
// Change background color
gradientDrawable.setColor(Color.parseColor("#DA850B"));
// Change stroke color. (Assumes 5px stroke width.)
gradientDrawable.setStroke(5, Color.parseColor("#FF0000"));


Related Topics



Leave a reply



Submit