user2379095
user2379095

Reputation: 1

check empty edit text in an alert dialog android

I have the following code for checking empty edit text in an alert dialog, but it is not working

    if (mPhoneNumber == null) {
        mPhoneNumber = GetNumber();
        if (mPhoneNumber == "Error") {

            final AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Warrning");
            alert.setMessage("Please Set Your Phone number");
            final EditText input = new EditText(this);
            input.setInputType(InputType.TYPE_CLASS_PHONE);
            alert.setView(input);
            alert.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int whichButton) {
                            String value = input.getText().toString();
                            while (value.isEmpty())
                            {
                                alert.setTitle("Warrning");
                                alert.setMessage("Please Set Your Phone number");
                                alert.setView(input);
                                alert.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,int whichButton) {
                                                String value = input.getText().toString();}});
                            }
                            String Result = SetNumber(value);
                            mPhoneNumber = value;
                            int UserServiceId = CallLogin(mPhoneNumber);
                            if (UserServiceId > 0) {
                                Intent Service = new Intent(MainScreeen.this,
                                        RecipeService.class);
                                Service.putExtra("UserId", UserServiceId);
                                startService(Service);
                            } else {
                                Intent Reg = new Intent(MainScreeen.this,Regsteration.class);
                                Reg.putExtra("PhoneNumber", mPhoneNumber);
                                startActivity(Reg);
                            }
                        }
                    });
            alert.show();

I need to enforce the user to inter his/her phone number and not leaving the edit text being empty, I used a while loop but it is not working

Upvotes: 0

Views: 3107

Answers (2)

TronicZomB
TronicZomB

Reputation: 8747

Instead of using a while loop, why don't you make your AlertDialog building a separate method and call that method, then in the onClick of your AlertDialog button use an if else to check if that value is empty and if it is make a recursive call on your AlertDialog method.

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

It looks like you are trying to compare String values. You can't do it like this

if (mPhoneNumber == "Error")

change that to

if("Error".equals(mPhoneNumber))

== compares if they are the same object for Strings but not if they have the same value. Doing it this way you shouldn't need the null check because "Error" won't equal mPhoneNumber if mPhoneNumber is null

Upvotes: 2

Related Questions