Android alert dialog and set positive button - java

This is for a slider puzzle. I want to show a dialog box with OK button when the puzzle is completed. When the OK button is pressed, I use an Intent to load a website via Android browser. Only problem is that with the current code, when the puzzle is complete, it does not load a box (it does when I use null). It doesn't do anything. Any ideas?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(!puzzle.isSolved() ? R.string.title_stats : stats.isNewBest() ? R.string.title_new_record : R.string.title_solved);
builder.setMessage(msg);
builder.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www..com"));
Bundle b = new Bundle();
b.putBoolean("new_window", true); //sets new window
intent.putExtras(b);
startActivity(intent);
}
});

AlertDialog.Builder builder = new AlertDialog.Builder(your_activity.this);
builder.setTitle(!puzzle.isSolved() ? R.string.title_stats : stats.isNewBest() ? R.string.title_new_record : R.string.title_solved);
builder.setMessage(msg);
builder.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www..com"));
Bundle b = new Bundle();
b.putBoolean("new_window", true); //sets new window
intent.putExtras(b);
startActivity(intent);
}
});
builder.show();
try this

Check the below code. It may help you
AlertDialog alertDialog = new AlertDialog.Builder(
GeneralClassPhotoCaptureImageVideo.this).create(); // Read
// Update
alertDialog.setTitle("Title of dialog");
alertDialog
.setMessage("contents");
alertDialog.setButton(Dialog.BUTTON_POSITIVE, "Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www..com"));
Bundle b = new Bundle();
b.putBoolean("new_window", true); //sets new window
intent.putExtras(b);
startActivity(intent);
}
});
alertDialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertDialog.show();

Add following code to show the dialog.
AlertDialog alert = builder.create();
alert.show();

Related

Error in Alert dialog on this

This is my code,
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v == b3) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Delete")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
}
}
});
if (!rs.isClosed()) {
rs.close();
}
name.setText(nam);
email.setText(emai);
When I use this code on a delete button an error appear on first "this". How to solve it? How can I use delete confirmation message?
Please help me. Thanks in advance!
Use context instead of simply "this" reference. Here you can use Activity context, not any other context. For more information visit these links Context , AlertDialog.Builder
For activity:
AlertDialog.Builder builder = new AlertDialog.Builder(ActivityName.this);
For fragment:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Thanks
You need to replace
AlertDialog.Builder builder = new AlertDialog.Builder(this);
with
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);

Dialogue box does not show up

