Check if edittext is empty or not on custom dialog

1.1k views Asked by At

I have taken a custom dialog with edittext on it to enter OTP. I have check whether OTP entered is correct or not. I also want to show toast message if no OTP is entered in edittext but the dialog opened should remain opened. Below is my code

alertDialogBuilder
    .setCancelable(false)
    .setPositiveButton("Ok",
    new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
    enterOtp=edtEnterOtp.getText().toString().trim();

    if(enterOtp.equals(randomNumber+"")){
    sendWalletBallance();
    }else{
    Toast.makeText(MerchantPayment.this,"OTP Mismatch",Toast.LENGTH_LONG).show();
    }
    }
    })
    .setNegativeButton("Cancel",
    new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
    dialog.cancel();
    }
    });

    AlertDialog alertDialog=alertDialogBuilder.create();

    alertDialog.show();
3

There are 3 answers

2
Mr.Popular On BEST ANSWER
if(enterOtp.equals(randomNumber+"")){
    sendWalletBallance();
    }else if(enterOtp.equals("")){
    Toast.makeText(MerchantPayment.this,"no otp is entered ",Toast.LENGTH_LONG).show();
    }else{
    Toast.makeText(MerchantPayment.this,"OTP Mismatch",Toast.LENGTH_LONG).show();
    }

You wanted to show A Toast then it ll help!!!

0
Rajakumar On
if(etmobile.getText().toString().length()>0){
    Toast.makeText(getApplicationContext(),"Its not empty",Toast.LENGTH_SHORT).show();
    }else{
    Toast.makeText(getApplicationContext(),"Its empty",Toast.LENGTH_SHORT).show();
    }
3
Kush Patel On
alertDialogBuilder
    .setCancelable(false)
    .setPositiveButton("Ok",
    new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
    enterOtp.setError(enterOtp.getText().toString().isEmpty()?mActivity.getString(R.string.title_error_message):null);
    enterOtp.addTextChangedListener(new TextWatcher(){

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

    }

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

    }

@Override
public void afterTextChanged(Editable s){

    enterOtp.setError(null);
    }
    });

    if(enterOtp.getError()==null&&enterOtp.equals(randomNumber+"")){
    sendWalletBallance();
    }else{
    Toast.makeText(MerchantPayment.this,"OTP Mismatch",Toast.LENGTH_LONG).show();
    }
    }
    })
    .setNegativeButton("Cancel",
    new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
    dialog.cancel();
    }
    });

    AlertDialog alertDialog=alertDialogBuilder.create();

    alertDialog.show();