I have a text input field that takes name as input. The layout contains an EditText widget inside the TextInputLayout. When the user input is blank, I want to display an error validation message saying - "This is not allowed. Learn more" where "Learn more" is a hyperlink. Is it possible to display an error message which contains a hyperlink using app:setError property?
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:error="@{viewModel.errorMsg}"
app:errorEnabled="true"
app:hintEnabled="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<EditText
android:id="@+id/text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:onTextChanged="@{(text, start, before, count) -> viewModel.validate(textInput, text)}"
android:scrollHorizontally="false"
android:text="@{viewModel.name}"
android:textAlignment="viewStart" />
ViewModel
var errorMsg = MutableLiveData<String?>()
var name = MutableLiveData<String?>()
fun validate(view: EditText, input: CharSequence) {
errorMsg.value = input.toString().let {
if (it.isBlank()) {
view.movementMethod = LinkMovementMethod.getInstance()
Html.fromHtml(view.context.getString(R.string.error_message_empty_name), Html.FROM_HTML_MODE_LEGACY).toString()
} else {
name.value = it
null
}
}
}
strings.xml
<string name="error_message_empty_name" comment="Error text to display when name is empty"><![CDATA[This is not allowed. <a href="https://fb.com">Learn more</a>]]></string>