-1

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

boomboxboy
  • 2,384
  • 5
  • 21
  • 33

1 Answers1

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