Android Java, Setting text of a field outside of the application - java

I'm currently writing an Android application that will allow a user to input the result of a scanned barcode into a field outside of the application.
The app will simply be a Floating Button that when pressed, will bring up a camera screen that scans and reads the barcode being pointed at, and save the result as an intent.
The user will primarily be working on Chrome when using this Floating Button.
Question: Is it possible to set the text of the current field in focus, outside of the application, as the result of the scanned barcode?
I'm using the following libraries:
- com.google.zxing:core:3.3.3
- com.journeyapps:zxing-android-embedded:3.5.0#aar
Current code:
public class MainActivity extends AppCompatActivity {
FloatingActionButton fabScan;
TextView textViewScanResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Activity activity = this;
textViewScanResult = findViewById(R.id.textViewScan);
fabScan = findViewById(R.id.floatingActionButton_scan);
fabScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan Barcode");
integrator.setCameraId(0);
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
integrator.setOrientationLocked(false);
integrator.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(getApplicationContext(), "nothing", Toast.LENGTH_SHORT).show();
} else {
textViewScanResult.setText(result.getContents());
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Currently, i'm only setting a textView as the scanned result.

try setting browser intent with the result content.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(getApplicationContext(), "nothing", Toast.LENGTH_SHORT).show();
} else {
textViewScanResult.setText();
//here
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.stackoverflow.com")); //change url to google
intent.putExtra(result.getContents().toString());
startActivity(intent);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}

Related

How do I pass ArrayLists to another activity, modify it, and return it back to use?

I'm trying to create a login/register part of a project, and I'm having trouble with passing the sign-up information back to the login activity. I initialized username_info, password_info, and name_info in MainActivity, and I want to send it to SignUpActivity through Intent.
Intent i = new Intent(this, SignUpActivity.class);
i.putExtra("username_info", username_info);
i.putExtra("password_info", password_info);
i.putExtra("name_info", name_info);
startActivityForResult(i, 101);
After values are added in the other activity, it's sent back like this (the arraylists have the same name in both activities):
Intent r = new Intent();
r.putExtra("username_info", username_info);
r.putExtra("password_info", password_info);
r.putExtra("name_info", name_info);
setResult(Activity.RESULT_OK, r);
finish();
}
and it's received here:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Bundle e = getIntent().getExtras();
username_info = e.getStringArrayList("username_info");
password_info = e.getStringArrayList("password_info");
name_info = e.getStringArrayList("name_info");
}
}
}
But the array lists are unchanged when I get back to the MainActivity. I'm new to Android Studio, so I might just be making a simple mistake.
EDIT:
I'm crashing when the username and password don't match, but it should be returning a toast instead:
#Override
public void onClick(View v) {
username = username_input.getText().toString();
password = password_input.getText().toString();
int index = username_info.indexOf(username);
if (username_info.size() < 1) {
Toast.makeText(MainActivity.this, "You must sign up first", Toast.LENGTH_SHORT).show();
}
else if (password_info.get(index).equals(password)) {
Toast.makeText(MainActivity.this, "make an activity", Toast.LENGTH_SHORT).show();
// open activity
}
else {
Toast.makeText(MainActivity.this, "Incorrect username/password", Toast.LENGTH_SHORT).show();
}
}
});
Don't use getIntent() in onActivityResult, instead use the Intent data:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Bundle e = data.getExtras();
username_info = e.getStringArrayList("username_info");
password_info = e.getStringArrayList("password_info");
name_info = e.getStringArrayList("name_info");
}
}
}

How to check from which onResume the user is coming

