13

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

AmrDeveloper
  • 3,826
  • 1
  • 21
  • 30
Ziad H.
  • 528
  • 1
  • 5
  • 20

5 Answers5

18

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.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 1
    Is there a way to get/access the `TextInputLayout` of a `TextInputEditText` that's "inside" said `TextInputLayout`, something like `editText.getTextInputLayout`? – LuckMan Nov 01 '20 at 04:55
  • 2
    getText() returns Editable. You have to call getText().toString(). I don't know why you use getEditText() on the Layout. I use the TextInputEditText directly: ((TextInputEditText)getView().findViewById(R.id.etTheText)).getText().toString(). – The incredible Jan Dec 01 '20 at 07:51
  • In my case, I was using `view` input argument to `OnClick()` method. But, when I changed `view.findViewById(R.id.input)` to `getView().findViewById(R.id.input_recipient)` everything works like a charm. After that, I get my text with `getView().findViewById(R.id.input_recipient).getText().toString()`. – Петр Воротинцев Jun 26 '22 at 08:54
1

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();
1

In my case, you can't just use String text = textInputLayout.getEditText().getText(); to get the text value from the TextInputEditText.
You need to convert it to Editable or wrap it using String.valueOf() if you gonna wrap it as a String, like this:

XML

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/edt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/request_title"
android:inputType="textCapWords"
android:maxLength="255" />
</com.google.android.material.textfield.TextInputLayout>

Java

TextInputLayout textInputLayout = findViewById(R.id.edt_title);

String strTitle = String.valueOf(textInputLayout.getEditText().getText());

or

Editable strTitle = textInputLayout.getEditText().getText();

Then you can use both above as a string like this

Toast.makeText(YourClassName.this, strTitle, Toast.LENGTH_SHORT).show();
Muhammad Faisal
  • 734
  • 10
  • 24
  • 1
    You can also use `toString()` method on Editable object, because documentation of this method said: `Return a String containing a copy of the chars in this buffer.`. I don't know why but my version of material library does not contain `getEditText()` method but `getEditableText()` I think they doing the same thing (returning Editable). Material lib version: `1.1.0` – Falcon Oct 25 '22 at 08:49
  • toString() woked for me. In kotlin use: `binding.myTextInputEditText.text.toString()` – Daniel Cettour Jan 18 '23 at 14:02
0

First Activity:

String uname = userV.getEditText().getText().toString();
    
Intent intent1 = new Intent(VolunteerActivity.this, VolunteerView.class);
intent1.putExtra("USER_NAME", uname);
startActivity(intent1);

Second Activity:

Bundle extras = getIntent().getExtras();
if (extras != null) 
{
    String uname = extras.getString("USER_NAME");
    tv2.setText(uname);
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Kinza
  • 1
  • 1
0

In Kotlin, I was having the same problem while getting text from TextInputEditText and this is the solution I applied:

private lateinit var emailForAuthentication : TextInputEditText
private lateinit var passwordForAuthentication : TextInputEditText

emailForAuthentication = findViewById(R.id.emailedittext)

val email = emailForAuthentication.editableText
val password = passwordForAuthentication.editableText

Log.d("login","$email")

Explaination: By wrapping the Editable Type variable in the String, I am able to showcase the text from the TextInputEditText without getting an empty String.

Note: I am not getting a reference from TextInputLayout like in previous answers but I am using TextInputEditText to get a reference.

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email"
        style="?attr/textInputFilledStyle"
        app:endIconDrawable="@drawable/mailicon"
        app:endIconContentDescription="mail icon"
        app:endIconMode="custom"
        android:textColorHint="@color/black"
        app:boxCornerRadiusBottomEnd="50dp"
        app:boxCornerRadiusBottomStart="50dp"
        app:boxCornerRadiusTopEnd="50dp"
        app:boxCornerRadiusTopStart="50dp"
        app:boxStrokeColor="@color/black"
        android:layout_margin="@dimen/_3sdp">
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/emailedittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:background="@color/white"/>
    </com.google.android.material.textfield.TextInputLayout>
Achintya
  • 137
  • 1
  • 6