-4
public class DialogAddSubject extends DialogFragment implements View.OnClickListener{


    Button add,cancel;
    EditText professorName;
    EditText subjectName;


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_add_subject, null);
        add = (Button) view.findViewById(R.id.addsub);
        cancel = (Button) view.findViewById(R.id.cancel);
        add.setOnClickListener(this);
        cancel.setOnClickListener(this);


        subjectName = (EditText)view.findViewById(R.id.subjectName);
        professorName = (EditText)view.findViewById(R.id.professorName);


        getDialog().setTitle(R.string.add_sub);
        return view;

    }

    @Override
    public void onClick(View view) {

        if (view.getId()== R.id.addsub)



        {

            String subjName = subjectName.getText().toString();
            String profName = professorName.getText().toString();


            ParseObject userData = new ParseObject("UserData");
            userData.put("createdBy", ParseUser.getCurrentUser());
            userData.put("professor", profName);
            userData.put("subject", subjName);


            userData.saveInBackground();


            dismiss();
            Toast.makeText(getActivity(),"subject and professor successfully added",Toast.LENGTH_SHORT).show();
        }

        else


        {
            dismiss();

        }

    }
}

The code shown its a dialog fragment that has 2 edit texts when the add button is pressed, the text entered on the editTexts fields is saved to the database (parse.com on this case), everything works perfectly.

What I want to do is to not allow the user to add the editTexts to the data base when the fields are empty and I also want to display a message either saying that one of the fields is empty or both of them are empty.

How can I do that?

Alex K
  • 8,269
  • 9
  • 39
  • 57
Sebastian Delgado
  • 706
  • 3
  • 7
  • 26

3 Answers3

0

After these lines:

String subjName = subjectName.getText().toString();
String profName = professorName.getText().toString();

Check if they're empty, and if so, make a toast and return.

if (subjName.isEmpty() || profName.isEmpty()) {
    Toast.makeText(getActivity(), "Please input values", Toast.LENGTH_SHORT).show();
    return;
}
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
0

Just check this:

if (subjName.isEmpty() || profName.isEmpty()) {
    Toast.makeText(getActivity(), "Please provide all the values", Toast.LENGTH_SHORT).show();
}
else {
    ParseObject userData = new ParseObject("UserData");
    userData.put("createdBy", ParseUser.getCurrentUser());
    userData.put("professor", profName);
    userData.put("subject", subjName);
    userData.saveInBackground();
    dismiss();
    Toast.makeText(getActivity(),"subject and professor successfully added",Toast.LENGTH_SHORT).show();
}

Hope this helps.. :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
0

Use this in your onclick's if condition

String subjName = subjectName.getText().toString();
                String profName = professorName.getText().toString();
    if(subjName.equals("")&& profName.equals(""))
    {
     Toast.makeText(getActivity(),"Please Enter empty fields",Toast.LENGTH_SHORT).show();
    }

    else
                ParseObject userData = new ParseObject("UserData");
                userData.put("createdBy", ParseUser.getCurrentUser());
                userData.put("professor", profName);
                userData.put("subject", subjName);
                userData.saveInBackground();
dismiss();
        Toast.makeText(getActivity(),"subject and professor successfully added",Toast.LENGTH_SHORT).show();
Hanish Sharma
  • 869
  • 8
  • 23