1

I have made a txt file with several lines. I want to display each line of the txt file in different textView. Only thing I have managed to do, is displaying the first line of the txt file. Maybe there is any other method how to display text from sd-card files?

My code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getTxtLine(2, R.id.textView1, txt);

 public void getTxtLine(int textLine, int resId, File txtFile){
          try {

             FileInputStream fIn = new FileInputStream(txtFile);
             BufferedReader myReader = new BufferedReader(
                     new InputStreamReader(fIn));
             String aDataRow = "";
             String aBuffer = "";

             while ((aDataRow = myReader.readLine()) != null) {
                 aBuffer += aDataRow;

                 // byte buffer into a string
                 text = new String(aBuffer);
                 TextView text1 = (TextView)findViewById(resId);
                 text1.setText(text);

             }

         } catch (Exception e) {
         }
user2532781
  • 47
  • 1
  • 5
  • Don't know how to read file from SD-CARD? Here is an example:
    http://stackoverflow.com/questions/2902689/how-can-i-read-a-text-file-from-the-sd-card-in-android
    – TeeTracker Aug 13 '13 at 15:52

2 Answers2

0

//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');
    }
}
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);
TeeTracker
  • 7,064
  • 8
  • 40
  • 46
  • Thanks for a quick reply. I managed to display the FIRST LINE of the txt file. But I want to, for example, display the first line in textview1, the second line in textview2 and so on.. So I need to extract the lines of txt file somehow.. or something like that. – user2532781 Aug 13 '13 at 16:50
0

I think you should define a ViewGroup i.e LinearLayout, RelativeLayout in your xml, and new TextView in codes. Call addView of ViewGroup. Based on your codes:

ViewGroup mGroup;   
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mGroup = (ViewGroup)findViewById(R.id.group);
    getTxtLine(2, R.id.textView1, txt);
}

public void getTxtLine(int textLine, int resId, File txtFile){
          try {

             FileInputStream fIn = new FileInputStream(txtFile);
             BufferedReader myReader = new BufferedReader(
                     new InputStreamReader(fIn));
             String aDataRow = "";
             String aBuffer = "";

             while ((aDataRow = myReader.readLine()) != null) {
                 aBuffer += aDataRow;

                 // byte buffer into a string
                 text = new String(aBuffer);
                 TextView tv = new TextView(this);
                 tv.setText(text);
                 mGroup.addView(tv);
             }

         } catch (Exception e) {
         }
}
TeeTracker
  • 7,064
  • 8
  • 40
  • 46
  • Thanks, but I can´t use it, because I have to use one specific layout file (animated gif-s, youtube video, and this text). I just managed to display the whole text into one text view, but is there any possibilties to broke the text into pieces, for example every time it finds new line ("/n") or "String = My text is here and here and here also" and it would display the text after "="? – user2532781 Aug 14 '13 at 18:07
  • You might use RegExp or Splite functions to reach your requirement. – TeeTracker Aug 14 '13 at 19:40
  • Thank You! RegExp is probably what I was looking for. Any example how to extract the text between the beginning and ending of a line? Or between some special character? EDIT: How can I access the new string to put it into a specific textview? PS, sorry for being newbie and asking dumb questions. – user2532781 Aug 15 '13 at 12:48
  • Thanks TeeTracker, I managed to do what I needed thanks to your hint to Splite and RegEx. :) – user2532781 Aug 15 '13 at 17:27