In every new version of my app I like to put a What's New section which is basically a long(-ish) text that tends to have a lot of apostrophes and percentage signs and newlines and what have you.
Is there some easier way of loading it in a textview other than escaping all the problematic characters and replacing newlines with \n or <br />?
Why don't you store the text as a Raw text file in resource folder and read it
InputStream inputStream = getResources().openRawResource(R.raw.share);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder text = new StringBuilder();
try {
while (( line = reader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
e.printStackTrace();
}
And then set to TextView
textView.setText(text.toString());
Related
I have downloaded a txt file from Firebase, and I have the path and everything, and I'm trying to convert this file into a String, so I can set all the content of the file to another method
i have tried this
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(myTxtFile));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
Log.e("OUTPUTSTRING",""+br);
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
but in my log I cant see the content of the file, instead, it returns this
java.io.BufferedReader#5eb1add
That's because you're logging br which is an object of BufferedReader. You probably want to build the string using text.toString() and log it.
I have a working filereader for a text file in my Raw Dir. The user should see the text file in the same format as I have formatted it in word but when the application is played, the text file is tightly grouped together with no paragraphs.
This is the file reader method below, as I said it does work but I just want the format to be as I have made it.
Your advice and guidance will be greatly appricated
#Override
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_support);
InputStreamReader isr = new InputStreamReader(this.getResources().openRawResource(R.raw.supporttext));
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String s;
try{
while ((s = br.readLine()) != null){
sb.append(s);
}
}catch (IOException e) {
e.printStackTrace();
}
TextView tv = (TextView)findViewById(R.id.supporttxt);
tv.setText(sb.toString());
From BufferedReader#readline java doc :
A String containing the contents of the line, not including any line-termination characters
So using readline strips the line-termination characters, that's why you do not see them in your Text View. You can add them back like this :
try{
while ((s = br.readLine()) != null){
sb.append(s);
sb.append("\n");
}
}catch (IOException e) {
e.printStackTrace();
}
Also, you should probably use getText instead of openRawResource
I am working on an android app in which I have a text file with a list of text and duration of display in seconds.
The app displays the text listed in for the mentioned duration.
It keeps looping non-stop.
Now my question is: how can I retrieve the text in the text file and display it into my TextView?
final 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) {
}
Is there a simplest way to download small text string from URL like this one:"http://app.georeach.com/ios/version.txt"
In iOS its pretty simple. But for android em not finding something good. what is the method for getting text like that from the above URL??
I used this code in onCreate of hello app,n app crashed:
try {
// Create a URL for the desired page
URL url = new URL("http://app.georeach.com/ios/version.txt");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
StringBuilder sb = new StringBuilder(100);
while ((str = in.readLine()) != null) {
sb.append(str);
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
tv.setText(sb.toString());
} catch (MalformedURLException e) {
tv.setText("mal");
} catch (IOException e) {
tv.setText("io");
}
You have to create a new class extended from AsyncTask. You can't do network stuff in the main thread. It could work but you may not want to do that. Take a look at this link : http://developer.android.com/reference/android/os/AsyncTask.html
Also don't forget to add Internet permissions to your AndroidManifest.xml.
Try this:
URL url = new URL("http://bla-bla...");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
// your text is here
String text = sb.toString()
Do not forget to catch and handle IOException and close all streams.
An "easier" way would be this:
String url2txt = null;
try {
// Being address an URL instance
url2txt = new Scanner(address.openStream(), "UTF-8").useDelimiter("\\A").next();
} catch (IOException e) { ... }
The thing is what you consider "easier". As far as code goes, probably this is the shortest way, but it depends on what you want to do afterwards with the obtained text.
I have my textfile stored in assets folder and my requirement is to show the contents in textview pointwise.I am able to access the contents if there is no space in text file which is stored in assets folder.If I puts the space in the text file then i am not able to get the contents after space.How to achieve this means to show the contents pointwise.
for example my textfile is as follows
a)Americab)Africac)India
I want output as
a) America
b) Africa
c) India
Here is my code to access the text file from assest folder which I am getting.
InputStream in = this.getAssets().open("detailtext.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
line = reader.readLine();
Since you need to read multiple lines, you need to loop through, till you reach the EOF. Try something like this:-
StringBuffer sb = new StringBuffer(0);
InputStream in = this.getAssets().open("detailtext.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = reader.readLine()) != null){
sb.append(line);
sb.append("\n");
}
String wholeText = sb.toString();
You're only reading one line, you need to use a while loop and continue to read each line until the end of the file
InputStream in = this.getAssets().open("detailtext.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while((line = reader.readLine()) != null){
// do whatever with line
}