1
@Override
    protected Dialog onCreateDialog(int id) {

        LayoutInflater factory = LayoutInflater.from(GmailFetchActivity.this);

        final View textEntryView = factory.inflate(R.layout.alertdialog_gmail, null);

        final EditText username = (EditText) textEntryView.findViewById(R.id.edit_username);
        editxt_pass = (EditText) textEntryView.findViewById(R.id.edit_password);
        final CheckBox remember = (CheckBox) textEntryView.findViewById(R.id.checkBox_remember);

        username.setText(myPrefs.getString("username", ""));
        editxt_pass.setText(myPrefs.getString("password", ""));


        AlertDialog.Builder alert=new AlertDialog.Builder(this);

        alert.setTitle("Gmail login");      
        alert.setView(textEntryView);  

        alert.setPositiveButton("Login", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) {          

                if(username.getText().toString().trim().equalsIgnoreCase(""))
                {

                }
                else{
                if(isNetworkAvailable()){               

                    String user=username.getText().toString();
                    String pass=editxt_pass.getText().toString();

                    if(remember.isChecked()){

                        prefsEditor.putString("username",user);
                        prefsEditor.putString("password",pass);
                        prefsEditor.commit();
                    }
                    else{
                        prefsEditor.putString("username","");
                        prefsEditor.putString("password","");
                        prefsEditor.commit();
                    }

                    progressHrz.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    progressHrz.setMessage("Fetching attachment's list");
                    progressHrz.setCancelable(true);
                    progressHrz.show();

                    new FetchGmail().execute(user+"@gmail.com",pass);               
                }
                else
                    Toast.makeText(getApplicationContext(), "No internet access", Toast.LENGTH_LONG).show();
                }
            } 
        });
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) { 

                finish();
            } 
        }) ;

         return alert.create();


    }

if the username is empty i just want to show a toast,but the dialog gets dismissedafter show the toast y?i want it to stay as it is

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
Harinder
  • 11,776
  • 16
  • 70
  • 126
  • that is the default behavior of alert dialog. If you click on positive button or any then it will close automatically. If you dont want that then do custom dialog. – harish Sep 28 '11 at 12:26

1 Answers1

0

There is a work around you can implement. You just would need to re-show the dialog if whatever checks you're performing don't pass. Take a look at this previous answer: Is there a way prevent AlertDialog from closing with invalid inputs?

Note: You may not even need to implement your own alert dialog to do this. You should be able to do this right in your onClick() listener by calling dialog.show(). Give it a shot and let us know if it works.

Community
  • 1
  • 1
SBerg413
  • 14,515
  • 6
  • 62
  • 88