-1

Hello I am making an app where user can order some books. When he, or she, fill out the order with his firs second name etc and hits order button applications,using Intent, will switch to SMS and thus will purchase books via text message.But i wish to be able, if user accidently forget to fill up all fields, toast pop up with message "Please fill up XYZ field". I used if else, but when some field remain empty something else happened.I got android message that my app needs to be closed and and return me to the previous activity.In my LogCat nothing happened . No error message.

this is my code :

public void kreiranjeNarudzbine(View v) {
    EditText editTextIme = (EditText) findViewById(R.id.ime);
    String imeNarucioca = editTextIme.getText().toString();
    EditText editTextPrezime = (EditText)findViewById(R.id.prezime);
    String prezimeNarucioca = editTextPrezime.getText().toString();
    EditText editTelefonNarucioca = (EditText)findViewById(R.id.telefon);
    String telefonNarucioca = editTelefonNarucioca.getText().toString();
    EditText editAdresaNarucioca = (EditText)findViewById(R.id.adresa);
    String adresaNarucioca = editAdresaNarucioca.getText().toString();
    EditText editGradNarucioca = (EditText)findViewById(R.id.grad);
    String gradNarucioca = editGradNarucioca.getText().toString();
    EditText editKolicina = (EditText)findViewById(R.id.kolicina);
    String narucenaKolicina = editKolicina.getText().toString();
    int kolicina = Integer.parseInt(narucenaKolicina);
    int cenaNarudzbine = cena(kolicina);
    String poruka = sumiranjeNarudzbine(imeNarucioca, prezimeNarucioca,telefonNarucioca,adresaNarucioca,gradNarucioca,cenaNarudzbine);
    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.setType("vnd.android-dir/mms-sms");
    smsIntent.putExtra("address", "+381629647169");
    smsIntent.putExtra("sms_body",poruka);
    if(imeNarucioca.equals("")){
        Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show();
    }
    else if(prezimeNarucioca.equals("")){
        Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show();

    }
    else if(telefonNarucioca.equals("")){
        Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show();
    }
    else if(adresaNarucioca.equals("")){
        Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show();
    }
    else if(gradNarucioca.equals("")){
        Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show();
    }
    else if(narucenaKolicina.equals("")){
        Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show();
    }
    else{

        startActivity(smsIntent);}

}
gojic
  • 363
  • 7
  • 20

5 Answers5

2

As already mentioned, you check to make sure it isn't null or empty.

if(imeNarucioca!=null && imeNarucioca.equals("")){
    Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show();
}
else if(prezimeNarucioca!=null && prezimeNarucioca.equals("")){
    Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show();

}
else if(telefonNarucioca!=null && telefonNarucioca.equals("")){
    Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show();
}
else if(adresaNarucioca!=null && adresaNarucioca.equals("")){
    Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show();
}
else if(gradNarucioca!=null && gradNarucioca.equals("")){
    Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show();
}
else if(narucenaKolicina!=null && narucenaKolicina.equals("")){
    Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show();
}
else{
    startActivity(smsIntent);
}

This can be converted into a method to prevent as much text:

public boolean isETEmpty(EditText et){
    return (et != null && (et.equals("") || et.equals(" ")));//the final piece checks if the edittext is empty or just contains a space. It is or between
    //in order to ensure that one of those are true. Thus the parenthesis 
}

(how the above works)

Now, for the required fields you can use HTML to format the text:

/*your view*/.setText(Html.fromHtml("Your title. <font color='red'>*</font>"));

The above code formats the code by adding a red star after the required fields. Later down you can do this:

/*your view*/.setText(Html.fromHtml("<font color='red'>*</font> Required field"));

The comment /*your view*/ you replace with a textview reference, edittext or whatever you use to set text


Further reading:

https://stackoverflow.com/a/9161958/6296561

Zoe
  • 27,060
  • 21
  • 118
  • 148
0

The string of empty edittext might be null, which causes the issue. Try the following code.

if(imeNarucioca==null || imeNarucioca.equals("")){
        Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show();
    }
    else if(prezimeNarucioca==null || prezimeNarucioca.equals("")){
        Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show();

    }
    else if(telefonNarucioca==null || telefonNarucioca.equals("")){
        Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show();
    }
    else if(adresaNarucioca==null || adresaNarucioca.equals("")){
        Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show();
    }
    else if(gradNarucioca==null || gradNarucioca.equals("")){
        Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show();
    }
    else if(narucenaKolicina==null || narucenaKolicina.equals("")){
        Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show();
    }
    else{
        startActivity(smsIntent);}
   }
Chowdary102
  • 106
  • 2
  • 12
  • If the content is null, you should check `et.equals(null)`. You are checking if the string in the edittext is empty, not null. – Zoe May 15 '17 at 14:01
0

You should use isEmpty method provided in android.text.TextUtils. The whole description and implement of this method:

/**
 * Returns true if the string is null or 0-length.
 * @param str the string to be examined
 * @return true if str is null or zero length
 */
public static boolean isEmpty(@Nullable CharSequence str) {
    if (str == null || str.length() == 0)
        return true;
    else
        return false;
}

So, to validate null or empty user input, you can do something like this:

if(android.text.TextUtils.isEmpty(imeNarucioca)){
    // ...
}
Robust
  • 2,415
  • 2
  • 22
  • 37
0

So i'm using this, you can do with it what you like:

This is the method:

public static boolean checkEditTextIsEmpty(EditText... editTexts)
{
    try
    {
        for (EditText editText : editTexts)
        {
            if (editText.getText().toString().trim().length() == 0)
            {
                Drawable d = _application.currentActivity.getResources().getDrawable(android.R.drawable.ic_dialog_alert);
                d.setBounds(0, 0, d.getIntrinsicWidth()/2, d.getIntrinsicHeight()/2);
                editText.requestFocus();
                editText.setError(editText.getHint() + " is empty", d);
                return false;
            }
        }
    }
    catch (Exception ignored)
    {
        return false;
    }
    return true;
}

This is a preview: (ignore my style and background) enter image description here

This is how i implemented it:

if(checkEditTextIsEmpty(txtEmail, txtPassword))
{
    //Do whatever if EditTexts is not empty
}
string.Empty
  • 10,393
  • 4
  • 39
  • 67
0

Just add this line in your Java file pro-grammatically, adding mandatory field near Your Text view.

tv.setText (Html.fromHtml(YourText
                + " <font color='"
                + getResources().getColor(R.color.colorAccent) + "'>" + " * "
                + "</font>"));

add Required field instead of yourText field, then use the required color from colors.xml

Kumar
  • 969
  • 2
  • 22
  • 56