Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text from material design TextInputLayout correctly?

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!!

like image 858
Ziad H. Avatar asked Sep 05 '19 02:09

Ziad H.


People also ask

How do I get TextInputLayout from text?

Just use: TextInputLayout textInputLayout = findViewById(R. id. custom_end_icon); String text = textInputLayout.

How do I remove TextInputLayout error?

Now you can simply do input. setError(..) for new error and input. setErrorEnabled(false) to remove it.

How do I show error in TextInputLayout?

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.


2 Answers

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.

like image 140
Gabriele Mariotti Avatar answered Oct 20 '22 00:10

Gabriele Mariotti


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();
like image 39
cypherpunk5 Avatar answered Oct 20 '22 02:10

cypherpunk5