calling dialog from another dialog repeatedly in amy android app - java

case 2: new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("Socket9 Registeration")
.setMessage("You have been Registered Successfully.Please Login to continue.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { #Override
public void onClick(DialogInterface dialog, int which) {
etMobileNo.setText("");
etPassword.setText("");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
etMobileNo.setText("");
etPassword.setText("");
}
}).show();
break;
case 3: new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Socket9 Registeration")
.setMessage("Your Code doesn't match.Try Again")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { #Override
public void onClick(DialogInterface dialog, int which) {
dismissDialog(2);
showDialog(4);
}}).show();
break;
case 4: new AlertDialog.Builder(this)
.setTitle("Enter Your Registeration Code")
.setView(input)
.setMessage("Registeration code has been delivered on your registered number via sms")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { #Override
public void onClick(DialogInterface dialog, int which) {
String value = input.getText().toString().trim();
String regsCode2=etFake.getText().toString().trim();
System.out.println("Va "+value+" Reg"+regsCode2);
if(value.compareToIgnoreCase(regsCode2)==0){
validCodeMatch=objCommonServices.sendEvalidCode(etMobileNo.getText().toString().trim(), etPassword.getText().toString().trim(),"OK");
if(validCodeMatch.contains("Code Match")){
showDialog(2);
}
}
else{
dismissDialog(4);
showDialog(3);
}
}}).show();
I have created 3 dialogs tand calling each other on each user input every timeI am also trying to dismissDialog method because earlier i was getting error remove Parent View . How to carry out the process?

case 3: new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Arihant Shopee Registeration")
.setMessage("Your Code doesn't match.Try Again")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { #Override
public void onClick(DialogInterface dialog, int which) {
removeDialog(4);
showDialog(4);
}}).show();
break;
case 4:AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter Your Registeration Code");
alert.setMessage("Registeration code has been delivered on your registered number via sms");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString().trim();
String regsCode2=etFake.getText().toString().trim();
System.out.println("Va "+value+" Reg"+regsCode2);
if(value.compareToIgnoreCase(regsCode2)==0){
validCodeMatch=objCommonServices.sendEvalidCode(etMobileNo.getText().toString().trim(), etPassword.getText().toString().trim(),"OK");
if(validCodeMatch.contains("Code Match")){
showDialog(2);
}
}
else{
removeDialog(3);
showDialog(3);
}
}
});

Related

Need to add a countdown timer on an AlertDialog and execute a method if there is no response after 2 minutes

I'm trying to add a timer on my AlertDialog so that if there is no response after 2 minutes, it will go to a method.
private void AlertMe() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MapsActivity.this);
alertDialogBuilder.setTitle("We detected an unexpected collision");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage("Do you need medical assistance? If you don't respond within 2 minutes, I will notify everyone on your emergency contacts.");
alertDialogBuilder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MapsActivity.this, "Requesting Emergency Services", Toast.LENGTH_SHORT).show();
CallServices();
}
});
alertDialogBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MapsActivity.this, "Request for Emergency Services Cancelled", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialogBuilder.show();
}
I tried adding a countdown timer but I couldn't stop the timer with the alertDialogBuilder.setNegativeButton
How can I implement it? Thanks.
Top of your file
private AlertDialog alertDialog;
then
private void AlertMe() {
CountDownTimer timer= new CountDownTimer(120000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
if(alertDialog != null && alertDialog.isShowing()){
alertDialog.dismiss();
CallServices();
}
}
};
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("We detected an unexpected collision");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage("Do you need medical assistance? If you don't respond within 2 minutes, I will notify everyone on your emergency contacts.");
alertDialogBuilder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(YesProfileUpdate.this, "Requesting Emergency Services", Toast.LENGTH_SHORT).show();
CallServices();
}
});
alertDialogBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
timer.cancel();
Toast.makeText(YesProfileUpdate.this, "Request for Emergency Services Cancelled", Toast.LENGTH_SHORT).show();
}
});
alertDialog = alertDialogBuilder.create();
alertDialogBuilder.show();
timer.start();
}

