22

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.

Suggestions in keyboard

Community
  • 1
  • 1
Kunal
  • 432
  • 1
  • 4
  • 14
  • I guess you want to use an [AutoCompleteTextView](http://developer.android.com/reference/android/widget/AutoCompleteTextView.html), instead... – Phantômaxx Mar 13 '15 at 11:47
  • So i want to confirm that AutoCompleteTextView is shown below EditText in which we are entering text or it can be used above Keyboard also as "Suggestions" which are shown directly above Keyboard layout – Kunal Mar 13 '15 at 11:54
  • I don't think there's a way. I asked a similar question before and was told, since it's dependent on the individual keyboard and its implementation, there's no way to enforce this. (I wanted to hide it instead of adding suggestions but it's the same principle). And AutoCompleteTextView is like a combo box with suggestions. It doesn't add suggestions to the keyboard itself. – kha Mar 17 '15 at 11:13
  • The main thing in my app are "Suggestions". So i don't think that i can hide that. Thanx a lot for your reply. – Kunal Mar 17 '15 at 11:24
  • @Kunal have you got a proper solution? – Bhaven Shah Aug 20 '21 at 08:13

2 Answers2

37

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).

Vikram
  • 51,313
  • 11
  • 93
  • 122
  • 1
    Thank u @Vikram for your answer but the "UserDictionary" code here you suggest is to add words to dictionary but i want to retrieve words from dictionary using the initial alphabets of word. Please help to do that – Kunal Mar 18 '15 at 05:08
  • 1
    @Kunal Hi Kunal, I have added some more information. – Vikram Mar 18 '15 at 15:48
  • 1
    Thank u for your answer. But can you please help me in explaining how to use "CandidateView" to add our own layout. I used the Keyboard code that you provided in your answer but their code is very hectic. Not able to understand that. I want only little help, rest i'll do it myself. And one more thing if you are not able to answer this Comment's questions your answer is still acceptable. – Kunal Mar 20 '15 at 08:25
  • 1
    @Kunal No problem. I'll update my answer by tomorrow. Sorry about the wait. – Vikram Mar 21 '15 at 01:07
  • 1
    Thnx a lot @Vikram..:) – Kunal Mar 21 '15 at 04:55
  • 1
    @Kunal I have added some info on how to set up the CandidatesView. – Vikram Mar 22 '15 at 16:12
  • 4
    Really very informative and descriptive answer i found till now about suggestions. Too too much nice. – Smeet Sep 03 '15 at 09:05
  • is there any library for that or i have to include my own Dictionary.txt? for example i want to suggest diff language words ? – Mateen Chaudhry Feb 22 '18 at 07:00
  • can u give a complete demo project link in which the dictrionary is used i soft keybaord..thanks in advance – Mazhar Iqbal Jul 13 '21 at 05:11
1

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.

Community
  • 1
  • 1
bond
  • 11,236
  • 7
  • 48
  • 62
  • 1
    I do not want to add Spell checker, i want to add suggestions from Dictionary given in android devices. Please provide example on that. – Kunal Mar 18 '15 at 05:12