4

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("");
                                }}
                            });
                }
            });



        }
    }

shakram02
  • 10,812
  • 4
  • 22
  • 21
Georges Badra
  • 65
  • 1
  • 3
  • 10
  • I'd add an invisible (gone) indeterminate (circular) ProgressBar and set it visible when you click the Button. Then the new Activity is started and the login Activity (including the ProgressBar) disappears. – Phantômaxx Dec 18 '14 at 16:31

4 Answers4

8

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("");
                        }}
                    });
        }
    });
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • 1
    please not provide code random : http://stackoverflow.com/questions/21636036/string-cannot-be-resolved-or-is-not-a-field // https://github.com/ejp456/Casino-Royale/blob/master/CardGames/src/com/example/cardgames/MainActivity.java –  May 18 '15 at 07:15
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)

Panther
  • 8,938
  • 3
  • 23
  • 34
0

ProgressDialog is what you want. It's a modal dialog with a spinner that greys the background activity while it's showing. Start it before the network call and dismiss it when the call finishes.

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

// do login call

then dismiss it when the call finished in the onDone method

pd.dismiss();

There are a few other options like ProgressBar which is a view that you will show/hide in your layout when the network call is happening. The ActionBar also has a stock ProgressBar that you can hide and show there if you like that look better. Put a progressBar on ActionBar

Community
  • 1
  • 1
browep
  • 5,057
  • 5
  • 29
  • 37
0
public class LoginActivity extends Activity {
// Declare Variables
Button loginbutton;
String usernametxt;
String passwordtxt;
EditText password;
EditText username;
private ProgressDialog mProgress;

/** 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);


    mProgress =new ProgressDialog(this);
    String titleId="Signing in...";
    mProgress.setTitle(titleId);
    mProgress.setMessage("Please Wait...");



    // 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
            mProgress.show();
            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("");
                        }}
                    });
        }
    });



}

}

Georges Badra
  • 65
  • 1
  • 3
  • 10