Set text Length of alert dialog in Android studio

How do I set the max of how many characters can be entered. So now the user can enter a lot in the alertdialog While it should have the max. it's not like XML. That would have been easier, any tips for this alert dialog?
private void RequestNewGroup()
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialog);
builder.setTitle("Enter Group Name ");
final EditText groupNameField = new EditText(MainActivity.this);
groupNameField.setHint("");
builder.setView(groupNameField);
builder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
String groupName = groupNameField.getText().toString();
if (TextUtils.isEmpty(groupName))
{
Toast.makeText(MainActivity.this, "Please write Group Name...", Toast.LENGTH_SHORT).show();
}
else
{
CreateNewGroup(groupName);
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
dialogInterface.cancel();
}
});
builder.show();
}
You need to add a InputFilter to your EditText
final EditText groupNameField = new EditText(MainActivity.this);
groupNameField.setHint("");
// ### -> max length
groupNameField.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(###) });

Issue with AlertDialog

I've just implemented an AlertDialog into a fragment within my Android app and it is causing my application to crash when it is shown.
Any ideas on why this might be?
Dialog
void addSiteOption() {
String[] options = {"Auto", "Manual"};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext());
builder.setTitle("Add");
builder.setMessage("Auto add - download. \n Manually add - no internet connection.");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int selectionIndex) {
switch (selectionIndex)
{
case 0:
break;
case 1:
break;
}
}
});
builder.show();
}
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x0
You are getting Application context here but you need to get the calling activity's context.So change your code
From this:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext());
To this:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Context=container.getContext();
private void showAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure to clear history?");
builder.setPositiveButton("sure", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}

intent in alart dialog [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
intent in alart dialog how pass in next intent
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String title = editTextTitle.getText().toString().trim();
String r=android.get(i).getPassword();
if(title.equals(r)){
view.getContext().startActivity(new Intent(activity,DetailView.class));
Toast.makeText(view.getContext()," user name " , Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(view.getContext()," user name not valid " , Toast.LENGTH_LONG).show();
}
// sendMessage(user.getId(), title, message);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
you need to use activity or context, for example, please refer below code
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity)
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
activity.startActivity(new Intent(activity, DetailView.class));
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
you can get activity or context from the contractor.
Change this part of the code:
view.getContext().startActivity(new Intent(activity,DetailView.class));
To this:
Intent intent = new Intent(FirstActivity.this,Screen2Activity.class);
startActivity(intent);
Full code:
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String title = editTextTitle.getText().toString().trim();
String r=android.get(i).getPassword();
if(title.equals(r)){
Intent intent = new Intent(FirstActivityName.this,Screen2ActivityName.class);
startActivity(intent);
Toast.makeText(view.getContext()," user name " , Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(view.getContext()," user name not valid " , Toast.LENGTH_LONG).show();
}
// sendMessage(user.getId(), title, message);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}

android alert box with multiple options, how to use those options?

I am a beginner, so if anyone would help me out. I created a list in the dialogue box , now how do i use those options? Like click one and it does something , click another and it does something else.
CharSequence features[] = new CharSequence[] {"Save", "Send", "Something", "Something"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Options");
alertDialog.setItems(features, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Eta chu ma aile",
Toast.LENGTH_LONG).show();
}
});
alertDialog.show();
return true;
}
If you know exact position of every item, just compare it with which param.
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
// handle "Save" option
} else if (which == 1) {
// handle "Send" option
} ...
}
You can use following code:
Somewhere in another function:
String title = "My Alert Box";
String msg = "Choose Option";
alertfunc(title,msg);
The main alert function:
private void alertfunc(String title, String msg) {
if (title.equals(TASK_VIEW_PROFILE)) {
new AlertDialog.Builder(MainActivity.this)
.setTitle(title)
.setMessage(msg)
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
//Do something
}
})
.setNegativeButton("Send",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
.setNegativeButton("Something",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
//...and so on
}
}

Categories