Ram Koti
Ram Koti

Reputation: 2211

how to validate text boxes in Alert Dialog box in android

i am working on alert dialog with some text boxes where the text boxes needed to be validated and if they are correct only then the pop up message need to be disappeared The problem here was, if user clicks ok button(with empty values) the pop up message was disappearing. Thanks in advance...

final AlertDialog.Builder builder = new AlertDialog.Builder(KmsActivity.this);
builder.setTitle("Enter OrderId,BillNo");
builder.setCancelable(false);
final EditText input1 = new EditText(KmsActivity.this);
final EditText input2 = new EditText(KmsActivity.this);
input1.setHint("Enter OrderId");
input2.setHint("Enter BillNo");
LinearLayout linearLayout = new LinearLayout(KmsActivity.this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(input1);
linearLayout.addView(input2);
builder.setView(linearLayout);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface popupDialog, int which) {
        String orderId = input1.getText().toString();
        String billNo = input2.getText().toString();
        Log.d(TAG_NAME, "order id:" + orderId);
        Log.d(TAG_NAME, "bill no" + billNo);
        if (orderId.length()<=0) {
            Toast.makeText(KmsActivity.this, "Please enter Order ID", Toast.LENGTH_LONG).show();
        } else if (billNo.length()<=0) {
            Toast.makeText(KmsActivity.this, "Please enter Bill No", Toast.LENGTH_LONG).show();
        } else {
            tripObjects.get(0).setOrderId(orderId);
            tripObjects.get(0).setBillNo(billNo);
            tripObjects.get(0).saveInBackground();
            Toast.makeText(KmsActivity.this, "values uploaded", Toast.LENGTH_LONG).show();
            popupDialog.cancel();
        }
    }
});
builder.show();

Upvotes: 1

Views: 1031

Answers (2)

Ankit Gupta
Ankit Gupta

Reputation: 672

You can either call your text validation method on positive click button with a condition with a toast message. or you can add addTextChangeListener to your textview where you are entering text, to call validation method everytime you change text.

Upvotes: 1

Onkar Nene
Onkar Nene

Reputation: 1379

First import class as - import android.support.v7.app.AlertDialog;

Then Try this -

final EditText input1 = new EditText(MainActivity.this);
        final EditText input2 = new EditText(MainActivity.this);
        input1.setHint("Enter name1");
        input2.setHint("Enter Name2");
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.addView(input1);
        linearLayout.addView(input2);

        final AlertDialog builder = new AlertDialog.Builder(MainActivity.this)
                .setTitle("Sign In Failed")
                .setCancelable(false)
                .setMessage("Invalid username or password").setView(linearLayout).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).create();
        builder.show();
        ((AlertDialog)builder).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (input1.length() <= 0) {
                    Toast.makeText(MainActivity.this, "Please Enter Name", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
                    builder.dismiss();
                }
            }
        });

Upvotes: 0

Related Questions