Reputation: 763
I have few edittexts in my application where user enters company name, client name, purpose....kinds of things. Now I want to add those words to dictionary programmatically and thus they don't have to re-enter the whole word everytime, instead dictionary should suggest the word once they start typing.
I searched on the web regarding the same and I got something like
UserDictionary.Words.addWord(getActivity(), et_client_name.getText().toString(), 1, "", locale);
And we need to give two permissions to the app:
<uses-permission android:name="android.permission.WRITE_USER_DICTIONARY"/>
<uses-permission android:name="android.permission.READ_USER_DICTIONARY"/>
But my problem is : Once I add words using the above statement; how to retrieve it back from dictionary and suggest user as soon as user starts typing in.
Any help or reference to any good tutorials is appreciated!
Upvotes: 2
Views: 3090
Reputation: 763
I managed to find solution of my question by my own....answering this question in the hope that it might help someone as a reference for the same kind of issue:
public class HomeActivity extends Fragment implements TextWatcher {
String itemClientName[] = {};
ArrayAdapter<String> clientNameAdapter;
ArrayList<String> clientNameList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView et_client_name = (AutoCompleteTextView) findViewById(R.id.et_client_name);
getFromDictionary();
suggestClientName();
et_client_name.addTextChangedListener(this);
}
public void getFromDictionary() {
System.out.println("Inside getFromDictionary");
ContentResolver resolver = getActivity().getContentResolver();
String[] projection = new String[]{BaseColumns._ID, UserDictionary.Words.WORD};
cursor = resolver.query(UserDictionary.Words.CONTENT_URI, projection, null, null, null);
if (cursor.moveToFirst()) {
do {
long id = Integer.parseInt(cursor.getString(0));
word = cursor.getString(1);
// Prepare list for autoCompletion
System.out.println("Inside prepareMyList" + word);
if (!clientNameList.contains(word))
clientNameList.add(word);
System.out.println("clientNameList::" + clientNameList);
System.out.println("Word from dictionary is::" + word);
// do something meaningful
} while (cursor.moveToNext());
}
}
public void suggestClientName() {
String newAdd1 = et_client_name.getText().toString();
if (!clientNameList.contains(newAdd1)) {
clientNameList.add(newAdd1);
// update the autocomplete words
clientNameAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_dropdown_item_1line, clientNameList);
et_client_name.setAdapter(clientNameAdapter);
}
// display the words in myList for your reference
String s = "";
for (int i = 0; i < clientNameList.size(); i++) {
s += clientNameList.get(i) + "\n";
}
}
}
Upvotes: 2