0

I am new to android. I am making a log in mechanism that when you click on Log In button then it call a web service, which check you are authenticated or not. Meanwhile i want that as long as result is not return from the service, then the progress bar should be shown, and when result come then progress dialog should be disappear and i navigate to another activity that you successfully login else show a message that sorry wrong username and password. I also want to show a cancel button so if authentication takes long then user has the option to dismiss the dialog. I tried this but i am getting error.

 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    Button btn_logIn = (Button)findViewById(R.id.btn_signIn );
    btn_logIn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {

            ProgressDialog dialog = new ProgressDialog(getBaseContext());
            dialog.setMessage("Please Wait. Your authentication is in progress");
            dialog.setButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {                        
                    dialog.dismiss();                       
                }
            }); //end of anonymous class

            dialog.show();              
        }           
    }); //end of anonymous class    
} //end of onCreate()

When i click on login button then i get message that force close application. I didn't use the onCreateDialog(int id) method because i read that on developer.android.com that

Opening a progress dialog can be as simple as calling ProgressDialog.show(). For
example, the progress dialog shown to the right can be easily achieved without
managing the dialog through the onCreateDialog(int) callback, as shown here:

ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 
                    "Loading. Please wait...", true);

See it is saying no need to use onCreateDialog() for Progress Dialog. without managing the dialog through the onCreateDialog(int) callback . How can i show the dialog in my case. Thanks

Basit
  • 8,426
  • 46
  • 116
  • 196
  • try by using `new ProgressDialog(YOUR_CURRENT_ACTIVITY.this);` instead of `new ProgressDialog(getBaseContext());` – ρяσѕρєя K Jun 28 '12 at 12:03
  • Thanks :) It worked.Why it is now working with getBaseContext()? Also i want to ask on Cancel button click is it ok to call dismiss() or should i call cancel()? Thanks – Basit Jun 28 '12 at 12:08

1 Answers1

0

if u want ur dialog with getBaseContext() or related to this than use either

 ProgressDialog dialog = new ProgressDialog(view.getBaseContext());

or

 ProgressDialog dialog = new ProgressDialog(view.getContext());

or

 ProgressDialog dialog = new ProgressDialog(view.getApplicationContext());

and the other way is

as suggested above

  ProgressDialog dialog = new ProgressDialog(YourRecentActivity.this);
Khan
  • 7,585
  • 3
  • 27
  • 44