How do I add a progress bar for login activity during login?
im creating an app to log into parse.com and then look at projects and other functions but they fail to add a progress bar or something like that so nothing happens when logging into the app just waiting for it to register and move on to another activity ...
this is my registration code for any help.
`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("");
}}
});
}
});
}
}
`
source to share
Define the 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("");
}}
});
}
});
source to share
Add a ProgressBar to your XML layout like this (make sure it sits on top of all other views so that it occupies all other views when 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 link inside onCreate
ProgressBar pb = findViewById(R.id.loadingProgress);
In public void onClick(View arg0)
for loginbutton
fit visibility to true
for pb
likepb.setVisibility(View.Visible)
In LogInCallback()
otherwise addpb.setVisiblity(View.Gone)
source to share
ProgressDialog is what you want. This is a modal dialogue with the spinner that removes background actions during the show. Launch it before the net call and release it when the call ends.
ProgressDialog pd = new ProgressDialog(context);
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
// do login call
then release it when the call is over in the onDone method
pd.dismiss();
There are a few more options like the ProgressBar , which is the view that you will show / hide in your layout when a network call occurs. The ActionBar also has a ProgressBar stock that you can hide and show there if you like what looks better. Place progressBar on ActionBar
source to share
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("");
}}
});
}
});
}
}
source to share