I have a problem. The following Picture visualises my issue. We start in App 1 and press the button. Now we either get to see the screen of app2 or the setting screen. Now when i press the back button (marked as the red circle in app2 and settings) and we get back to the app1 screen. But what i wanna know is from what screen we get back to the app1 screen.
#Override
protected void onResume() {
super.onResume();
// if(from setting) {
// do this
}
NfcManager manager = (NfcManager) this.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {
Intent userHomeScreen = new Intent(getApplicationContext(), UserHomeActivity.class);
startActivity(userHomeScreen);
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nfc_setting);
NfcManager manager = (NfcManager) this.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if (currentUser != null){
Intent userHomeScreen = new Intent(getApplicationContext(), UserHomeActivity.class);
startActivity(userHomeScreen);
}else{
Intent loginScreen = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(loginScreen);
}
}
else {
btn_nfc_navigate_setting = findViewById(R.id.btn_nfc_navigate_setting);
btn_nfc_navigate_setting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
}
});
}
Look now that I understand whats going on, you have to startActivityForResult() not startActivity():
Now in the activity that you use to open other activities:
When you open Log in screen:
Intent loginScreen = new Intent(getApplicationContext(), LoginActivity.class);
int requestCode = 10;
startActivityForResult(loginScreen , requestCode);
When you open settings
btn_nfc_navigate_setting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int requestCode = 11;
startActivityForResult(new Intent(Settings.ACTION_NFC_SETTINGS) , requestCode);
}
});
Override this method to listen to from where you came instead of onResume()
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//this is triggered when you come back to this activity
if(requestCode == 10){
//we came from login
}else if(requestCode == 11){
//we came from settings
}
}
Use startActivityForResult()
From App1 to App2 or Setting:
int FLAG_APP2 = 1
Intent i = new Intent(this, App2.class);
startActivityForResult(i, FLAG_APP2);
Then in App2 button back tapped:
finish();
Then in App1 override onActivityResult():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FLAG_APP2) {
//here you know this is from App2 Screen
}
}
If you want to know the result of an Intent (in this case settings or app2), you can use
startActivityForResult(intent, requestCode)
instead of
startActivity(intent).
Where request code should be unique for different intents (in this case settings and app2).
To handle the result you have to override the onActivityResult() in the actvity.
So your required code will be
#Override
protected void onResume() {
super.onResume();
// if(from setting) {
// do this
// }
NfcManager manager = (NfcManager) this.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {
Intent userHomeScreen = new Intent(getApplicationContext(), UserHomeActivity.class);
startActivityForResult(userHomeScreen,1);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
// Activity returned from userHomeScreen
}
else if (requestCode ==2) {
// Activity returned from Settings
}
}
UPDATE:
To add to that above if you want to access any Settings FEATURE you can use SETTINGS Constants as intent parameters.
Settings Documentation
For example, if you want to access NFC settings:
// Look into the constants in the documentation to know which constants to use in intent
Intent intent = new Intent(Settings.ACTION_NFC_SETTINGS);
startActivityForResult(intent, 2);

how do I get my intent to work with Google Place Autocomplete?

Can you tell me what I need to do to make my intent work please? When the user clicks mEditInit, the Places drop down menu appears.
When the user clicks the place in the menu, I want to send this back to mEditInit
I've used intents before but not working in this case.
public class MyActivity extends AppCompatActivity {
String stuff;
private EditText mEditInit;
public static final int AUTOCOMPLETE_REQUEST_CODE = 1;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mEditInit = (EditText) findViewById(R.id.edit_message);
mEditInit.setX(0);
mEditInit.setY(250);
//Get the bundle
Bundle bundle = getIntent().getExtras();
if (stuff != null && !stuff.isEmpty())
//Extract the data…
{ stuff = bundle.getString("stuff");
mEditInit.setText(stuff);
}
Places.initialize(this, getString(R.string.places_api_key));
mEditInit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "bowwow", Toast.LENGTH_SHORT).show();
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fields).build(MyActivity.this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
//setResult(RESULT_OK, intent);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
//Toast.makeText(MyActivity.this, place.getName(), Toast.LENGTH_SHORT).show();
Toast.makeText(MyActivity.this, place.getName(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(this, MyActivity.class);
String getrec= place.getName();
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("stuff", getrec);
//Add the bundle to the intent
i.putExtras(bundle);
// Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
// Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
// super.onActivityResult(requestCode, resultCode, data);
}
}
}
In your onActivityResult method, you do not need any extra.
Just call the method setText() on your EditText:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
//Toast.makeText(MyActivity.this, place.getName(), Toast.LENGTH_SHORT).show();
mEditInit.setText(place.getName());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
// Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
// super.onActivityResult(requestCode, resultCode, data);
}
}

Integrate git project on an existing project startActivityForResult and onActivityResult android

