I want to get a text from the material design's TextInputLayout using a custom end Icon.
I tried to do that:
TextInputLayout textInputCustomEndIcon = findViewById(R.id.editText);
final TextInputEditText editText = new TextInputEditText(textInputCustomEndIcon.getContext());
textInputCustomEndIcon.setEndIconOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editText.getText() != null) {
String text = editText.getText().toString();
Log.i("MainActivity", "setEndIconOnClickListener:"+ text);
}
}
});
But I get an empty text, not null but empty!!
Just use: TextInputLayout textInputLayout = findViewById(R. id. custom_end_icon); String text = textInputLayout.
Now you can simply do input. setError(..) for new error and input. setErrorEnabled(false) to remove it.
You can use the TextInputLayout to display error messages according to the material design guidelines using the setError and setErrorEnabled methods. In order to show the error below the EditText use: TextInputLayout til = (TextInputLayout) findViewById(R. id.
Using something like that:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/custom_end_icon"
...>
<com.google.android.material.textfield.TextInputEditText
../>
</com.google.android.material.textfield.TextInputLayout>
Just use:
TextInputLayout textInputLayout = findViewById(R.id.custom_end_icon);
String text = textInputLayout.getEditText().getText();
Your issue is here:
final TextInputEditText editText = new TextInputEditText(textInputCustomEndIcon.getContext());
You are only creating a new instance of TextInputEditText
, it isn't the TextInputEditText
inside the TextInputLayout
.
I had the same problem as you. I just solved it this way. In the String variable I need to validate that the data has been correctly inserted with .trim()
String vname=Name.getEditText().getText().toString().trim();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With