I've this code in Java Android APP that is launched after a camera scan. How Can i go to new activity after a scan?
ActivityResultLauncher<ScanOptions> barLauncher = registerForActivityResult(new ScanContract(), result ->{
if (result.getContents()!=null){
AlertDialog.Builder builder = new AlertDialog.Builder(QRScanActivity.this);
builder.setTitle("Result");
builder.setMessage(result.getContents());
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
}
});
Thanks
So you are observing a dialog after scanned something, so the following part is the callback of your scanning event:
if (result.getContents()!=null){
AlertDialog.Builder builder = new AlertDialog.Builder(QRScanActivity.this);
builder.setTitle("Result");
builder.setMessage(result.getContents());
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
}
So all you need to do, is to replace the above code into code that triggers a new Activity:
if (result.getContents() != null) {
Intent intent = new Intent(QRScanActivity.this, NextTargetActivity.class);
// You can also put the scanned result contents using intent.putExtra()
startActivity(intent);
}
Related
I'm struggling to get my android app to exit via the alertdialog.
Clicking 'yes' to play again works fine as in it restarts the app and you get to go again.
Unfortunately, when I click 'no' it just removes the dialog alert box and I can't figure out why.
Any help greatly appreciated.
Thanks
#Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(Task1Activity.this);
alert.setMessage("You have guessed incorrectly three times. " +
"The answer was " + ranNum + ". " + "Would you like to play again?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Task1Activity.this, Task1Activity.class);
startActivity(i);
}
});
alert
.setCancelable(false)
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Task1Activity.this.finish();
};
)
.setTitle("Unlucky!")
.create();
you are creating the Activity Intent i = new Intent(Task1Activity.this, Task1Activity.class); which added the activity on stack.
try this code:
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
Task1Activity.this);
alertDialog.setTitle("Unlucky!");
alertDialog.setCancelable(false)
alertDialog.setMessage("You have guessed incorrectly three times. " +
"The answer was " + ranNum + ". " + "Would you like to play again?");
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent i = new Intent(Task1Activity.this, Task1Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(i);
}
});
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
alertDialog.dismiss();
//getActivity().finish();
finishAffinity(); //finish all previous activity on stack
}
});
alertDialog.show();
You keep adding activities on top of each other every time you do:
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Task1Activity.this, Task1Activity.class);
startActivity(i);
}
});
In order to keep only 1 activity alive you should finish() the old one when you start a new activity:
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Task1Activity.this, Task1Activity.class);
startActivity(i);
finish();
}
});
I think problem is startActivity(i);
When you click on the ok, startActivity(i) starts another Task1Activity on current Task1Activity and when you press the cancel, the top Task1Activity is finishing and you go back the previous Task1Activity.
Try startActivity(i) with flag.
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
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);
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.
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();
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();