I am new to android development. Can I add .txt file in my app and use it to show text(the text file contains a paragraph of description) in my activity instead of writing the whole description in my strings.XML file. If yes please help me with the code. Thanks
Asked
Active
Viewed 248 times
-1
-
1Try this: http://stackoverflow.com/a/8695590/3110234 – activesince93 Nov 02 '15 at 11:55
-
Possible duplicate of [How can I read .txt and display it as TextView in Android?](http://stackoverflow.com/questions/8695361/how-can-i-read-txt-and-display-it-as-textview-in-android) – Uma Kanth Nov 02 '15 at 11:56
-
@activesince93 I want to add .txt in my project like in assets or raw folder not from SD card – boomboxboy Nov 02 '15 at 11:58
-
@UmaKanth I want to add .txt in my project like in assets or raw folder not from SD card – boomboxboy Nov 02 '15 at 12:00
-
try replacing "getContext().getAssets()" with "sdcard" – activesince93 Nov 02 '15 at 12:05
1 Answers
0
There are a few decent answers to this in this question
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);
//Set the text
tv.setText(text.toString());
Unless this is something that is dependant upon reading from a possibly changing .txt file, you should definitely try to use the strings XML file.

Community
- 1
- 1

Twentyonehundred
- 2,199
- 2
- 17
- 28