I implemented this git on my current project without cloning
implementation 'com.github.adityaarora1:LiveEdgeDetection:master-SNAPSHOT'
But I'm unable to call it on my method. The document says
Start startActivityForResult from your activity
startActivityForResult(new Intent(this, ScanActivity.class), REQUEST_CODE);
Get a file path for cropped image on onActivityResult
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
So I tried calling like this onClick button from a new Class
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this,
ScanActivity.class);
startActivity(myIntent);
}
and put the rest inside my onActivityResult
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
}
Edit: here is the MainActivity the author used on git I tried using it I get this error:
scannedImageView = findViewById(com.adityaarora.liveedgedetection.R.id.scanned_image);
MainActivity (imported)
private static final int REQUEST_CODE = 101;
private ImageView scannedImageView;
Button scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
scannedImageView = findViewById(com.adityaarora.liveedgedetection.R.id.scanned_image);
startScan();
scan = findViewById(R.id.open_scan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(ScanActivity2.this,
ScanActivity.class);
startActivityForResult(myIntent ,111);
}
});
}
private void startScan() {
Intent intent = new Intent(this, ScanActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if(resultCode == Activity.RESULT_OK) {
if(null != data && null != data.getExtras()) {
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
scannedImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
scannedImageView.setImageBitmap(baseBitmap);
}
} else if(resultCode == Activity.RESULT_CANCELED) {
finish();
}
}
}
Update :
After some research I found that the imported project was on read file only and cannot be changed (ScanActivity.java) and my current project was updated sdk 28 which is different from the one Imported so there is some errors in ScanActivity which Is why the button (technically) wasn't working
You should use startActivityForResult instead of startActivity like below.
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this,
ScanActivity.class);
startActivityForResult(myIntent ,111);
}
});
and modify your onActivityResult like
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == 111){
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
Log.d("YourTAG","File Path "+filePath);
// here you can set bitmap to your image view
yourImageView.setImageBitmap(baseBitmap);
}
}
UPDATE
You Main Activity should be like
private static final int REQUEST_CODE = 111;
private ImageView scannedImageView;
Button scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
scannedImageView = findViewById(R.id.scanned_image); // this ImageView should be in your activity_scan.xml file with same id(scanned_image)
startScan();
scan = findViewById(R.id.open_scan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startScan();
}
});
}
private void startScan() {
Intent intent = new Intent(ScanActivity2.this, ScanActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if(resultCode == Activity.RESULT_OK) {
if(null != data && null != data.getExtras()) {
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
scannedImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
scannedImageView.setImageBitmap(baseBitmap);
}
} else if(resultCode == Activity.RESULT_CANCELED) {
finish();
}
}
}
Demo project
I have uploaded a demo project on Github which is integrated LiveEdgeDetection library and working as expected.
To check it go here

Zxing Scanner App, How to show a result as a yes or no?

Using zxing on Android Studio with empty activity and 4.4 KitKat. I would like to show the result as a yes or no. I would like to reference a google sheet with the UPC codes. If the UPC is in the spreadsheet already then show "YES" or if the UPC is not in the spreadsheet show "NO". Can I use an "If, Else" in the MainActivity java code using only the UPC column on the google sheet?
#Override
protected void onActivityResult ( int requestCode, int resultCode, Intent data){
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
I want to show this code as a "Yes" or "NO" based on if the scanned barcode is already in the spreadsheet. My question is how to get the spreadsheet to communicate with the scanner or can copy all of the barcodes from the spreadsheet in a boolean?
Set<String> getStringSet (String key,
Set<String> defValues)
makeText(Context context, int resId, int duration)
protected void onActivityResult ( int requestCode, int resultCode, Intent data){
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
So above is what ive come up with from search. Is it practical to use shared preference to store my data(1000 UPC) use ZXING to parse as well as IF ELSE to check the code and finally TOAST to show "YES" or "NO" based on if my result is located in the shared preference?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = this.findViewById(R.id.button);
final Activity activity = this;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
public void scanCode(View view) {
}
public class myWebView extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = new WebView(this);
setContentView(webview);
WebView myWebView = findViewById(R.id.myWebView);
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.loadUrl("https://google.com/");
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
}
for segregation of code you need to create SacnActivity as a separate activity which is extend Activity and impliments ZXingScannerView.ResultHandler and call from the MainActivity and give call back to Mainactivity in onActivityResult().
MainActivity
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scan = (Button)findViewById(R.id.btnScan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this,SacnActivity .class),1);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
Log.d("result================>",""+result);
try {
//processData(result);
} catch (Exception e) {
e.printStackTrace();
}
}
if (resultCode == Activity.RESULT_CANCELED) {

Categories