0

I have a TextView that displays different text files depending on the user selection from a Spinner. How can I get the name of the text file that is being displayed? The files are in the raw folder.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
vin shaba
  • 167
  • 3
  • 12

1 Answers1

0

This is the way to obtain the file from raw after being selected in a spinner

Spinner mySpinner=(Spinner) findViewById(R.id.your_spinner);
String rawTextName = = mySpinner.getSelectedItem().toString();
InputStream ins = getResources().openRawResource(
            getResources().getIdentifier(rawTextName ,
            "raw", getPackageName()));

If you need to read the content this should work

TextView textView =(TextView ) findViewById(R.id.your_textview);
textView.setText = readTextFile(ins);

...

public String readTextFile(InputStream inputStream) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    byte buf[] = new byte[1024];
    int len;
    try {
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {

    }
    return outputStream.toString();
}

Hope this helps!!

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57