Reputation: 8499
How can I verify the password in alertdialog box before doing the next tasks?
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.insert_btn:
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
makeHistory();
Looper.loop();
}
}).start();
break;
}
}
private void makeHistory(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("PASSWORD");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
mPassword = input.getText().toString();
// ****** stop proceeding to next task if password is incorrect *******
if(mPassword == true) .....
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
return;
}
});
builder.show();
// ***** next task here ******
Log.d("Stop", "I dont want this to be print if password is incorrect");
}
Upvotes: 1
Views: 292
Reputation: 5017
Depending on the response returned from your api call, check if the password is correct or not and then use a if else
check to decide whether to execute next tasks . You have probably misplaced your next tasks contents after the alert dialog. See following example
if(mPassword.equals("password returned from server")){
executeNextTasks();
} else {
// when wrong password execute something
}
inside executeNextTasks
add whatever code you want to execute when the password is correct
Upvotes: 1
Reputation: 21053
I think the problem is default AlertDialog
behavior. DialogInterface.OnClickListener
will close the dialog anyway regardless of your code . So you need to prevent it . below is an example .
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Some random message");
builder.setPositiveButton("Check",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Useless
}
});
final AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do your stuff here
}
});
Upvotes: 0
Reputation: 753
I think you mean to say dont close dialog unless the password is correct. If so try this code
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("PASSWORD");
builder.setView(input);
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!input.equals(password)){
input.requestFocus();
input.setError("Incorrect Password");
}else{
dialog.dismiss();
doNext();
}
}
});
then on Cancel button just call
dialog.dismiss
Upvotes: 0
Reputation: 4445
Try with below code :
REFERENCE CODE HERE :
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
mPassword = input.getText().toString();
//API call from Here
if (utils.haveNetworkConnection()) {
if (!input.getText().toString().isEmpty()) {
jsonRequestLogin(mPassword);
}else{
input.setError("Enter Password");
}
} else {
utils.showtoast("Internet Connection Not Available");
}
}
});
//API call here
private void jsonRequestLogin(String mPassword) {
if (code.equals("1")) { //Correct Password
//Go to next Activity
}else{
//In correct Password
}
}
Upvotes: 0