I have written a program to take text entered into an editText and pass it to another system via a USB/serial link. I need to be able to enter fully arbitrary text and have it transmitted "as-is" without modification.
However, when I enter a string such as 4 2 . or SWAP OVER . . the space between the final character and the first . is removed — what is returned in these cases is 4 2. and SWAP OVER. .
Understandably, the target device doesn't recognise these and rejects the results.
I am entering the text into a textView cmdLine and returning it to my program using
cmdString = (SpannableStringBuilder) cmdLine.getText();
Is there a "feature" of getText() that tries to clean up the text entry, and, if so, how can I disable it?
Related
I have a string and I want to make the value clickable but want to avoid using XML so I want to be able to do it programmatically in the View Holder.
Given: This is a string and I want to to call this number 777-888-9999 there might be stuff here
Output: Same string output but with the phone number clickable and underlined
Currently I am trying using Linkify but am having troubble with having this value to output the same as before. I was able to have it work but for some reason on some devices it doesn't work (it worked well using Linkify.ALL but this was depreciated)
in android stuido I would like to code an activity, where the user can input numbers. For, example he types the number to the textfield, click 'OK' button, then the textfield gets clear, he types the second number, than the third, and after they give the third number the program goes to another activity and sayst thanks for the free number. I would like to save the three numbers for further use and have them in an ascending order. How should I do it?
I agree with Andrii, this is a very vague and general question. To get you pointed in the right direction though, you would want a layout with an number based-editText widget (for the user input). I would then add the button, and then implement OnClickListener for the button so that everytime the button is pressed, it calls a method you will define that will store the value in an array or list (which can be sorted), along with some kind of tracker to keep track of how many numbers have been entered - then clearing the editText field so that another number can be input; on the third number, the method calls the activity via intent or some other way saying "thanks for the free number".
This is all general and it is going to take quite a bit of work and API Guide/DeveloperDocs searching on the Android web site.
I'm trying to use an IR remote to pass certain key codes to Android. So far, I'm able to pass numeric keys (0-9) and D-pad keys (up, down, left, right, enter). Now I'm trying to extend the keys to include other characters like a-z.
The code that I'm modifying is an IR driver in the Linux kernel part of Android. It's similar to this driver. However, when I pass a value like KEY_A (maps "a" to 30: defined in Linux's include/linux/input.h), Android doesn't see it.
The section of code that passes the command up is the following:
input_report_key(cir->input, cir->last_key, 1);
input_report_key(cir->input, cir->last_key, 0);
input_sync(cir->input);
When I print cir->last_key, I can see the value 30 when I press the "a" button. However, I'm not sure how to trace the code from here to Android to see where the button press is being dropped.
In Android, I have a file called /system/usr/keylayout/qwerty.kl that maps values, e.g. 30 maps to "a". The problem is Android never gets the value of 30 when I press "a".
The keybit field of this structure has to be set to include all the key codes being passed.
For example,
set_bit(KEY_A, input_dev->keybit);
I am writing a dictionary-type app. I have a list of hash-mapped terms and definitions. The basic premise is that there is a list of words that you tap on to see the definitions.
I have this functionality up and running - I am now trying to put dynamic links between the definitions.
Example: say the user taps on an item in the list, "dog". The definition might pop up, saying "A small furry [animal], commonly kept as a pet. See also [cat].". The intention is that the user can click on the word [animal] or [cat] and go to the appropriate definition. I've already gone to the trouble of making sure that any links in definitions are bounded by square brackets, so it's just a case of scanning the pop-up string for text [surrounded by brackets] and providing a link to that definition.
Note that definitions can contain multiple links, whilst some don't contain any links.
I have access to the string before it is displayed, so I guess the best way to do this is to do the scanning and ready the links before the dialog box is displayed.
The question is, how would I go about scanning for text surrounded by square brackets, and returning the text contained within those brackets?
Ideally the actual dialog box that is displayed would be devoid of the square brackets, and I need to also figure out a way of putting hyperlinks into a dialog box's text, but I'll cross that bridge when I come to it.
I'm new to Java - I've come from MATLAB and am just about staying afloat, but this is a less common task than I've had to deal with so far!
You could probably do this with a regular expression; something like this:
([^[]*)(\[[^]]+\])
which describes two "match groups"; the first of which means any string of zero or more characters that aren't "[" and the second of which means any string starting with "[", containing one or more characters that aren't "]", and ending with "]".
Then you could scan through your input for matches to this pattern. The first match group is passed through unchanged, and the second match group gets converted to a link. When the pattern stops matching your input, take whatever's left over and transmit that unchanged as well.
You'll have to experiment a little; regular expressions typically take some debugging. If your link text can only contain alphanumerics and spaces, your pattern would look more like this:
([^[]*)(\[[\s\w]+\])
Also, you may find that regular expression matching under Android is too slow to be practical, in which case you'll have to use wasyl's suggestion.
Quite simple, I think... As the text is in brackets, you need to scan every letter. So the basic recipe would be :
in a while loop scan every character (let's say, while i < len(text))
If scanned character is [:
i++;
Add letter at index i to some temporary variable
while (character # i) != ']' append it to the temporary variable
store this temporary variable in a list of results.
Some tips:
If you use solution above, use StringBuilder to append text (as regular string is immutable)
You might also want (and it's better, I think) to store starting and ending positions of all square brackets first, and then use string.substring() on each pair to get the text inside. This way you'd first iterate definition to find brackets (maybe catch unmatched ones, for early error handling), then iterate pairs of indices...
As for links, maybe this will be of use: How can I get clickable hyperlinks in AlertDialog from a string resource?
I want to make a smart keyboard that can learn and save new words from user. I already made note and keyboard separately, the problem is :
how to read all keystrokes and write it to my note in background?
how to save my note automatically?
thanks for your help
Keep a String or StringBuilder that stores all the text that the user types. All text sent through your soft keyboard will have to pass through the onKey method.
So, I'd do something like this:
1) In onKey, check to make sure primaryCode (the keycode that was pressed) is a letter/number/apostrophe using the corresponding functions. So, something like
Character.isDefined(primaryCode)
2) Concatenate primaryCode onto the end of your StringBuilder/String.
You'll also have to deal with the user moving the cursor/backspacing. In my keyboard, I only store the most recent two words (resetting this whenever the user moves the cursor). That way, the keyboard can learn what the most likely word is given the last word.
You can save your "note" using an ObjectOutputStream or (if it's fairly small) using sharedPreferences.
Send me an email if you run into an more issues: I've been writing a soft keyboard for a while so I'm pretty familiar with it.