How to write style to error text of EditText in android?

前端 未结 7 1804
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 12:37

I\'m trying to write new custom style for my android application. I need to give style to errorText which appears after setting setError in EditText

7条回答
  •  独厮守ぢ
    2020-11-27 13:01

    This works fine!

    private fun setErrorOnSearchView(searchView: SearchView, errorMessage: 
    String) {
    val id = searchView.context
            .resources
            .getIdentifier("android:id/search_src_text", null, null)
    val editText = searchView.find(id)
    
    val errorColor = ContextCompat.getColor(this,R.color.red)
    val fgcspan = ForegroundColorSpan(errorColor)
    val builder = SpannableStringBuilder(errorMessage)
    builder.setSpan(fgcspan, 0, errorMessage.length, 0)
    editText.error = builder
    }
    

提交回复
热议问题