How to custom Edittext listener - android

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.

Related

Best way to detect editText inputType in Android?

I developed a simple keyboard.
It's a T9 keyboard that is conceived specifically for phones with an hardware keyboard.
When I want to write something into a standard EditText I have no problem, but when i try to write numbers inside an ediText with android:inputType="phone" or android:inputType="number" the keyboard still tries to write letters.
My question is: What is the best and simplest way to detect if the editText I have focus on is Number-only, Text-only, etc. from my custom Keyboard?
How can I detect the inputType from my Keyboard?
I hope someone of you already solved this problem in the past and can help...thank you in advance!
Try something like this:
if(txt.getInputType()==InputType.TYPE_CLASS_PHONE){
//do something
}
Also check this question: Android: Determine active input method from code it describes using the InputMethodManager
**
Another Approach:
**
when developing your custom keyboard: as described here http://developer.android.com/guide/topics/text/creating-input-method.html#IMEUI
you extends InputMethodService which provides the method onStartInputView(EditorInfo info, boolean restarting)
onStartInputView has the EditorInfo argument which has the inputType field that you can know the current edit text input type from
Under "Handling different input types" on this page:
When an input field receives focus and your IME starts, the system calls onStartInputView(), passing in an EditorInfo object that contains details about the input type and other attributes of the text field. In this object, the inputType field contains the text field's input type.
It then goes on to explain how to read the input type and what many input types (and their related flags) mean.

How to include suggestions in Android Keyboard

