intent in alart dialog [closed] - java

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

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

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

On click in adapter class (ListView)

I need some help with my on click in my adapter class.
This is the code that I want to run when an item is clicked. connectOrDisconnectUser()
private void connectOrDisconnectUser(final int position) {
final AlertDialog.Builder builder = new AlertDialog.Builder(
RelationshipRemoved.this);
builder.setMessage("Do you want to un tag " + usersInfo.get(position).get(
RelationshipRemoved.likedOne))
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
//do something
public void onClick(DialogInterface dialog, int randomvar) {
Log.d("OkHttpRemove", "getting removing!");
String username = (globalInt);
String UT = usersInfo.get(position).get(
RelationshipRemoved.likedOne);
String type = "remove";
BackgroundWorker backgroundWorker = new BackgroundWorker(getApplicationContext());
backgroundWorker.execute(type, username, UT);
startActivity(new Intent(RelationshipRemoved.this, MainActivity.class));
Toast.makeText(context, "UnTagged!",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
The file that holds the adapter is called AdapterClass and the class that uses the adapter class is Called RelationshipRemoved. When I try to write this code. I get an error on RelationshipRemoved.this on this line RelationshipRemoved.this);, startActivity and RelationshipRemoved.this on this line startActivity(new Intent(RelationshipRemoved.this, MainActivity.class));
What's wrong?

Web Link using DialogFragment [duplicate]

This question already has answers here:
How can I open a URL in Android's web browser from my application?
(41 answers)
Closed 8 years ago.
I have this dialog fragment, but i'm trying to get it so that when a user clicks on the "My Other Apps" Button they get redirected to a web address. Is there any way of doing this.
Any help is appreciated
Thanks
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
AlertDialog.Builder theDialog = new AlertDialog.Builder(getActivity());
theDialog.setTitle("About");
String d1 = "Thanks For Downloading! ";
String d2 = "Made by ME";
theDialog.setMessage(d1 +"\n"+ d2 );
theDialog.setPositiveButton("My Other Apps", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked My Other Apps",
Toast.LENGTH_SHORT).show();
}
});
theDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked Cancel",
Toast.LENGTH_SHORT).show();
}
});
return theDialog.create();
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
AlertDialog.Builder theDialog = new AlertDialog.Builder(getActivity());
theDialog.setTitle("About");
String d1 = "Thanks For Downloading! ";
String d2 = "Made by ME";
theDialog.setMessage(d1 +"\n"+ d2 );
theDialog.setPositiveButton("My Other Apps", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
theDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked Cancel",
Toast.LENGTH_SHORT).show();
}
});
return theDialog.create();
}
}
Do this on your button click:
#Override
public void onClick(DialogInterface dialog, int which) {
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}

calling dialog from another dialog repeatedly in amy android app

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

Categories