I am creating a dialogue box that should appear to the user in case of the GPS service is disabled. But what is happening is, although I disabled the service manually to force the dialogue appear, the App starts and nothing is happening.
The code below shows how I tried to create the dialogue box and when. Please let me know if there is any mistake.
JavaCode:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpstest00);
locMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
gpsEnable = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnable) {
showGPSDialogueBox();
}
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this.locationListener);
}
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}*/
private void showGPSDialogueBox() {
AlertDialog.Builder alertDialogue = new AlertDialog.Builder(this);
alertDialogue.setTitle("GPS Settings");
alertDialogue.setMessage("GPS is deactivated. Do you want to switch " + to settings menu to activate it?");
alertDialogue.setPositiveButton("Settings",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
alertDialogue.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
}// Enf of showDialogueBox function.
You need to call show function for the dialog box to show
alertDialogue.show();
You need to create AlertDialog from AlertDialog.Builder object then show the AlertDialog using show() method as follows...
AlertDialog dialog = alertDialogue.create();
dialog.show();
Update your showGPSDialogueBox() as below...
private void showGPSDialogueBox() {
AlertDialog.Builder alertDialogue = new AlertDialog.Builder(this);
alertDialogue.setTitle("GPS Settings");
alertDialogue.setMessage("GPS is deactivated. Do you want to switch " +
" to settings menu to activate it?");
alertDialogue.setPositiveButton("Settings",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
alertDialogue.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog dialog = alertDialogue.create();
dialog.show();
}

When user click on alert button open mobile browser and display URL on android apps

When user click on alert massage know more button open mobile browser and display URL of my website on android apps what I have made.I need the code. I have tried many code like web view and others, but not working. Please help me!
Below Code not working.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
AlertDialog.Builder dialog = new AlertDialog.Builder(NoteEdit.this);
dialog.setTitle("About");
dialog.setMessage("Hello!);
dialog.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}});
dialog.setPositiveButton("KNOW MORE", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
Below code perfect working in my case:
Create
Define private static final int DIALOG_EXIT = 1;
now create
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
AlertDialog.Builder builder = new Builder(this);
switch (id) {
case DIALOG_EXIT:
return new AlertDialog.Builder(MainActivity.this)
.setTitle("About")
.setMessage("Hello")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
})
.setNegativeButton("Know More",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
/*Uri uri = Uri
.parse("http://www.google.com");
Intent intent = new Intent(
Intent.ACTION_VIEW, uri);
startActivity(intent);*/
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
}).create();
}
dialog = builder.show();
return dialog;
}
and you just called this from your menu item selected function;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.setting:
showDialog(DIALOG_EXIT);
break;
}
return true;
}
try this this may help you.
this code works for me:
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(i);
You need to show your dialog. At the end, dd the line
dialog.show()
Also, in the line
dialog.setMessage("Hello!);
you are missing an ending quote.

android- AlertDialog showing two times

I have an alertDialog from which I am creating another alertDialog. The problem is that the first alertDialog pops up twice. So, the second alertDialog is also created twice. Here is my code-
public void navigationMenu() {
AlertDialog.Builder builder = new AlertDialog.Builder(
getSherlockActivity());
String[] items = { "Current Location", RajputanaGrnd.NAME,
NCCOffice.NAME, NewMech.NAME, ChemGrounds.NAME, Rampur.NAME,
"Swatantrata Bhavan Ground", ElectDept.NAME, ABLT.NAME };
builder.setTitle("From:");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
AlertDialog.Builder builder1 = new AlertDialog.Builder(
getSherlockActivity());
String[] items = { RajputanaGrnd.NAME, NCCOffice.NAME,
NewMech.NAME, ChemGrounds.NAME, Rampur.NAME,
"Swatantrata Bhavan Ground", ElectDept.NAME, ABLT.NAME };
builder1.setTitle("To:");
builder1.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert1 = builder1.create();
alert1.show();
}
});
AlertDialog alert = builder.create();
if (!alert.isShowing()) {
alert.show();
}
}
The problem is not with AlertDialog but the in the onOptionsItemSelected. Here is the answer.

Showing a dialog box with contents of choosing file and camera

i want to open a dialog box that gives me two option.
1- Choose file from SD Card
2- Take a snapshot from Camera
Right now i am using this code
receipt.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(RECEIPT_DIALOG_ID);
}
});
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
AlertDialog.Builder builder = new Builder(this);
case RECEIPT_DIALOG_ID:
builder.setTitle("Choose your file");
dialog = builder.create();
return dialog;
}
Now, how can i add these two options.
Best Regards
Try this it provides both the options
final CharSequence[] items = {"Camera", "Memory Card"};
builder.setTitle(R.string.pic_option);
builder.setIcon(R.drawable.camera_icon);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
launchCamera(item);
}
});
builder.create();
builder.show();
and the Function launchCamera(item) is here
public void launchCamera(int id){
switch (id) {
case 0:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity)getParent()).startActivityForResult(cameraIntent, 1888);
break;
case 1:
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
((Activity)getParent()).startActivityForResult(intent, 2);
break;
default:
break;
}
}
Use this directly in your method.It will show 2 buttons on dialog.
Dialog dialog;
dialog = new Dialog(getParent());
dialog.setContentView(R.layout.camdialog);
dialog.setTitle(" Select the Image");
dialog.setCancelable(true);
sdCard = (Button) dialog.findViewById(R.id.sdCard);
camDialog = (Button) dialog.findViewById(R.id.camDialog);
dialog.show();
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, 200);
try this...
new AlertDialog.Builder(menu)
.setTitle("Choose your file")
.setMessage("Your msg")
.setPositiveButton("Choose file from SD Card",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// Your code
}
})
.setNegativeButton("Take a snapshot from Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
//Your code
}
}).show();

Categories

Resources