Android Material Design TextInputEditText not able to get the string

3.5k views Asked by At

I am using material design in my android app registration page. I have used the following code in my xml file.

XML File

<com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/regEmailId"
            android:hint="Email Id"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </com.google.android.material.textfield.TextInputLayout>

The above xml is rendered from inside a linear layout.

In my Java file, I have written the following:

TextInputLayout regMailId = findViewById(R.id.regEmailId);
Editable regEmail =  regMailId.getEditText().getText();

I followed the option given in this SO post here. Though the answer indicates I should use

String text = textInputLayout.getEditText().getText();

I am getting an error with Android Studio forcing me to change this to:

Change variable text type to editable
Migrate text type to editable
Wrap using String.valueOf()

If I wrap using string value of

String text = String.valueOf( regMailId.getEditText().getText() );

I am not getting the output of variable text.

When I changed it to

Editable regEmail =  regMailId.getEditText().getText();

I am able to toast the input text, but I could not get the actual text to do any validations on top of them. For example, the password and confirmpassword fields, I am not able to compare though I could see the values getting toasted.

When I tried to get the variable type for regEmail, I got the following toasted:

java.lang.String.android.text.SpannableStringBuilder

I tried extracting string by using toString(), getText().toString(), etc to the regEmail. Nothing worked.

How do I get the string of the textinput field in material design layout.

The following is my app gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "com.example.telescope"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.material:material:1.2.0'
    implementation 'com.android.support:multidex:2.0.0'
    implementation 'com.amplifyframework:core:1.1.2'
    implementation 'com.amplifyframework:aws-datastore:1.1.2'
    implementation 'com.amplifyframework:aws-api:1.1.2'
    implementation 'com.amplifyframework:aws-auth-cognito:1.3.0'

}
2

There are 2 answers

2
Mariusz Brona On BEST ANSWER

To get the text in the 'real time', you should add TextChangeListener to the EditText object:

TextInputLayout regMailId = findViewById(R.id.regEmailId);
regEmailId.getEditText().addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {}

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
         //get the String from CharSequence with s.toString() and process it to validation

   }
  });
0
هيثم On

Simply you can add an id attribute to the [com.google.android.material.textfield.TextInputEditText], then you will be able to call it from the Java class.

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

Then you can inflate this in the Java class, and get its object

TextInputEditText textInputEditText = findViewById(R.id.edittext);
// Get text
textInputEditText.getText().toString();
// watch the edit text
textInputEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });