Taking index of a string - java

Hello I have an edittext and an button.
I want when the user enters a number by pressing the number to be taken and put a bet on the index penultimate number.
Basically, what should I do? Thank you.
I try to do this with the following code:
public class BarcodScanner extends AppCompatActivity implements OnClickListener {
private Button scanBtn;
private Button nextLevel;
private EditText formatTxt, contentTxt;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scanner);
scanBtn = (Button)findViewById(R.id.scan_button);
nextLevel =(Button)findViewById(R.id.btn_enter);
textView = (TextView) findViewById(R.id.view1);
formatTxt = (EditText) findViewById(R.id.scan_format);
contentTxt = (EditText) findViewById(R.id.scan_content);
nextLevel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String inn = formatTxt.getText().toString();
int length_s = inn.length();
char m = inn.charAt(length_s - 2);
if ( m == 1){
Toast.makeText(BarcodScanner.this, "قبض آب",Toast.LENGTH_LONG).show();
}
}
});
scanBtn.setOnClickListener(this);
}
public void onClick(View v){
if(v.getId()==R.id.scan_button){
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
if (scanContent != null) {
formatTxt.setText(scanContent.substring(0, 13));
contentTxt.setText(scanContent.substring(18));
}
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}

Related

How to pass data from return activity to dialog in previous activity?

Visitor Details
I have a dialog in Visitor Activity. When I click on the icon, it will redirect to Camera Activity. Then when click Confirm button in Camera Activity, it will send the intent back to previous Activity which is Visitor Activity. The problem is how should I pass the intent result from Camera Activity back to the dialog in Visitor Activity. The intent data should be placed in the Pass No Edit Text. I'm using material dialog without any Fragment that attached to that dialog. It all placed in Visitor Activity. Can someone please help me? Thank you!
private void checkin()
{
final MaterialDialog dialog = new MaterialDialog.Builder(VisitorDetailActivity.this)
.customView(R.layout.sample,false)
.build();
View view = dialog.getCustomView();
final EditText etPassNo = (EditText) view.findViewById(R.id.etPassNo);
final EditText etVehicleNo = (EditText) view.findViewById(R.id.etVehicleNo);
final ImageView ivScanCode = (ImageView) view.findViewById(R.id.ivScanCode);
final TextView tvCancel = (TextView) view.findViewById(R.id.tvCancel);
final TextView tvSubmit = (TextView) view.findViewById(R.id.tvSubmit);
etVehicleNo.setText(model.getFldVehicleNo());
if(barCode != null)
{
etPassNo.setText(barCode);
}
ivScanCode.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//Toast.makeText(VisitorDetailActivity.this, "Bar code scanner", Toast.LENGTH_SHORT).show();
Intent i = new Intent(VisitorDetailActivity.this, ScanCodeActivity.class);
startActivityForResult(i,97);
}
});
tvCancel.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
tvSubmit.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(etPassNo.getText().toString().trim().isEmpty())
{
Toast.makeText(VisitorDetailActivity.this, "Pass No is required", Toast.LENGTH_SHORT).show();
return;
}
if(selImage == null)
{
proceedCheckin(etPassNo.getText().toString(), etVehicleNo.getText().toString());
}
else
{
proceedCheckinImage(etPassNo.getText().toString(), etVehicleNo.getText().toString());
}
}
});
dialog.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 98 && resultCode == RESULT_OK)
{
selImage = data.getStringExtra("photo");
Glide.with(VisitorDetailActivity.this).load(selImage).into(iDrivingLicense);
}
else if (requestCode == 99 && resultCode == RESULT_OK)
{
selImage = data.getStringExtra("photo");
Glide.with(VisitorDetailActivity.this).load(selImage).into(iDrivingLicense);
}
else if(requestCode == 97 && resultCode == RESULT_OK)
{
barCode = data.getStringExtra("barCode");
}
}
First you need to take the view object globally out of that checkIn methoed
View view;
view = dialog.getCustomView();
final EditText etPassNo = (EditText) view.findViewById(R.id.etPassNo);
Now under your onActivityResult method, as i can see you already getting the barcode there, so just initiate the EditText object again on that same simply set the data in it.
barCode = data.getStringExtra("barCode");
EditText etPassNo = (EditText) view.findViewById(R.id.etPassNo);
etPassNo.setText(barCode);
Let me know if that works.
you can do it using interface
public interface OnBarcodeSelect{
void onBarcodeSelected(String barcode);}
OnBarcodeSelect onBarcodeSelect;
private void checkin()
{
final MaterialDialog dialog = new MaterialDialog.Builder(VisitorDetailActivity.this)
.customView(R.layout.sample,false)
.build();
View view = dialog.getCustomView();
final EditText etPassNo = (EditText) view.findViewById(R.id.etPassNo);
final EditText etVehicleNo = (EditText) view.findViewById(R.id.etVehicleNo);
final ImageView ivScanCode = (ImageView) view.findViewById(R.id.ivScanCode);
final TextView tvCancel = (TextView) view.findViewById(R.id.tvCancel);
final TextView tvSubmit = (TextView) view.findViewById(R.id.tvSubmit);
etVehicleNo.setText(model.getFldVehicleNo());
if(barCode != null)
{
etPassNo.setText(barCode);
}
ivScanCode.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//Toast.makeText(VisitorDetailActivity.this, "Bar code scanner", Toast.LENGTH_SHORT).show();
Intent i = new Intent(VisitorDetailActivity.this, ScanCodeActivity.class);
onBarcodeSelect = new OnBarcodeSelect() {
#Override
public void onBarcodeSelected(String barcode) {
//handle barcode here
etPassNo.setText(barcode);
}
}
startActivityForResult(i,97);
}
});
tvCancel.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
tvSubmit.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(etPassNo.getText().toString().trim().isEmpty())
{
Toast.makeText(VisitorDetailActivity.this, "Pass No is required", Toast.LENGTH_SHORT).show();
return;
}
if(selImage == null)
{
proceedCheckin(etPassNo.getText().toString(), etVehicleNo.getText().toString());
}
else
{
proceedCheckinImage(etPassNo.getText().toString(), etVehicleNo.getText().toString());
}
}
});
dialog.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 98 && resultCode == RESULT_OK)
{
selImage = data.getStringExtra("photo");
Glide.with(VisitorDetailActivity.this).load(selImage).into(iDrivingLicense);
}
else if (requestCode == 99 && resultCode == RESULT_OK)
{
selImage = data.getStringExtra("photo");
Glide.with(VisitorDetailActivity.this).load(selImage).into(iDrivingLicense);
}
else if(requestCode == 97 && resultCode == RESULT_OK)
{
barCode = data.getStringExtra("barCode");
if(onBarcodeSelect != null){
onBarcodeSelect.onBarcodeSelected(barCode);
}
}
}

