I'm building a small app that scans barcodes and put them in a database (using ZXing library). After the scan is taken, and the barcode doesn't exist in the database I want to show a popup dialog that allows an user to enter the name manually.
The dialog works perfectly if it's invoked in an onClickListener, however when I try to put it in an 'if' statement within another method (which checks if the scan result is in the database) the dialog doesn't show.
What's strange, I put tags in the beginning of the method and at the end, both of them are shown in the log, but the alertdialog doesn't appear and it gets me back to the main activity without any error...
public void popUP (String barcode) {
Log.d(TAG, "method started");
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_input, null);
TextView tv = (TextView) mView.findViewById(R.id.textView2);
final EditText et = (EditText) mView.findViewById(R.id.item1);
Button bt1 = (Button) mView.findViewById(R.id.button2);
Button bt2 = (Button) mView.findViewById(R.id.button3);
final String code = barcode1;
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mydb.addNewItem(et.getText().toString(), code);
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
Log.d(TAG,"dialog shown");
}
And here is the method from which I try to inflate the popUP
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
final String barcode = scanResult.getContents();
if (scanResult != null) {
if(scanResult.getContents() == null) {
Log.d(TAG, "Cancelled Scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d(TAG, "SCANNED");
int rows = mydb.checkRows(barcode);
if (rows == -1){
Log.d(TAG, "popUP should appear");
popUP(barcode); //INVOKING THE ALERT DIALOG
} else {
Log.d(TAG, "found and attempting adding");
mydb.addItem(barcode, rows);
}
}
} else {
Toast.makeText(this, "oddal zero", Toast.LENGTH_LONG);
}
Intent intynt = new Intent(this, MainActivity.class);
startActivity(intynt);
}
Related
I have app, which turn on camera QR reader onClick in Fragment,
after that it turns on ServiceActivity
and when you click FAB it turns on TeaDialogFragment, where you click button scan QR it turns on camera QR reader
and it should give result of QR to new Activity (TeaFromQrActivity). But here is a problem - method onActivityResult doesn't called anyway.
TeaDialogFragment.java
public class TeaDialogFragment extends DialogFragment {
public static int BARCODE_READER_REQUEST_CODE = 2;
String qrTeaCode;
private String TAG = TeaDialogFragment.class.getSimpleName();
public TeaDialogFragment() {
// Empty constructor required for DialogFragment
}
public static TeaDialogFragment newInstance(String title) {
TeaDialogFragment frag = new TeaDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle(title);
alertDialogBuilder.setMessage("Message");
alertDialogBuilder.setPositiveButton("scan QR", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// This code works - it turns on new Activity
// Intent i = new Intent(getActivity().getApplicationContext(), TeaFromQrActivity.class);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// getActivity().getApplicationContext().startActivity(i);
// This code doesn't works - it doesn't turn on newActivity, when QR Reader have result, and it turn on current Activity
Intent teaDialogIntent = new Intent(getActivity().getApplicationContext(), BarcodeCaptureActivity.class);
teaDialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivityForResult(teaDialogIntent, BARCODE_READER_REQUEST_CODE);
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null) {
dialog.dismiss();
}
}
});
return alertDialogBuilder.create();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BARCODE_READER_REQUEST_CODE) {
if (resultCode == CommonStatusCodes.SUCCESS) {
if (data != null) {
Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
Intent testIntent = new Intent(getActivity().getApplicationContext(), TeaFromQrActivity.class);
qrTeaCode = "teaQrCode";
testIntent.putExtra(qrTeaCode, barcode.displayValue);
// Start the new activity
startActivity(testIntent);
Log.d(TAG, "Barcode read: " + barcode.displayValue);
} else {
Log.d(TAG, "No barcode captured, intent data is null");
}
} else {
Log.e(TAG, String.format(getString(R.string.barcode_error_format),
CommonStatusCodes.getStatusCodeString(resultCode)));
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
TeaFromQrActivity.java
public class TeaFromQrActivity extends AppCompatActivity {
private String teaQR;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tea_from_qr);
teaQR = getIntent().getExtras().getString("teaQrCode", "null");
Toast.makeText(TeaFromQrActivity.this, "Text from qr: " + teaQR, Toast.LENGTH_SHORT).show();
}
}
Please tell me where I'm wrong.
launch barcode activity like this
Intent intent = new Intent(this, BarcodeCaptureActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, BARCODE_READER_REQUEST_CODE);
more detail refer BarcodeCaptureActivity,barcodereader
What I am doing is sending Intent to MainActivity from Listview the listview item I am displaying in alertbox.
holder.txtStore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String value = holder.txtStore.getText().toString();
Intent myIntent = new Intent(view.getContext(),MainActivity.class);
myIntent.putExtra("restaurant_name", value);
try {
context.startActivity(myIntent);
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
Toast.makeText(context, "yourActivity is not founded", Toast.LENGTH_SHORT).show();
}
}
});
In MainActivity right now I am using button to start getIntent but I don't want to use button I just want to start getIntent function when alertbox disappear or ya when MainActivity start I try to use onResume or onRestart. But not work for me.
This is my code to call getIntent using button:
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = getIntent();
String restaurant_name = intent.getStringExtra("restaurant_name");
Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
if(restaurant_name != null ) {
if (restaurant_name.equals("Romys")) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89209, 75.82759), 15.0f));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(26.89553, 75.82842))
.title("ROMYS"))
.showInfoWindow();
}
}else {
Toast.makeText(MainActivity.this,"It was not", Toast.LENGTH_LONG).show();
}
}
});
Use onNewIntent to update MainActivity. Kind of like this:
#Override
public void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
String restaurant_name = intent.getStringExtra("restaurant_name");
Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
}
If you are displaying the alertbox from the MainActivity, consider adding flags to your intent to maintain the activity stack.
I am working on an app which uses a custom camera (with Surfaceview and such), I am using startActivityForResult from my ObjectActivity to go to the activity with the camera named CameraActivity. This happens in this method.
public void addPicture(View v) {
final CharSequence[] items = { "Take Photo", "Choose from Gallery", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(ObjectActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
executeOnResume = false;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
int hasWriteExternalStoragePermission = checkSelfPermission(Manifest.permission.CAMERA);
if (hasWriteExternalStoragePermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
REQUEST_CODE_ASK_PERMISSIONS);
}
}
Intent intent = new Intent(ObjectActivity.this,CameraActivity.class);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Gallery")) {
executeOnResume = false;
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
REQUEST_SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
To be more specific, it happens at these lines:
Intent intent = new Intent(ObjectActivity.this,CameraActivity.class);
startActivityForResult(intent, REQUEST_CAMERA);
This works pretty well, but when I try to return to ObjectActivity after taking a picture, which happens here:
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
//TODO Code to process picture taken
//create a new intent...
Intent intent = new Intent();
intent.putExtra("data",data);
//close this Activity...
setResult(Activity.RESULT_OK, intent);
finish();
}
};
It returns to the activity previous to the ObjectActivity named MainActivity, while it's supposed to go back to ObjectActivity and call onActivityResult() :
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
executeOnResume = false;
loadStuff();
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA || requestCode == REQUEST_SELECT_FILE) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Description");
alertDialog.setMessage("Enter Description");
final EditText input = new EditText(this);
alertDialog.setView(input);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alertDialog.setView(input);
alertDialog.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
description = input.getText().toString();
if (description == null || description.equals("")) {
description = "-";
}
try {
savePhoto(requestCode,data);
} catch (IOException e) {
e.printStackTrace();
}
}
});
final AlertDialog.Builder tmpDialog = alertDialog;
final AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setTitle("Direction");
dlgAlert.setMessage("Stand with your phone facing the same direction as the picture made and press Ok");
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mBearingProvider.updateBearing();
bearing = mBearingProvider.getBearing();
cardinalDirection = bearingToString(bearing);
Log.e("Direction", cardinalDirection + "," + bearing);
tmpDialog.create().show();
dialog.dismiss();
}
});
dlgAlert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
tmpDialog.create().show();
}
});
dlgAlert.create().show();
}
}
}
But it never gets there.
Does anyone know why this happens?
Get rid of that getParent. You want to set the result for the current activity, not your parent. So replace:
//close this Activity...
if (getParent() == null) {
setResult(Activity.RESULT_OK, intent);
} else {
getParent().setResult(Activity.RESULT_OK, intent);
}
with
setResult(Activity.RESULT_OK, intent);
I've found the solution, if I don't send over a byte[] with the Intent (but instead maybe write out a file and send the path of that file with the Intent), the code seems to work and the ObjectActivity opens.
Part that I removed/changed:
intent.putExtra("data",data);
I don't know why this works, I'd like to know why, but for now I'm happy it works.
My activity has an accept button witch sends intent to parent through onClickOkayButton method. I have overriden the onBackPressed() method so that it shows an AlertDialog asking the user if he really wishes to leave or if he wants to save the preferences, witch does the exact same action as the accept button.
Is it possible to merge both onClick methods into one so I don't need to copy/paste in case of editing one of those, even if both methods use different parameters?
public void onClickOkayButton(View view) {
// THIS IS THE ACCEPT BUTTON
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!(title.matches("")) || !(title.isEmpty()) || !(title.equals("")) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(this, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onBackPressed() {
if (adapter.getCount() != 0) {
showAlertDialog();
} else {
super.onBackPressed();
}
}
public void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.surveyadd_warn_back_message);
builder.setNegativeButton(R.string.surveyadd_warn_back_save, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// THIS WAS COPIED FROM THE ACCEPT BUTTON
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!(title.matches("")) || !(title.isEmpty()) || !(title.equals("")) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(context, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
});
// UP TO HERE
builder.setPositiveButton(R.string.surveyadd_warn_back_erase, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
You won't be able to use the same callback methods because they are from different interfaces.
What you should do is move all the common code into a third method and just call it from both click handlers like below:
private void setResultAndFinish() {
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!TextUtils.isEmpty(title)) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(context, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
And then the handlers:
public void onClickOkayButton(View view) {
setResultAndFinish();
}
#Override
public void onClick(DialogInterface dialog, int which) {
setResultAndFinish();
}
I have my main activity using the startActivityForResult method which calls an activity that i need to return two string values from. I have it working to return one, but even with all the tutorials and other questions on here i have read i cant seem to get it to return the two values. Below is my code.
here is where i start the second activity:
Button button = (Button) findViewById(R.id.add);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(addM, 1);
}
});
Here is the activity it starts, i need to return the text that is in the titleField(which works now) and the yearField
public class AddMovie extends Activity {
String movieTitle, movieYear;
EditText titleField, yearField;
Button save;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_movie);
titleField = (EditText) findViewById(R.id.titleField);
yearField = (EditText) findViewById(R.id.yearField);
save = (Button) findViewById(R.id.saveMovie);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.setData(Uri.parse(titleField.getText().toString()));
setResult(RESULT_OK, data);
//data.setData(Uri.parse(yearField.getText().toString()));
//setResult(RESULT_OK, data);
finish();
}
});
}
}
Here is the method in my main class that receives results
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == request_Code)
{
if(resultCode == RESULT_OK)
{
tempTitle = data.getData().toString();
//tempYear = data.getStringExtra("movieYear");
Toast.makeText(this, tempTitle, Toast.LENGTH_SHORT).show();
dbAddMovie(tempTitle, tempYear);
}
}
}
The code that is commented out was one attempt at making it receive multiple values, although they failed. Any help with this situation would be great. Thanks!
You should use something like:
Intent returnIntent = new Intent();
returnIntent.putExtra("title",titleField.getText().toString());
returnIntent.putExtra("year",yearField.getText().toString());
setResult(RESULT_OK,returnIntent);
finish();
And on your main activity, onActivityResult:
tempTitle = data.getStringExtra("title");
tempYear = data.getStringExtra("year");
use data.putExtra("keys", data); instead of data.setData(.....)
and get data like;
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String value = extras.getString("keyName");
}
You should be able be put extras on your intent and get that data through an associated tag. This is how I'm doing it from an Activity's onCreate.
Intent mIntent = new Intent(this, MainActivity.class);
mIntent.putExtra("your_tag", "the string you want to get");
startActivity(mIntent);
finish();
Then in the activity you want that data (the MainActivity or whatever you're calling it)....
Intent intent = getIntent();
String value = intent.getExtras().getString("your_tag");
It may also be worth noting that you can Override the onNewIntent in your MainActivity to handle new Intents coming in, mine currently looks like...
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
}
Try this
public class AddMovie extends Activity {
String movieTitle, movieYear;
EditText titleField, yearField;
Button save;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_movie);
titleField = (EditText) findViewById(R.id.titleField);
yearField = (EditText) findViewById(R.id.yearField);
save = (Button) findViewById(R.id.saveMovie);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("title_field", titleField.getText().toString);
data.putExtra("year_field", yearField.getText().toString);
setResult(RESULT_OK, data);
finish();
}
});
}
And this
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == request_Code)
{
if(resultCode == RESULT_OK)
{
tempTitle = data.getStringExtra("title_field");
tempYear = data.getStringExtra("year_field");
Toast.makeText(this, tempTitle, Toast.LENGTH_SHORT).show();
dbAddMovie(tempTitle, tempYear);
}
}
}
You can use a putExtra method on your Intent:
data.putExtra("title", movieTitle);
data.putExtra("year", movieYear);
Then in the Activity that opens, you can get this data as follows:
String title, year;
Intent data = getIntent();
if (data != null) {
title = data.getExtras().getString("title");
year = data.getExtras().getString("year");
}
You can create a new intent and then add your two variables
Intent i = new Intent(getApplicationContext(), asd8.as.hwapp.timemonday.class);
i.putExtra("username",username);
i.putExtra("password", password);
startActivity(i);
And then retrieving the information in your new activity as so;
public void getInformation(){
Bundle extras = getIntent().getExtras();
if (extras != null) {
username = extras.getString("username");
password = extras.getString("password");
}
}