Asuwathaman R C
Asuwathaman R C

Reputation: 303

Android Material TextInputLayout and TextInputEditText

I'm trying to check whether the Material TextField is empty or not. If it is empty, error should be enabled. How to set error in Material TextField. And if the text field is not empty how can I fetch the text from the Material TextField.

All this need's to be done in mainactivity.java

myxml.xml

    <com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_project_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="32dp"
android:hint="Project Name"
app:boxCornerRadiusTopEnd="15dp"
app:boxCornerRadiusTopStart="15dp"
app:endIconMode="clear_text"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/projectname_np"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>

Upvotes: 1

Views: 1084

Answers (1)

kshitij86
kshitij86

Reputation: 129

To check if a text field is empty, you can do this in your Java code -

if(textInputLayout.getText().toString().matches(""){
    // Do something
}

You can set the text in the TextInputLayout by using textInputLayout.setText() method in your MainActivity when you want to use it. Check out the documentation here.

Upvotes: 1

Related Questions