I want to change selected field of text color using setTextColor. But Android Studio gives me this error. What should I do? Min SDK is 21. This is code of my CustomNumberPicker class:
import android.annotation.TargetApi
import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.NumberPicker
import android.widget.NumberPicker.OnScrollListener
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.widget.TextViewCompat
import ir.partsoftware.cup.R
import timber.log.Timber
class CustomNumberPicker : NumberPicker {
constructor(context: Context?) : super(context) {
init()
}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
init()
}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init()
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
constructor(
context: Context?,
attrs: AttributeSet?,
defStyleAttr: Int,
defStyleRes: Int
) : super(context, attrs, defStyleAttr, defStyleRes) {
init()
}
private fun init() {
setDividerColor(ContextCompat.getColor(context, R.color.color_secondary))
setNumberPickerTextColor(this, ContextCompat.getColor(context, R.color.color_secondary))
this.setOnValueChangedListener { picker, oldVal, newVal ->
setNumberPickerTextColor(this, ContextCompat.getColor(context, R.color.color_secondary))
}
this.setOnScrollListener { numberPicker, scrollState ->
setNumberPickerTextColor(this, ContextCompat.getColor(context, R.color.color_secondary))
}
}
private fun setNumberPickerTextColor(numberPicker: NumberPicker, color: Int) {
if (VERSION.SDK_INT >= VERSION_CODES.Q) {
numberPicker.textColor = color
} else {
val count = numberPicker.childCount
for (i in 0 until count) {
val child = numberPicker.getChildAt(i)
if (child is EditText) {
try {
child.setTextColor(color)
val fieldSelectorWheelPaint = numberPicker.javaClass.getDeclaredField("mSelectorWheelPaint")
val paint = fieldSelectorWheelPaint[numberPicker] as Paint
paint.color = color
fieldSelectorWheelPaint.isAccessible = true
numberPicker.invalidate()
} catch (ex: java.lang.Exception) {
// Ignore
}
}
}
}
}
private fun setDividerColor(@ColorInt color: Int) {
try {
val fDividerDrawable =
NumberPicker::class.java.getDeclaredField("mSelectionDivider")
fDividerDrawable.isAccessible = true
val d = fDividerDrawable[this] as Drawable
DrawableCompat.setTint(d, color)
d.invalidateSelf()
postInvalidate()
} catch (e: Exception) {
Timber.d(e)
}
}
override fun addView(
child: View,
index: Int,
params: ViewGroup.LayoutParams
) {
super.addView(child, index, params)
updateView(child)
}
private fun updateView(view: View) {
if (view is EditText) {
try {
TextViewCompat.setTextAppearance(view, R.style.TextAppearance_PartPay_NumPicker)
val customFont: Typeface? = ResourcesCompat.getFont(context, R.font.iran_yekan)
view.typeface = customFont
// setNumberPickerTextColor(ContextCompat.getColor(context, R.color.color_secondary))
} catch (e: Exception) {
Timber.d(e)
}
}
}
}