I am making an android studio app and I want it to make a TextView disappear when I say a certain phrase. The code saves everything I say into an array list. I want to see if it includes the phrase.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 100:
if (resultCode == RESULT_OK && data != null){
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
boolean yes = Arrays.asList(result.get(0)).contains(InputActivity.Item1);
if(yes == true){
result1.setVisibility(View.GONE);
}
}
break;
}
}
Use
for(String s:result){
if(s.equals(InputActivity.Item1)){
//hide text view
}
}
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 have to throw in the towel on this. I am trying to use Image Cropper: Arthur Hub in a Fragment and I keep getting this
error: onActivityResult(int,int,Intent) in ProfileFragment cannot
override onActivityResult(int,int,Intent) in Fragment attempting to
assign weaker access privileges; was public
Here is the imageCropper function in the fragment:
private void ImagePicker() {
CropImage.activity(mainImageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(startActivityForResult();,this);
}
And here is the onActivityResult in the same fragment I am using to obtain the image:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == Activity.RESULT_OK) {
mainImageUri = result.getUri();
profileImage.setImageURI(mainImageUri);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
I had this implemented previously in an activity and it worked fine. As soon as I adjusted it to work in a Fragment, I cannot proceed.
Please help! Also I am a relatively new developer so please be a bit more descriptive in your explanation. Thanks!
In fragment overrides onActivityResult like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { //note: public, not protected.
super.onActivityResult(requestCode, resultCode, data);
}
and in fragment call startActivityForResult method.
I want two ImageView's on one activity view, One image for the profile picture and the other for the profile cover page.
My code
setupImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bringImagePicker();
}
});
private void bringImagePicker() {
// start picker to get image for cropping and then use the image in cropping activity
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1)
.start(SetupActivity.this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mainImageURI = result.getUri();
setupImage.setImageURI(mainImageURI);
isChanged = true;
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
Now how can I add for profile cover image?
You can use ID for both of them, and change the image with corresponding ID from the intent that holds data, add another condition after receiving the results.
I don't know if this library can handle this or not.
If not this simplest solution to hold state variable that tells you which image to update
feel free to ask for clarification
I am having some issues with ZXing's barcode scanner in my android application. I asked a question here yesterday that explains a null pointer error that I was having. The issue was solved and just showed that the cursor wasnt retrieving any information back.
My issues is that I know query is correct and I know the code is correct because I have tested it out without the scanner separately and it all works fine. Is it possible that I am calling to the database wrong in the barcode scanner intent or is there a certain way that I should be doing it?
Any help or guidance would be great!
onActivity Result
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
//get the extras that are returned from the intent
barcode_number = intent.getStringExtra("SCAN_RESULT");
String bNumber = barcode_number.toString();
//Code to add scanned item to DB, needs barcode data before can be ruin
final Cursor barInformation = adapter.barcodeInfo(barcode_number);
barInformation.moveToFirst();
String itemName = barInformation.getString(barInformation.getColumnIndex("barcode_item_name"));
Toast toast = Toast.makeText(this, "Barcode Number:" + itemName ,Toast.LENGTH_SHORT);
toast.show();
}
}
}
Query
public Cursor barcodeInfo(String number){
// Safe check to make sure db and number is not null
if(db == null || number == null){
return null;
}
return db.rawQuery("select _id, barcode_item_name, barcode_measurement, barcode_unit from barcode where barcode_number = ?", new String[]{number});
}
Error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
02-12 11:15:07.544 12808-12808/com.example.rory.prototypev2 E/AndroidRuntime: at com.example.rory.prototypev2.DBMain.barcodeInfo(DBMain.java:437)
02-12 11:15:07.544 12808-12808/com.example.rory.prototypev2 E/AndroidRuntime: at com.example.rory.prototypev2.scanner.onActivityResult(scanner.java:77)
Try like this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String strData = scanResult.getContents(); // use this
} else {
Toast.makeText(this, "Error message", Toast.LENGTH_LONG).show();
}
break;
}
}
}
I have fixed the issue. My problem was that I wasnt opening the database before I was calling the functions and thus the function was a null object. The correct code is as below.
adapter.open(); // I was missing this line
Cursor c = adapter.barcodeInfo(bNumber);
c.moveToPosition(-1);
while(c.moveToNext())
{
name = c.getString(c.getColumnIndex("barcode_item_name"));
measurement = c.getInt(c.getColumnIndex("barcode_measurement"));
unit = c.getString(c.getColumnIndex("barcode_unit"));
Toast toast = Toast.makeText(this, "Adding" + name + "to Kitchen", Toast.LENGTH_SHORT);
toast.show();
}
I have an app where at an activity I am taking a photo (among other things) .
Now, when I press the button to take the photo it opens the camera.If i will press the back button or the cancel button (not taking photo) ,it crashes and gives
nullpointer
and
Failure delivering result ResultInfo
in this line:
Bitmap photo = (Bitmap) data.getExtras().get("data");
I use:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_REQUEST){
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
blobvalue = stream.toByteArray();
Bundle extras = new Bundle();
Intent k=new Intent(this,MainActivity.class);
extras.putParcelable("Bitmap", photo);
k.putExtras(extras);
}
if (requestCode == RESULT_CANCELED) {
}
}
and in my adapter:
ImageView myImage=(ImageView) convertView.findViewById(R.id.myimage);
final Bitmap image;
if(theItems.getImagemyItems() != null)
{
byte []temp = theItems.getImagemyItems();
image = BitmapFactory.decodeByteArray(temp, 0, temp.length);
myImage.setImageBitmap(image);
}
else
{
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
myImage.setImageBitmap(image);
}
As far as I remember , the above used to workd for this purpose.
I don't know what else to do.
You have just tested requestCode but haven't resultCode so I would suggest you to check resultCode whether user has captured image or cancel capturing.
Try:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST){
if (resultCode == Activity.RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
}
else if (resultCode == Activity.RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
You just have to place a check in your onActivityResult , the case RESULT_OK is when the user takes the picture successfully and the case RESULT_CANCELLED is when you press the hardware back button and want to return to your activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_REQUEST){
if(resultCode == RESULT_OK){
// your code comes here
}
if(resultCode == RESULT_CANCELED){
}
}
}