I am working on Android SoftKeyboard. I've created layout for keyboard but dont't know how to include suggestions which appears if we type some word in EditText.
For example if i write "Kn" then "Known" and "Known" are shown in Suggestions.
So my questions are -
1) How to include suggestions in Android Softkeyboard?
2) Is there any way to include our own list of suggestions?
Thanx a lot in advance.
I've already checked this and this but not able to find any proper answer. Any help would be appreciated.
EDIT
I want to include suggestions directly above Keyboard as shown in picture below.
You can use the static method UserDictionary.Words.addWord(....): Link
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// On JellyBean & above, you can provide a shortcut and an explicit Locale
UserDictionary.Words.addWord(this, "MadeUpWord", 10, "Mad", Locale.getDefault());
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
UserDictionary.Words.addWord(this, "MadeUpWord", 10, UserDictionary.Words.LOCALE_TYPE_CURRENT);
}
You will need to add this permission to your manifest:
<uses-permission android:name="android.permission.WRITE_USER_DICTIONARY"/>
Added words will appear in Settings > Language & input > Personal dictionary.
If you are implementing your own soft keyboard, I suggest you go through Creating an Input Method. The suggestions are usually shown in the Candidates View. By default, InputMethodService#onCreateCandidatesView() returns null. You should override this method to return your implementation of the suggestions bar.
Here's a sample project that implements the Candidates view: SoftKeyboard.
More info:
Word and phrase suggestions go in the candidates view. Info about how to create & populate it are in the sample project mentioned above.
As far as I know, the selection of what words/phrases to suggest is developer's responsibility. Android does not provide those for you. You will probably need a set of dictionaries - one for each language/locale you plan on supporting. You may also want to maintain a dictionary of user-specified words.
Android's default keyboard uses these: Link
If you download one of these, unpack it and open with a text editor:
dictionary=main:en,locale=en,description=English,date=1402373178,version=47
word=the,f=222,flags=,originalFreq=222
word=to,f=215,flags=,originalFreq=208
word=of,f=214,flags=,originalFreq=214
word=and,f=212,flags=,originalFreq=212
word=in,f=210,flags=,originalFreq=210
.... 165,635 more lines
As apparent, the frequency plays a pivotal role in determining the suitability of a word as a suggestion. You probably don't want to suggest tachometer when the user types ta. You probably do want to suggest take - frequency helps you there.
Autocorrection:
word=id,f=99,flags=,originalFreq=99
shortcut=I'd,f=whitelist
The flags indicate appropriateness:
word=goddamn,f=0,flags=offensive,originalFreq=62
Even if you decide to use these dictionaries, the code to parse them and obtain meaningful suggestions will have to come from you.
Two articles (both by Peter Kankowski) that talk about predictive text input & spelling correction:
Using DAWG for predictive text input
Using Ternary DAGs for Spelling Correction
CandidatesView:
The first thing you should know about the CandidatesView: it is optional. In fact, LatinIME (android's default soft keyboard) does not use it. Instead LatinIME has its own implementation - SuggestionStripView - which is similar. The default behavior of InputMethodService#onCreateCandidatesView() is to return null. If you choose to provide your own implementation, don't override this method.
You need to decide what your CandidatesView should look like. One possible implementation can be a HorizontalScrollView. After you evaluate your suggestions (for example, user start writing "as", and your suggestion-logic gives you a List<String> containing "has", "was", "assist", "ask", "asked", "asking", "assume"), create & add TextViews holding these strings to the HorizontalScrollView(LinearLayout). This way, user can scroll horizontally and choose the intended word by clicking on it.
It is up to you to decide whether to use the API or handle the CandidatesView yourself. If you want to use the API, override InputMetodService#onCreateCandidatesView(), inflate your custom layout, then return it. Hold a reference to it, so you can update it when required. To control CandidatesView's visibility, use the method setCandidatesViewShown(boolean).
If you are creating a custom keyboard, I suggest you go through Creating Input Method, there is a sample code that you can go over. CandidateView is probably what you are looking for. It is explained in the link above.
If you want to provide inline spell checker, you would want to check out Spellchecker framework
Hope this helps.

Problems intercepting space key in EditText

I'm attempting to make a way for a user to input text into a TextView from an EditText. However, if the user enters something, and wants to fix it, I want them to be able to press space on an empty EditText to get the last thing they wrote back. The first problem is, if they type in "hello", hit enter to add it to the TextView (which clears it from the EditText), then hit space, the EditText then has " hello". Not what I want, and I can't figure out why.
My code to place the entered text into a holding string:
b1 = ti.getText().toString();
Then, if the user hits the space key, I believe they should get b1 in the EditText. Instead, I get: " " + b1. Why is this added space in there?
if((event.getAction()==KeyEvent.ACTION_DOWN)&&(key == KeyEvent.KEYCODE_SPACE)){
if(ti.getText().toString().equals("")){
ti.setText(b1);
}
My second, bigger problem is that the above code only works on a hardware keyboard. What is the key event for a software keyboard pressing space?
This is all in an onKeyListener.
An easy way to solve the extra space problem is to just remove the space from the String before you put it into the EditText
b1 = b1.substring(1); //<--- cut out the first character, which is the " " in your case.
ti.setText(b1);
p.s. I strongly recommend more descriptive variable names. Your programs are likely to be confusing to work on if you use names like ti and b1. Perhaps these choices make more sense in the context of your program. But from what you've shown here it is not easy to tell what these names refer to.
For your first problem I suspect you need to return true from your onKey method in the onKeyListener when your 'if' condition is met, to indicate that the event is consumed, otherwise you get the default onKeyListener adding the space to the EditText. i.e.:
if((event.getAction()==KeyEvent.ACTION_DOWN)&&(key == KeyEvent.KEYCODE_SPACE)){
if(ti.getText().toString().equals("")){
ti.setText(b1);
return true;
}
}
This extract from the KeyEvent API docs should help with problem 2:
"As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method. In particular, the default software keyboard will never send any key event to any application targetting Jelly Bean or later, and will only send events for some presses of the delete and return keys to applications targetting Ice Cream Sandwich or earlier. Be aware that other software input methods may never send key events regardless of the version. Consider using editor actions like IME_ACTION_DONE if you need specific interaction with the software keyboard, as it gives more visibility to the user as to how your application will react to key presses."

Have a barcode scanner called instead of the keyboard in a webview

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.

Android: need to validate an edittext for non-blank input

If I want to enforce a maximum length of input in an EditText field, I can use the maxLength attribute. But if I want to enforce a minimum length (in my case, simply non-blank), I find no corresponding minLength attribute.
I've also looked at the various 'inputType' attributes (phone, password, etc) but I don't see anything like 'required' or 'non-blank'.
I can't believe this would require a custom input filter; it's more likely the answer is so obvious it just doesn't get written down in the form I'm asking the question.
You can simply check that yourField.text() is not equivalent to null or "" at the point of submission. If it is, prompt the user to input something.
Carefull with something though. If you have a submit button, for instance, and your user presses submit without changing the focus out of your TextEdit, your method won't be called.
Take an Activity with 2 EditTexts and 1 button named "Go":
1) User writes something in EditText1
2) User clicks on EditText2
3) Instead of writting something there just clicks go.
Your onFocusChanged() method won't be called because focus is not "lost".
If you have a validation in your Go button_click method, it will trigger, of course, but you won't achieve your goal "I was looking/hoping for was a way to give instant feedback"
You can try using TextWatcher.

Categories

Resources