Check if Edittext is empty

Asked

Viewed 3,996 times

2

Hello guys I’m making an app on android studio but I don’t know exactly how to check if the text fields are empty, I tried the following solution in the code below :

public class MainActivity extends AppCompatActivity {

EditText edt1,edt2;
TextView tv3;
float n1 = 0.70f;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    edt1 = (EditText) findViewById(R.id.editText);
    edt2 = (EditText) findViewById(R.id.editText2);
    tv3 = (TextView) findViewById(R.id.textView3);

}


public void calcular(View v){

    double valor1 = Double.parseDouble(edt1.getText().toString());
    double valor2 = Double.parseDouble(edt2.getText().toString());


    double resultado1 = valor1 * n1;

    if (edt1.getText().toString().equals("")){
       tv3.setText("Campo vazio");
    }

    if (edt2.getText().toString().equals("")){
       tv3.setText("Campo vazio");
    }

    if (resultado1 < valor2) {
        tv3.setText("Vale a pena Abastecer com Gasolina");
    }


    else {
        tv3.setText("Vale a pena Abastecer com Alcool");
    }


    }

}

Further by clicking on the run with the empty fields I get the following error:

08-08 15:21:53.288 25012-25012/com.dolardehoje.formulario W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41a19930)
08-08 15:21:53.298 25012-25012/com.dolardehoje.formulario E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
        at android.view.View.performClick(View.java:4380)
        at android.view.View$PerformClick.run(View.java:18094)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5279)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
        at android.view.View.performClick(View.java:4380) 
        at android.view.View$PerformClick.run(View.java:18094) 
        at android.os.Handler.handleCallback(Handler.java:725) 
        at android.os.Handler.dispatchMessage(Handler.java:92) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:5279) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
        at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.NumberFormatException: Invalid double: ""
        at java.lang.StringToReal.invalidReal(StringToReal.java:63)
        at java.lang.StringToReal.parseDouble(StringToReal.java:248)
        at java.lang.Double.parseDouble(Double.java:295)
        at com.dolardehoje.formulario.MainActivity.calcular(MainActivity.java:38)
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
        at android.view.View.performClick(View.java:4380) 
        at android.view.View$PerformClick.run(View.java:18094) 
        at android.os.Handler.handleCallback(Handler.java:725) 
        at android.os.Handler.dispatchMessage(Handler.java:92) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:5279) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
        at dalvik.system.NativeStart.main(Native Method) 

6 answers

7


The mistake happens because you are trying to get one Double of a string that is null or has no convertible value.

Change the code so that validation of Edittext before obtaining the values in Double

public void calcular(View v){

    if (edt1.getText().toString().trim().equals("") ||
        edt2.getText().toString().trim().equals("")){

       Toast.makeText(this, "Valores incorretos",Toast.LENGTH_LONG).show();

    }else{
        double valor1 = Double.parseDouble(edt1.getText().toString());
        double valor2 = Double.parseDouble(edt2.getText().toString());

        double resultado1 = valor1 * n1;

        if (resultado1 < valor2) {
            tv3.setText("Vale a pena Abastecer com Gasolina");
        }
        else {
            tv3.setText("Vale a pena Abastecer com Alcool");
        }
    }
}

To ensure that the user only enters valid values (numbers) add the following attributes to the Edittext edt1 and edt2

android:text="0" 
android:inputType="numberDecimal"
  • 1

    managed using your solution, thank you!

3

If what you want is just check if the field is empty try to use the following line:

if(edt1.getText().toString().equals("")){
   //Colocar o que vc quer que aconteça caso esteja vazio.
}

or else:

if(!edt1.getText().toString().equals("")){
   //Colocar o que vc quer que aconteça caso não esteja vazio.
}

I’m basically choosing the field and picking up what’s in it. And with . What("") I’m comparing it to an empty field.

I hope I’ve helped.

  • Even with this error when I click to run and the error app

  • Edit your question and post the error q is giving.

  • edited with updated code

  • Re-edit and place the error displayed.

  • The error is the following "your application has stopped"

  • I’m flando the "LOG" q is presented on the "Android Monitor" at the bottom of android studio.

  • added the error log

Show 2 more comments

1

"thread exiting with uncaught Exception"

declare value1 and value2 global and use it here :

try
{
    valor1 = Double.parseDouble(edt1.getText().toString());
    valor2 = Double.parseDouble(edt2.getText().toString());
} 
catch(NumberFormatException e)
{
    tv3.setText("Valor Invalido");
}

1

Based on what the ramaral did, because this correct, I would only change a few points and add a few lines for the code to be more complete.

public void calcular(View v){

   if (edt1.getText().toString().trim().equals("") &&
       edt2.getText().toString().trim().equals("")){

       Toast.makeText(this, "Os dois campos estao vazios!",Toast.LENGTH_LONG).show();

   }else if(edt1.getText().toString().trim().equals("")){

       Toast.makeText(this, "O primeiro campo esta vazio",Toast.LENGTH_LONG).show();

   }else if(edt2.getText().toString().trim().equals("")){

       Toast.makeText(this, "O segundo campo esta vazio",Toast.LENGTH_LONG).show();

   }else{

       double valor1 = Double.parseDouble(edt1.getText().toString());
       double valor2 = Double.parseDouble(edt2.getText().toString());

       double resultado1 = valor1 * n1;

       if (resultado1 < valor2) {
           tv3.setText("Vale a pena Abastecer com Gasolina");

       }else {
           tv3.setText("Vale a pena Abastecer com Alcool");
       }
   }
}

I know few changes, but I hope I can help

1

You can do it like this:

if (edt1.getText().toString().isEmpty()) {

   //Informar que o campo 1 está vazio.

}
  • If you have a blank space, it will not validate.

0

You can check this way by creating the following function, which will check if it has white space and if the string size is less than 1.

private boolean isEmpty(EditText etText) {
        String text = etText.getText().toString().trim();
        if (text.length()<1)
            return true;
        return false;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.