I am currently developing android keyboard. I want to make word dictionary for the keyboard to suggest the words with respect to the user inputs. But dictionary is not an activity so, I cannot use getAssets()
method to read text file if I put the text file in asset folder. Is there a way to read text document in Android Studio?
Asked
Active
Viewed 199 times
0

Zoe
- 27,060
- 21
- 118
- 148

chathura kapugedara
- 49
- 1
- 6
-
you want to read file in sub class of InputMethodService? – Jaykishan Sewak Mar 20 '19 at 09:54
-
I mean are you extending any service, class where you want to use getAsseste() – Jaykishan Sewak Mar 20 '19 at 09:58
-
I want to read a file within a class also it is not a sub class of any service or activity – chathura kapugedara Mar 20 '19 at 12:52
1 Answers
0
This is a list which should be stored in SharedPreferences
or Database
and should be loaded into memory(RAM) whenever the keyboard pops up. Getting from assets is an IO operation and its usages should be minimal.
If you want to read data from your .txt file,
then you can prepare a JSON
or XML
and use getAssets()
to read that JSON during initial startup of the "Application" and after that.
[UPDATE] As requested in the answer
Here's how you can read JSON data from the text file.
and how to parse JSON.

Daksh Gargas
- 3,498
- 2
- 23
- 37
-
Can you further explain the second paragraph which is in your answer ? – chathura kapugedara Mar 20 '19 at 07:37