Как программно установить оттенок TextView в Android

Чтобы программно установить оттенок TextView в Android, вы можете использовать следующие методы:

Метод 1: использование DrawableCompat (рекомендуется для обратной совместимости):

import androidx.core.graphics.drawable.DrawableCompat;
import android.widget.TextView;
import android.graphics.drawable.Drawable;
import android.content.res.ColorStateList;
TextView textView = findViewById(R.id.your_textview_id);
Drawable[] drawables = textView.getCompoundDrawablesRelative();
// Replace index with the desired position of the drawable (left, top, right, bottom)
Drawable drawable = drawables[index];
// Create a new instance of the drawable with desired tint color
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(drawable, ColorStateList.valueOf(your_tint_color));
// Set the modified drawable back to the TextView
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, drawables[1], drawables[2], drawables[3]);

Метод 2: использование setTintList() (требуется уровень API 23 или выше):

import android.widget.TextView;
import android.graphics.drawable.Drawable;
import android.content.res.ColorStateList;
TextView textView = findViewById(R.id.your_textview_id);
Drawable[] drawables = textView.getCompoundDrawablesRelative();
// Replace index with the desired position of the drawable (left, top, right, bottom)
Drawable drawable = drawables[index];
// Create a ColorStateList with desired tint color
ColorStateList colorStateList = ColorStateList.valueOf(your_tint_color);
// Apply the color state list to the drawable
drawable.setTintList(colorStateList);
// Set the modified drawable back to the TextView
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, drawables[1], drawables[2], drawables[3]);

Метод 3: использование setColorFilter() (не поддерживается на уровне API 29):

import android.widget.TextView;
import android.graphics.drawable.Drawable;
TextView textView = findViewById(R.id.your_textview_id);
Drawable[] drawables = textView.getCompoundDrawablesRelative();
// Replace index with the desired position of the drawable (left, top, right, bottom)
Drawable drawable = drawables[index];
// Apply the color filter with desired tint color
drawable.setColorFilter(your_tint_color, PorterDuff.Mode.SRC_IN);
// Set the modified drawable back to the TextView
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, drawables[1], drawables[2], drawables[3]);