Home > Back-end >  If / Else statement to validate user input in Android Studio
If / Else statement to validate user input in Android Studio

Time:02-11

enter image description hereI am looking to handle bad user input in my Android app.

what i need help with:

If the user does not enter any message and tap the button, the text will be removed, and nothing is displayed. I need to modify the displayMessage method so that it will show 'enter a message' if the user enters nothing into the text field.

 public void displayMessage(View View)
   {
       Context context = getApplicationContext();

      TextView textView = (TextView) findViewById(R.id.textViewOutput);
           EditText editText = (EditText) findViewById(R.id.editTextInput);
           //textView.setText(editText.getText());
      //  String Uname = editText.getText().toString();

       if(editText.length()==0)
       {
           textView.setText("Enter a message");

           editText.setError("Enter a message");
           Toast.makeText(context,"Enter a message", Toast.LENGTH_SHORT).show();
       }
       else{

           textView.setText(editText.getText().toString());

        }

    }
Here is the XML

    ```
     <EditText
            android:id="@ id/editTextInput"
            android:layout_width="250dp"
            android:layout_height="60dp"
            android:layout_marginEnd="80dp"
            android:layout_marginBottom="75dp"
            android:ems="20"
            android:hint="Enter a message then tap ok"
            android:inputType="textPersonName"
            android:text="Name"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/textViewOutput"
            app:layout_constraintVertical_bias="0.287" />
    
        <Button
            android:id="@ id/Button"
            android:layout_width="105dp"
            android:layout_height="47dp"
            android:layout_marginStart="68dp"
            android:layout_marginTop="20dp"
            android:onClick="displayMessage"
            android:text="Button"
            app:layout_constraintStart_toStartOf="@id/editTextInput"
            app:layout_constraintTop_toBottomOf="@id/editTextInput" /

>

CodePudding user response:

Instead of the following condition->

 if(editText.getText().length()==0)
   {
       System.out.println("Enter a message");
   }

Use the below condition ->

if(editText.getText().toString() == "")
    {
       System.out.println("Enter a message");
   }

CodePudding user response:

Try this code,

if(TextUtils.isEmpty(editText.getText().toString().trim()))
   {
       System.out.println("Enter a message");
   }  

trim() method removes white spaces around.

CodePudding user response:

try this :

if(editText.getText().toString().length()==0 && 
 editText.getText().toString() == "")
{
     System.out.println("Enter a message");
}
else
 {
    textView.setText(editText.getText().toString());
 }

CodePudding user response:

Try this code it should work First define your convert your edit text in to String like this

String Uname = editTextname.getText().toString();

Then try this condition !!

if(Uname.length()==0){
    Name.requestFocus();
    Name.setError("Field Is Empty");
}

CodePudding user response:

You are missing the toString() I think.

Then your code should be something like this:

if(editText.getText().toString().length()==0)
{
    Toast.makeText(getApplicationContext(), "Please enter a message", Toast.LENGTH_SHORT).show();
}
else
{
    textView.setText(editText.getText().toString());
}
  • Related