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(....)
: LinkYou will need to add this permission to your manifest:
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:
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 typesta
. You probably do want to suggesttake
- frequency helps you there.Autocorrection:
The flags indicate appropriateness:
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. InsteadLatinIME
has its own implementation -SuggestionStripView
- which is similar. The default behavior ofInputMethodService#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 aList<String>
containing "has", "was", "assist", "ask", "asked", "asking", "assume"), create & addTextViews
holding these strings to theHorizontalScrollView(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, overrideInputMetodService#onCreateCandidatesView()
, inflate your custom layout, then return it. Hold a reference to it, so you can update it when required. To controlCandidatesView's
visibility, use the methodsetCandidatesViewShown(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.