i want know how Read text from editText and show the answer

I have a question, by pressing long on imageView imageViewClick.setOnLongClickListener recognizes the voice and answers the question. How do I make the normal press imageViewClick.setOnClickListener recognize the text of the editText and answer the question?
i want know how Read text from editText and show the answer
Thanks
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
imageViewClick = (ImageView) findViewById(R.id.imageViewClick);
imageViewClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
imageViewClick.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "es-ES");
voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "es-MX");
voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "pr-PR");
startActivityForResult(voice, VOICE_RECOGNIZER);
return false;
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == VOICE_RECOGNIZER){
ArrayList<String> recognized = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String listened = recognized.get(0);
editTextListening.setText(listened);
prepareAnswer(listened);
}
}
private void prepareAnswer(String listened) {
String normalize = Normalizer.normalize(listened, Normalizer.Form.NFD);
String withouttilde = normalize.replaceAll("[^\\p{ASCII}]", "");
int result;
String answer = arrayListAnswer.get(0).getAnswer();
for (int i = 0; i < arrayListAnswer.size(); i++) {
result = withouttilde.toLowerCase().indexOf(arrayListAnswer.get(i).getQuestion());
if(result != -1){
answer = arrayListAnswer.get(i).getAnswer();
}
}
answerTo(answer);
}
private void answerTo(String simpleAnswer) {
textViewAnswer.setText(simpleAnswer);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeechRead.speak(simpleAnswer, TextToSpeech.QUEUE_FLUSH, null, null);
}else {
textToSpeechRead.speak(simpleAnswer, TextToSpeech.QUEUE_FLUSH, null);
}
}
public void initialize(){
editTextListening = (EditText) findViewById(R.id.editTextQuestion);
textViewAnswer = (TextView) findViewById(R.id.textViewAnswer);
arrayListAnswer = provideData();
textToSpeechRead = new TextToSpeech(this, this);
}
#Override
public void onInit(int status) {
}
public ArrayList<AnswersActivity> provideData(){
ArrayList<AnswersActivity> answers = new ArrayList<>();
answers.add(new AnswersActivity("defecto", "No estoy programado para hablar de eso"));
answers.add(new AnswersActivity("puff", "Puff"));
answers.add(new AnswersActivity("chiste", "¿Sabes que mi hermano anda en bicicleta desde los 4 años? Mmm, ya debe estar lejos"));
answers.add(new AnswersActivity("adios", "que descanses"));
answers.add(new AnswersActivity("estas", "esperando serte de ayuda"));
answers.add(new AnswersActivity("dj", "YEY BALBIN"));
return answers;
}
}
To recognize the input in EditText and convert the input to speech, you can use the TextToSpeech class provided by Android. Example of this:
textToSpeech = new TextToSpeech(MainActivity.this, MainActivity.this);
editText = (EditText) findViewById(R.id.editText);
imageViewClick = (ImageView) findViewById(R.id.imageViewClick);
imageViewClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextToSpeechFunction() ;
}});
}
public void TextToSpeechFunction()
{
String text = editText.getText().toString();
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
Toast.makeText(MainActivity.this , text, Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
textToSpeech.shutdown();
super.onDestroy();
}
#Override
public void onInit(int TexttoSpeechCurrentStatus) {
if (TexttoSpeechCurrentStatus == TextToSpeech.SUCCESS) {
textToSpeech.setLanguage(Locale.US);
imageViewClick.setEnabled(true);
TextToSpeechFunction();
}
}

Retrieve contact from mobile's phone directory

This code can open a built-in contacts directory on a mobile device. However, when I select a contact, the app reports "done", but doesn't retrieve the contact number -- the user can't send a message.
How do I retrieve the contact number? List View is not a solution: I need to return just the one number, not extra information.
Here's my code:
public EditText no;
public EditText msg;
public Button cancel;
public Button snd;
public String Number,name,Message;
public int run=0;
public static final int PICK_CONTACT=1;
public void callContacts(View v)
{
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,PICK_CONTACT);
}
#Override
public void onActivityResult(int reqCode,int resultCode,Intent data)
{
super.onActivityResult(reqCode,resultCode,data);
if(reqCode==PICK_CONTACT)
{
if(resultCode== ActionBarActivity.RESULT_OK)
{
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData,null,null,null,null);
if(c.moveToFirst())
{
name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.CONTENT_URI.toString()));
Toast.makeText(this,"done",Toast.LENGTH_SHORT).show();
}
}
}
}
and here is the onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
no = (EditText)findViewById(R.id.num);
msg = (EditText)findViewById(R.id.sms);
cancel = (Button)findViewById(R.id.cancel);
snd = (Button)findViewById(R.id.snd);
no.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
msg.setText("");
no.setText("");
callContacts(null);
}
});
snd.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Number = no.toString();
Message = msg.getText().toString();
SmsManager manager = SmsManager.getDefault();
ArrayList<String>long_sms = manager.divideMessage(Message);
manager.sendMultipartTextMessage(Number,null,long_sms,null,null);
Toast.makeText(getApplicationContext(),"Sent Successfully",Toast.LENGTH_SHORT).show();
}
});
}
Here is the code just for selecting one phone number from contact list. i found this code simplest. let me know if there is any problem.
start activity with pick intent on phone data type:
findViewById(R.id.choose_contact_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(pickContact, PICK_CONTACT_REQUEST);
}
});
Now set onAcitivityResult();
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
switch (requestCode){
case PICK_CONTACT_REQUEST:
if (resultCode == RESULT_OK){
Uri contactUri = intent.getData();
Cursor nameCursor = getContentResolver().query(contactUri, null, null, null, null);
if (nameCursor.moveToFirst()){
String name = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
((EditText)findViewById(R.id.person_name)).setText(name);
((EditText)findViewById(R.id.enter_mobile)).setText(number);
nameCursor.close();
}
}
break;
}
}

