I have a Android Device with a embedded barcode Scanner.
On pressing the "Scan" button on device, it scans the barcode and the data is displayed in the EditText only if the cursor is on EditText. i.e. The EditText is selected.
I wish to get the scanned result without the EditText selected.
I have tried it with onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
But I am not getting any results on this.
What can I do ?
I don't want to press a Button to activate the Scanner.
Using the ZXIng library, you can launch the scanner as an intent and parse the result.
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public void onActivityResult(int requestCode, int resultCode, Intent intent){
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
Context context = getApplicationContext();
if (scanResult != null){
String scanContent = scanResult.getContents();
Toast toast = Toast.makeText(context, scanContent, 5000);
toast.show();
}
else {
Toast toast = Toast.makeText(context, "Error", 5000);
toast.show();
}
};
This intent launched through one of the android lifecycle methods, such as the onCreate() override.
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
Related
am scan Qr code/Barcode, so I want to scan different qr code/barcode and arrange them is String listview.
Example I want to setText() like this ,scanNumber1,scanNumber2,....,..., so far i have managed to scan that Qr Code/Barcode but when i scan the next number it remove the first one, so I want ti arrange those number.
here is my code
public void scanCode(View view){
IntentIntegrator intentIntegrator = new IntentIntegrator(this);
intentIntegrator.initiateScan();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String scanNumber=intentResult.getContents();
if (intentResult != null) {
if(intentResult.getContents() == null) {
Toast toast = Toast.makeText(getApplicationContext(), "Scan The QR code/Barcode", Toast.LENGTH_LONG);
toast.show();
}
//validate ur scan numbers
else if (intentResult.getContents().length() == 10) {
textView1.setText(intentResult.getContents());
}
else if(intentResult.getContents().length() == 12){
textView2.setText(intentResult.getContents());
}
else {
Toast toast = Toast.makeText(getApplicationContext(), "That Number is Out Of Our Range", Toast.LENGTH_LONG);
toast.show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
and this is output
so I want on that Slave to appear like ,scanNumber1,scanNumber2,....,...,
you can use a global JointoString function object to achieve the desired behaviour i.e. "barcode1","barcode2"....
ArrayList<String> results = new ArrayList<String>()
when you receive the result try
results.add(<value>)
while displaying to text view use
textView.setText(results.joinToString(","))
I am trying to get result for dialer Intent using startActivityForResult()
Below is my code for Dialer Intent.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:123456789"));
startActivityForResult(intent, 1234);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1234){
if (resultCode == Activity.RESULT_OK){
Toast.makeText(getApplicationContext(), "result ok", Toast.LENGTH_LONG).show();
}else if (resultCode == Activity.RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Result Cancelled", Toast.LENGTH_LONG).show();
}
}
}
whenever I am returning to my activity, Result Cancelled Toast is triggering.
Thanks in advance.
From doc:
ACTION_DIAL
public static final String ACTION_DIAL
You only have the ACTION. If you want to call a number from your application then you just have to put these lines of code into the onClick() method and can get what you want:
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:123456789"));
startActivity(intent); // no need to use startActivityResult(intent,1234)
Here, If an ACTION_DIAL input is nothing, an empty dialer is started; else getData() is URI of a phone number to be dialed or a tel: <yourURI> of an explicit phone number.
Additionally, there is no "Output" of RESULT_OK or RESULT_CANCELED, Cause, the startActivityResult() doesn't mean anything to ACTION_DIAL but startActivity(intent). Hope it helps.
why am I getting RESULT_CANCELED instead of RESULT_OK.
ACTION_DIAL does not return a result. If you read the documentation for ACTION_DIAL, you will see "Output: nothing". Hence, you will usually get RESULT_CANCELED. Only activities designed for use with startActivityForResult() will return a result code.
Update
Activity A ==> host MyFragment and from here,user started ==> ActivityB from here I need to get some value from this activty for example value integer and some String to MyFragment that hosted in ActivityA
I've MyFragment that start the activityB like this :
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
startActivityForResult(intent,addComment);
and then when user start the activty and finish it I'm trying to pass some value from that ActivityB to MyFragment like this :
#Override
public void onBackPressed() {
super.onBackPressed();
//set ok result before finish the activity
Intent returnIntent = new Intent();
returnIntent.putExtra("idBadgeComment", idBadgeComment);
returnIntent.putExtra("totalCommentInserted", totalCommentInserted);
setResult(RESULT_OK, returnIntent);
finish();
}
and I've implement onActivityResult in MyFragment :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "Result from News Comment ");
if(resultCode== Activity.RESULT_OK){
Log.i(TAG, "Result from News OK");
if(requestCode==addComment){
Log.i(TAG, "Result ReqCode Oke");
int idBadgeComment = data.getExtras().getInt("idBadgeComment");
int totalCommentInserted = data.getExtras().getInt("totalCommentInserted");
Log.i(TAG, "idComment: "+idBadgeComment);
Log.i(TAG, "Total Comment Inserted: "+totalCommentInserted);
}
}
}
also I've called onActivityResult in ActivityA that hosted MyFragment :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/**
* call below code to get the result on the fragment
*/
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.viewpager);
fragment.onActivityResult(requestCode, resultCode, data);
}
when I try the code above I only get Log.i(TAG, "Result from News Comment "); and it didn't get the RESULT_OK value and the Intent data is null, can someone please pointed out where did I go wrong? and how to pass the value from activity to fragment ? or maybe a better way instead of using onActivityResult?
You can try putting super.onBackPressed(); as the last line since RESULT_CANCELED is returned on back press.
Just startActivity by activity.
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
getActivity().startActivityForResult(intent,addComment);
Or put your onActivityResult to fragment.
There are many question here from people having the exact same problem as I, and I've looked though a million, tried different things for 2-3 hours now, and I still can't get it working.
Child Activity:
Intent resultIntent = new Intent(myColorPicker.this, WidgetConfig.class);
resultIntent.putExtra("key", appwidget_notecolor);
setResult(RESULT_OK, resultIntent);
finish();
Parent Activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Toast does not show
Toast.makeText(getApplicationContext(), "onActivityResult fired", Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK ) {
// Toast does not show
Toast.makeText(getApplicationContext(), "Result Recieved", Toast.LENGTH_SHORT).show();
}
}
I launch child activity from parent activity like this:
Intent myColorPickerIntent = new Intent(WidgetConfig.this, myColorPicker.class);
myColorPickerIntent.putExtra("appwidget_notecolor", appwidget_notecolor);
WidgetConfig.this.startActivity(myColorPickerIntent);
Of course you won't get the result, you're calling startActivity() instead of startActivityForResult().
You don't seem to be calling startActivityForResult() after creating the Intent.
Are you pass the intent to startActivityForResult() method?
I created a 'New File' activity using
startActivityForResult(new Intent(MainActivity.this, NewFile.class),1);
The NewFile activity lets users set certain options for their text file then after clicking a button a string is saved to a static variable in my StringBuilder class and finish(); is called. How can I load this new string into the MainActivity's EditText? onCreate() is only called when the activity is first created right?
Do it on onResume or onActivityResult. It would be ideal though onActivityResult since you've used startActivityForResult, before finishing the other activity you set the setResult(int resultCode, Intent data) if you have intent to sent back or if none setResult(int resultCode). I think it is better to put the string which will be used to update your EditText as extra in the intent, then set the text using that string in onActivityResult method.
#Override
protected void onResume() {
super.onResume();
et.setText(DocumentBuilder.docText);
}
in your class NewFile.java :
String strName = "toto";
Intent intent = new Intent();
intent.putExtra("name", "toto");
setResult(1, intent);
finish();
in your MainActivity.java :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// Handle successful result
String name = intent.getStringExtra("name");
editText.setText(name);
}
}
}
refer this tutorial for more explanations