Skip to content Skip to sidebar Skip to footer

How Dialog Keep Stay Or Disable Dismiss When Data Is Invalid

i create a form in a dialog . i use validate form to prevent when the data is empty here is my dialog new AlertDialog.Builder(this) .setTitle(R.string.add_title)

Solution 1:

Here is what I have done to retain dialog on validation

table_array=new String[numOfTables+1];
    for(int i=1; i < numOfTables+1; i++)
    {
        table_array[0]="Select";
        table_array[i]=String.valueOf(i);
    }

    // populate spinner
    spinner=new Spinner(context);
    spinner.setAdapter( new   ArrayAdapter(context,android.R.layout.simple_dropdown_item_1line, table_array));

    final AlertDialog d = new AlertDialog.Builder(context)
    .setView(spinner)
    .setTitle("Select table number")
    .setPositiveButton(android.R.string.ok,
            new Dialog.OnClickListener() {
                @Override
                public void onClick(DialogInterface d, int which) {

                }
            })
    .setNegativeButton(android.R.string.cancel, null)
    .create();

    d.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {

            Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                    if(!spinner.getSelectedItem().toString().equals("Select"))
                        {
                               d.dismiss();


                        }
                        else
                            {
                                Toast.makeText(context, "Please select the table number", 0).show();
                            }
                }
            });
        }
    });


    d.show();

Post a Comment for "How Dialog Keep Stay Or Disable Dismiss When Data Is Invalid"