Since I'm using scanner, after scanning the barcode into edittext I need to get text from the edittext automatically and check the record for that barcode from the local database.
I know that there is addTextChangedListener for getting the text from the edittext automatically. But there are issues with that like it is checking each character of barcode when scanned instead of checking the barcode string in the local database.
You may use Handler and its methods
postDelayed(Runnable r, long delayMillis) and
removeCallbacks(Runnable r).
In your TextWatcher invoke removeCallbacks(...) in order to remove any previous Runnable and invoke postDelayed(...) with a little delay and a Runnable that will check the barcode string.
So you may accomplish your search in the database in several milliseconds after the last digit was sent to your EditText.
Also your scanner may send a special symbol at the end of a barcode. So read its documentation. Probably you don't need Handler, but you need to wait for that special symbol
Related
Good Afternoon, I have a question please have look at this.
I am using Retrofit beta 2 for retrieving data from api. I have an EditText in which I want to search some names from server. I got the output also, but for example consider there are some names:
ABC,XYZ,PQR,STU,ETC. These are the names stored in the server and I am retrieving this names using Retrofit beta2.
When I searched for ABC or abc it will display the results and when I removed the string from the EditText then nothing is displayed.
Till here I Have done.
My question is when I type names fast then the result is something else.
So, can anyone tell me how to avoid this.
Thank You.
The Call object has a couple of utility methods you can use namely isExecuted and cancel to control your request.
http://square.github.io/retrofit/2.x/retrofit/retrofit2/Call.html
I am assuming you are using TextWatcher. Pseudo-code as follows:
public void afterTextChanged (Editable s) {
// Cancel the request first before sending it again; this way you won't have two separate calls
if(call != null && call.isExecuted()) {
call.cancel();
}
// reinitialize call and execute it again
}
Generally speaking, it is ill-advised to listen for immediate input from user to spawn an action: you should rethink your approach; it's better to put a "submit" or button to execute the API call; otherwise you'll be spamming the server with several HTTP requests per input.
Remember the search text e.g. "AB" on the search result. Before displaying the result, check if the currently entered text still matches the text of the search result and only use the result, if it does.
Searched for "AB", current text "ABC" -> don't show the result.
Searched for "ABC", current text "ABC" -> show the result.
Like this it won't even matter in what order the two requests return, it will always display only the correct result.
I think it is not safe to start a new thread for every character
This is How I did in my App:
You can use a Queue to store every string that user input. Every time the edittext changed, a new string will be inserted to the queue
At the same time, you can send the query async by retrofit. but if the retrofit is on its way querying, you do not send immediately, only put the string into the queue. when your callback function is called by retrofit, you can remove all strings from queues except the latest one, then use retrofit to query this newest query.
Too much thread, will affect the performance of any device, because it the overhead involve will consume lot of resource.
starting a new thread for each character is by no means advisable, to answer your question each thread should continually check for a change in characters, when you type fast, it mean tend to skip some result but will eventually display the result of the last string correctly.
I've got this mobile device, it has a barcode reader and SCAN button. So, I've got no EditTexts on my mainActivity, but I want to get an event, when barcode scanning ends, and reŃeive barcode value in event's handler.
Kinda way to do this is to place an edittext to the activity and bind a textwatcher to it, and when edittext's value changes, read it's value. But I have to do this on a simple screen without any inputs.
The SCAN button doesn't place any value, it seems to be starting another component, that reads a barcode and places value into an input field.
So, how do I get this without any inputs? Thanks in advance.
Tried having an invisible EditText field?
<EditText
...
android:visibility="invisible">
Have you used zxing library for barcode scan? If yes than you will get event in handleDecodeInternally function in CaptureActivity because that function called when barcode scanning ends. Also you can set your EditText in capture.xml and set value in handleDecodeInternally function.
I have two challenges in my android program whcih are listed below , Kindly help me.
Suppose I start a thread A in MainActivity , and based on whether a certain condition
is met in thread A , the whole application must be closed. ( as finish() is available only in Activity and not accessible from a thread )
2.In MainActivity I have an EditText. Every letter i press is setup to transfer over bluetooth Socket to reach PC over bluetooth StreamCOnnection.
Hence , I need to get every letter (single character) that I type in the EditText, stored in a char variable. (It should also take into account the backspace button which means that no letter was typed).
As and when user types a letter in EditText , a char variable (single letter) is initialized in MainActivity and a running thread "A" will fetch and send value of the char variable to socket outputstream and clear the char variable so that newly entered letter could be stored for subsequent socket streams sending.
Hence my question for the point 2 is , How to fetch the last typed character of an EditBox taking into consideration backspace key to be ignored.
I Googled lot and also referred similar posts (Link1) & Link2, but I couldn't successfully implement it.
( PS: I'm Sorry that I have written detailed, instead of pasting Snippets as my program is a bit big and could confuse what my correct question is)
1) finish() is a function on the Activity object. If the thread is a subclass of the activity, it can fall it. If the Thread has a reference to the Activity, it can call activityReference.finish(). Finish() is the only way to end an activity programmatically.
2)Use a TextWatcher and add it to the EditText. That will tell you whenever a character is typed. You can figure out what the new data is and process it.
My suggestions would be:
1) Use a Handler to make a callback to the UI thread if the condition is met. From there, you can call the finish() method.
2) I would implement on onKeyListener for the Edittext. In that listener, you can listen for any key press you want (character or other).
Hope that helps.
How can I use the edittext listener to prevent the user from entering special characters (&%+?#...etc) and also prevent the user from entering numbers, finally allow user to enter only letters (A B C ...)
Check out EditText.setFilters(). This allows you to specify one or more instances of InputFilter for an EditText. These filter certain characters from the input, as the name suggest. There are a lot of prebuild ones (see "know subclasses" at the top of the class documentation), but you may also implement your own by extending the InputFilter class.
You can use add TextWatcher (link: http://developer.android.com/reference/android/text/TextWatcher.html) to your EditText (link: http://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener%28android.text.TextWatcher%29) and implement the three provided methods in order to control the input from the user and perform any operation you want.
Is it possible to have the barcode scanner (zxing for example) called instead of the keyboard in a webview? So, just to be clear, when I tap in a text box inside my webview I'd like to override the keyboard call and have the barcode scanner open instead as an input to the text field. Is this feasible?
Yes its possible , i implemented this softkeyboard which does the job.
'Long press' shift key and you can scan a code which adds to the text field
which has focus.
http://www.androidbroadcast.com/en/softkeyboard/index.html
Barcodescanner Keyboard lets you do exactly this. It is definitely possible.
it possible go to http://www.tec-it.com/en/software/android/barcode-keyboard/barcodekeyboard-overview/Default.aspx
download barcode keyboard demo version and enjoy....
Not in any kind of standard fashion: you could plug a custom JavaScript handler that listened for focus messages from the specific text input (identified perhaps by id?) and then use the InputMethodManager to hide the input method and launch your barcode reader. When the barcode reader returns with content you could then use the same JavaScript handler to insert the result into the text input field.
This isn't exactly what you are asking for but might work: You could modify the ZXing bar code scanning library to dispatch key events for each character in the scanned bar code. I assume these key events would be interpreted by the text field. You can program the bar code scanner to append a carriage return or whatever to accept the data into the text field as if someone had hit the enter key. You could put a border around your layout and turn it red or something to indicate to the user that it will accept typed in data or a bar code scan.
It is not possible -- at least, there's nothing in Android or ZXing that lets you do this directly. But you can certainly write your own Input Method that does such a thing, if you're willing to put in the work. ZXing is open source and there have been some topics about it in the discussion group.