I am making a sign in activity and i want it to show the progress dialog when the sign in button is pressed I declared, initialized, and called it but it's not showing. But when I called the progress dialog within the on create it showed up
This is my code below:
public class Login extends Activity {
private EditText username, password;
private Button login;
private Button signup;
String txtUsername, txtPassword, loggedin;
private ProgressDialog mProgress;
String titleId = "Logging in";
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final DatabaseHelper db = new DatabaseHelper(this);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
signup = (Button) findViewById(R.id.signup);
// Login button listener
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
showProgress();
// Retrieve text from the EditText
txtUsername = username.getText().toString();
txtPassword = password.getText().toString();
if (txtUsername.isEmpty() && txtPassword.isEmpty()) {
mProgress.dismiss();
Toast.makeText(getApplicationContext(),"Enter Username And Password", Toast.LENGTH_LONG);
} else {
User user = db.getUser(txtUsername, txtPassword);
id = user.getId();
if (txtUsername.equalsIgnoreCase(user.getUsername())
&& txtPassword.equals(user.getPassword())) {
mProgress.dismiss();
Intent intent = new Intent(Login.this,
AdministratorPage.class);
intent.putExtra("username", "username");
intent.putExtra("id", "id");
startActivity(intent);
Toast.makeText(
getApplicationContext(),
"Successfully Logged In as "
+ user.getUsername(), Toast.LENGTH_LONG)
.show();
finish();
} else {
mProgress.dismiss();
Toast.makeText(getApplicationContext(),
"Usename Or Password Incorrect",
Toast.LENGTH_LONG).show();
username.setText("");
password.setText("");
}
}
}
});
// Sign up button listener
signup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
showProgress();
// Retrieve text from the EditText
txtUsername = username.getText().toString();
txtPassword = password.getText().toString();
if (txtUsername.isEmpty() && txtPassword.isEmpty()) {
mProgress.dismiss();
Toast.makeText(getApplicationContext(),
"Enter Username And Password", Toast.LENGTH_LONG);
} else {
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addUser(new User(txtUsername, txtPassword, "Y"));
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<User> users = db.getAllUsers();
for (User cn : users) {
String log = "Id: " + cn.getId() + " ,Name: "
+ cn.getUsername() + " ,Password: "
+ cn.getPassword();
// Writing Contacts to log
Log.d("Name: ", log);
}
mProgress.dismiss();
Intent intent = new Intent(Login.this,
AdministratorPage.class);
startActivity(intent);
Toast.makeText(getApplicationContext(),
"Successfully Logged In", Toast.LENGTH_LONG).show();
finish();
}
}
});
}
public void showProgress() {
// Initialize the Progress Dialog
mProgress = new ProgressDialog(Login.this);
mProgress.setTitle(titleId);
mProgress.setMessage("Logging In Please Wait...");
mProgress.show();
}
All your code is running synchronously and you are dismissing the progress dialog before the system had a chance to actually show it.
You would have to put all your login stuff into an AsyncTask or similar, then the progress bar would be shown. Although in your case the AsyncTask would finish so quickly that you'd hardly notice the progress dialog. But if you'd check the username/password against a backend server, it would work with an AsyncTask.
If you simple want to use Progress Dialog you should write this code in a Veriable.
ProgressDialog dialog;
and in Singin ClickListener Write this code.
dialog = ProgressDialog.show(Login.this, "Logging In", "Please wait...", true);
and Write this when you want to dismiss
dialog.dismiss();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.