How to open dialer on phone with a selected number in android [duplicate]

This question already has answers here:
How do I get the dialer to open with phone number displayed?
(7 answers)
Closed 6 years ago.
I have made an application that uses Google Places API. Once I click on a place it returns the name of the company, the address and the number to the MainActivity. When I click on the number I can open the dialer but I can't seem to get the number that is returned on the MainActivity into the dialer. Any ideas on how I could go about doing this. Thanks
My Code is as follows
MainActivity
private TextView mName;
private TextView mAddress;
private TextView mNumber;
private static final LatLngBounds Sligo = new LatLngBounds(
new LatLng(54.27, -8.47), new LatLng(54.27, -8.47));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
mName = (TextView) findViewById(R.id.textView);
mAddress = (TextView) findViewById(R.id.textView2);
mAttributions = (TextView) findViewById(R.id.textView3);
mNumber = (TextView) findViewById(R.id.textView4);
Button pickerButton = (Button) findViewById(R.id.pickerButton);
pickerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
PlacePicker.IntentBuilder intentBuilder =
new PlacePicker.IntentBuilder();
intentBuilder.setLatLngBounds(Sligo);
List<Integer> filterTypes = new ArrayList<Integer>();
filterTypes.add(Place.TYPE_CAR_REPAIR);
Intent intent = intentBuilder.build(MainActivity.this);
startActivityForResult(intent, PLACE_PICKER_REQUEST );
} catch (GooglePlayServicesRepairableException
| GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
#Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST
&& resultCode == Activity.RESULT_OK) {
final Place place = PlacePicker.getPlace(this, data);
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
final CharSequence formatted_phone_number = place.getPhoneNumber();
// final CharSequence car = place.TYPE_CAR_REPAIR();
//public abstract List<Integer> getTypeFilter(place.TYPE_CAR_REPAIR);
String attributions = (String) place.getAttributions();
if (attributions == null) {
attributions = "";
}
mName.setText(name);
mAddress.setText(address);
mAttributions.setText(Html.fromHtml(attributions));
mNumber.setText(formatted_phone_number);
} else {
super.onActivityResult(requestCode, resultCode, data);
}
onClick event
public void onClickNumber(View arg)
{
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
}
The number is at the bottom of the screen
Try This
String phone = mNumber.getText().toString();
Intent phoneIntent = new Intent(Intent.ACTION_DIAL, Uri.fromParts(
"tel", phone, null));
startActivity(phoneIntent);
Try this.
String number = "494498498";
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" +number));
startActivity(intent);

