Okay, so I'm working on an my first android app. Forgive me if this sounds naive as I am a bit new at this but I'm using the following code to read from 6 different .txt files into an
private void displayFiles(int path) {
TextView text = (TextView) findViewById(R.id.text);
ImageView display = (ImageView) findViewById(R.id.head_image);
String line = "";
StringBuilder finalString = new StringBuilder();
InputStream iStream = null;
if (path == 0) {
iStream = getResources().openRawResource(R.raw.salm);
display.setImageResource(R.drawable.salm);
} else if (path == 1) {
iStream = getResources().openRawResource(R.raw.lyme);
display.setImageResource(R.drawable.lyme);
} else if (path == 2) {
iStream = getResources().openRawResource(R.raw.pox);
display.setImageResource(R.drawable.pox);
} else if (path == 3) {
iStream = getResources().openRawResource(R.raw.shig);
display.setImageResource(R.drawable.shig);
} else if (path == 4) {
iStream = getResources().openRawResource(R.raw.gia);
display.setImageResource(R.drawable.gia);
} else if (path == 5) {
iStream = getResources().openRawResource(R.raw.intro);
display.setImageResource(R.drawable.intro);
} else {
Toast.makeText(this, "Something went wrong! Please refresh EpiQuick.", Toast.LENGTH_SHORT).show();
return;
}
BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));
try {
while ((line = bReader.readLine()) != null) {
finalString.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
text.setText(finalString);
}
The corresponding xml:
<TextView
android:id="@+id/text"
style="@style/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/head_image"
android:layout_marginBottom="16dp"
/>
<style name="body">
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#000000</item>
</style>
And a sample of the text is as follows:
Those at Risk: Travelers to countries where giardiasis is common People in childcare settings Those who are in close contact with someone who has the disease People who swallow contaminated drinking water Backpackers or campers who drink untreated water from lakes or rivers People who have contact with animals who have the disease Men who have sex with men
However when I run the app after calling all of the new line characters disappear. I was wondeirng if there is a way to fix this? If so how?