how to add a progress bar to the login activity while logging in?

13,389

Solution 1

Define a progress bar as private ProgressDialog mProgress;

in oncreate use this

mProgress = new ProgressDialog(context);
mProgress.setTitle("Processing...");
mProgress.setMessage("Please wait...");
mProgress.setCancelable(false);
mProgress.setIndeterminate(true);

Now this

// Login Button Click Listener
    loginbutton.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            mProgress.show();
            // Retrieve the text entered from the EditText
            usernametxt = username.getText().toString();
            passwordtxt = password.getText().toString();

            // Send data to Parse.com for verification
            ParseUser.logInInBackground(usernametxt, passwordtxt,
                    new LogInCallback() {
                        public void done(ParseUser user, ParseException e) {
                                // If user exist and authenticated, send user to Welcome.class
                            if(user !=null){   
                            mProgress.dismiss(); 
                            Intent intent = new Intent(
                                        LoginActivity.this,
                                        AddUserPage.class);
                                startActivity(intent);
                                Toast.makeText(getApplicationContext(),
                                        "Successfully Logged in",
                                        Toast.LENGTH_LONG).show();
                                finish();
                        }else{
                            mProgress.dismiss();
                            Toast.makeText(getApplicationContext(), "No such user", Toast.LENGTH_LONG).show();
                            username.setText("");
                            password.setText("");
                        }}
                    });
        }
    });

Solution 2

Add a ProgressBar to your xml layout like this(make sure this is on the top of all other views so that it occupies all other views when its visible.

<ProgressBar
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/loadingProgress"
  android:indeterminate="true"
  android:visibility="false"/>

Then get the view reference inside the onCreate

ProgressBar pb =  findViewById(R.id.loadingProgress);

On public void onClick(View arg0) for loginbutton set the visibility to true for pb like pb.setVisibility(View.Visible)

In LogInCallback() in else part add pb.setVisiblity(View.Gone)

Share:
13,389
Georges Badra
Author by

Georges Badra

Im a Junior Software developer i loved programming since i was a teen

Updated on June 09, 2022

Comments

  • Georges Badra
    Georges Badra almost 2 years

    im creating an app to log in to parse.com and then browse thru projects and other functions but im not able to add a progress bar or anything similar so while the app is loging in nothing is happening im just waiting it to log in and move to the other activity

    this is my code for the logging in any help please

        import android.app.Activity;
        import android.content.Intent;
        import android.os.Bundle;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.Toast;
    
        import com.androidbegin.parselogintutorial.R;
        import com.parse.LogInCallback;
        import com.parse.ParseException;
        import com.parse.ParseUser;
    
    
        public class LoginActivity extends Activity {
            // Declare Variables
            Button loginbutton;
            String usernametxt;
            String passwordtxt;
            EditText password;
            EditText username;
    
            /** Called when the activity is first created. */
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                // Get the view from login.xml
                setContentView(R.layout.login);
                // Locate EditTexts in login.xml
                username = (EditText) findViewById(R.id.username);
                password = (EditText) findViewById(R.id.password);
    
                // Locate Buttons in main.xml
                loginbutton = (Button) findViewById(R.id.login);
    
    
                // Login Button Click Listener
                loginbutton.setOnClickListener(new OnClickListener() {
    
                    public void onClick(View arg0) {
                        // Retrieve the text entered from the EditText
                        usernametxt = username.getText().toString();
                        passwordtxt = password.getText().toString();
    
                        // Send data to Parse.com for verification
                        ParseUser.logInInBackground(usernametxt, passwordtxt,
                                new LogInCallback() {
                                    public void done(ParseUser user, ParseException e) {
                                            // If user exist and authenticated, send user to Welcome.class
                                        if(user !=null){    
                                        Intent intent = new Intent(
                                                    LoginActivity.this,
                                                    AddUserPage.class);
                                            startActivity(intent);
                                            Toast.makeText(getApplicationContext(),
                                                    "Successfully Logged in",
                                                    Toast.LENGTH_LONG).show();
                                            finish();
                                    }else{
                                        Toast.makeText(getApplicationContext(), "No such user", Toast.LENGTH_LONG).show();
                                        username.setText("");
                                        password.setText("");
                                    }}
                                });
                    }
                });
    
    
    
            }
        }