zxing bar code scanner is not returning results

I am trying to make this app to scan for a book's ISBC and then make a http request to fetch for the book's title and other details.
I got the app working in terms of initiating the camera with rootView.findViewById(R.id.scan_button)..... . Now I want to log the bar code and the code format to make sure it's working in onActivityResult but it's not logging or making a toast. The camera activity closes after the camera focuses on the code and returns to the view where I have the button to initiate the scan function but doesn't log anything or making any toast. I used this tutorial - http://code.tutsplus.com/tutorials/android-sdk-create-a-barcode-reader--mobile-17162 - earlier to see how zXing works and the tutorial worked perfectly and now I'm trying to implement some of the codes.
rootView.findViewById(R.id.scan_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
scanIntegrator.setBeepEnabled(true);
scanIntegrator.setBarcodeImageEnabled(true);
scanIntegrator.setPrompt("Scan a barcode");
scanIntegrator.initiateScan();
}
});
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
Log.i("FORMAT ", scanFormat);
Log.i("CONTENT", scanContent);
Log.i("xZing", "contents: "+scanContent+" format: "+scanFormat);
Toast.makeText(getActivity(), "FORMAT " + scanFormat + " CONTENT " + scanContent, Toast.LENGTH_LONG).show();
}else{
Toast toast = Toast.makeText(getActivity(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
full code
public class AddBook extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final String TAG = "INTENT_TO_SCAN_ACTIVITY";
private EditText ean;
private final int LOADER_ID = 1;
private View rootView;
private final String EAN_CONTENT="eanContent";
private static final String SCAN_FORMAT = "scanFormat";
private static final String SCAN_CONTENTS = "scanContents";
private String mScanFormat = "Format:";
private String mScanContents = "Contents:";
private Button scanBtn;
private TextView formatTxt, contentTxt;
public AddBook(){
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(ean!=null) {
outState.putString(EAN_CONTENT, ean.getText().toString());
}
}
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_add_book, container, false);
ean = (EditText) rootView.findViewById(R.id.ean);
formatTxt = (TextView)rootView.findViewById(R.id.scan_format);
contentTxt = (TextView)rootView.findViewById(R.id.scan_content);
ean.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//no need
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//no need
}
#Override
public void afterTextChanged(Editable s) {
String ean = s.toString();
//catch isbn10 numbers
if (ean.length() == 10 && !ean.startsWith("978")) {
ean = "978" + ean;
}
if (ean.length() < 13) {
clearFields();
return;
}
//Once we have an ISBN, start a book intent
Intent bookIntent = new Intent(getActivity(), BookService.class);
bookIntent.putExtra(BookService.EAN, ean);
bookIntent.setAction(BookService.FETCH_BOOK);
getActivity().startService(bookIntent);
AddBook.this.restartLoader();
}
});
rootView.findViewById(R.id.scan_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
scanIntegrator.setBeepEnabled(true);
scanIntegrator.setBarcodeImageEnabled(true);
scanIntegrator.setPrompt("Scan a barcode");
scanIntegrator.initiateScan();
}
});
rootView.findViewById(R.id.save_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ean.setText("");
}
});
rootView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent bookIntent = new Intent(getActivity(), BookService.class);
bookIntent.putExtra(BookService.EAN, ean.getText().toString());
bookIntent.setAction(BookService.DELETE_BOOK);
getActivity().startService(bookIntent);
ean.setText("");
}
});
if(savedInstanceState!=null){
ean.setText(savedInstanceState.getString(EAN_CONTENT));
ean.setHint("");
}
return rootView;
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
Log.i("FORMAT ", scanFormat);
Log.i("CONTENT", scanContent);
Log.i("xZing", "contents: "+scanContent+" format: "+scanFormat);
Toast.makeText(getActivity(), "FORMAT " + scanFormat + " CONTENT " + scanContent, Toast.LENGTH_LONG).show();
}else{
Toast toast = Toast.makeText(getActivity(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
// Checks for network connection
public boolean checkNetworkConnection(){
ConnectivityManager cm =
(ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
return isConnected;
}
private void restartLoader(){
getLoaderManager().restartLoader(LOADER_ID, null, this);
}
#Override
public android.support.v4.content.Loader<Cursor> onCreateLoader(int id, Bundle args) {
if(ean.getText().length()==0){
return null;
}
String eanStr= ean.getText().toString();
if(eanStr.length()==10 && !eanStr.startsWith("978")){
eanStr="978"+eanStr;
}
return new CursorLoader(
getActivity(),
AlexandriaContract.BookEntry.buildFullBookUri(Long.parseLong(eanStr)),
null,
null,
null,
null
);
}
#Override
public void onLoadFinished(android.support.v4.content.Loader<Cursor> loader, Cursor data) {
if (!data.moveToFirst()) {
return;
}
String bookTitle = data.getString(data.getColumnIndex(AlexandriaContract.BookEntry.TITLE));
((TextView) rootView.findViewById(R.id.bookTitle)).setText(bookTitle);
String bookSubTitle = data.getString(data.getColumnIndex(AlexandriaContract.BookEntry.SUBTITLE));
((TextView) rootView.findViewById(R.id.bookSubTitle)).setText(bookSubTitle);
String authors = data.getString(data.getColumnIndex(AlexandriaContract.AuthorEntry.AUTHOR));
String[] authorsArr = authors.split(",");
((TextView) rootView.findViewById(R.id.authors)).setLines(authorsArr.length);
((TextView) rootView.findViewById(R.id.authors)).setText(authors.replace(",","\n"));
String imgUrl = data.getString(data.getColumnIndex(AlexandriaContract.BookEntry.IMAGE_URL));
if(Patterns.WEB_URL.matcher(imgUrl).matches()){
new DownloadImage((ImageView) rootView.findViewById(R.id.bookCover)).execute(imgUrl);
rootView.findViewById(R.id.bookCover).setVisibility(View.VISIBLE);
}
String categories = data.getString(data.getColumnIndex(AlexandriaContract.CategoryEntry.CATEGORY));
((TextView) rootView.findViewById(R.id.categories)).setText(categories);
rootView.findViewById(R.id.save_button).setVisibility(View.VISIBLE);
rootView.findViewById(R.id.delete_button).setVisibility(View.VISIBLE);
}
#Override
public void onLoaderReset(android.support.v4.content.Loader<Cursor> loader) {
}
private void clearFields(){
((TextView) rootView.findViewById(R.id.bookTitle)).setText("");
((TextView) rootView.findViewById(R.id.bookSubTitle)).setText("");
((TextView) rootView.findViewById(R.id.authors)).setText("");
((TextView) rootView.findViewById(R.id.categories)).setText("");
rootView.findViewById(R.id.bookCover).setVisibility(View.INVISIBLE);
rootView.findViewById(R.id.save_button).setVisibility(View.INVISIBLE);
rootView.findViewById(R.id.delete_button).setVisibility(View.INVISIBLE);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
activity.setTitle(R.string.scan);
}
}
enter code here
The problem is that I was using IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity()), which effected on onActivityResult being called at the Activity/Parent level. And since onActivityResult doesn't do anything, nothing happened. And since that I am using fragments, the child/fragment's onActivityResult wasn't being called.
To fix it, I replace IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity()) in onClick to IntentIntegrator.forSupportFragment(AddBook.this), with "AddBook.this" in reference to the fragment so onActivityResult in the fragment is called, as opposed to one in Activity. .
:)
Cheers!

Categories

Resources