Formating a textfile to display in an EditText on android
I got my little html fisher working and it grabs a text file form a URL string. I can call setText to my EditText view and it will indeed display the text in the text file, but there is like no formatting at all (I am talking simple stuff like carriage returns and line feeds). How can I get this to render a bit more nicely in the EditText? Original text file looks like:
Imagine the following from a resource:
http://www.someaddress.com/thetextfile.txt
Original Text File here.
1. First thing on the text file.
2. Second thing on the text file...
Finally the end of the text file. This is a long string blah blah
that I like to use here. Notice the carriage return line breaks and indentation
on the paragraph.
I can get the above as a string but there is no carriage returns at all when it displays in the EditView. Is there anyway I can add this? Its not a matter of adding \n or \r cause those would already be 开发者_JAVA技巧in the text file I would suspect (was written in Notepad++). So is there anyway to get this even ever so slightly more formatted? (and preserve the formatting when the string is saved back out to disk or to a database?
EDIT:
<EditText android:id="@+id/contract_text_input"
android:layout_width="650sp" android:layout_height="wrap_content"
android:lines="25"
android:scrollbars = "vertical"
android:gravity="top|left" android:inputType="textMultiLine"
android:scrollHorizontally="false"
android:minWidth="10.0dip"
android:maxWidth="5.0dip"/>
EDIT:
These are the methods that fetch this text file from the internet. They seem to be ripping out all the \n and \r. But if I inspect that file that's on line it only has \t's in it. So maybe its filezilla's uploading to my webserver of the file?
public static InputStream getInputStreamFromUrl(String url){
InputStream contentStream = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
contentStream = response.getEntity().getContent();
} catch(Exception e){
e.printStackTrace();
}
return contentStream;
}
public static String getStringFromUrl(String url) {
BufferedReader br = new BufferedReader(new InputStreamReader(getInputStreamFromUrl(url)));
StringBuffer sb = new StringBuffer();
try{
String line = null;
while ((line = br.readLine())!=null){
sb.append(line);
}
}catch (IOException e){
e.printStackTrace();
}
return sb.toString();
}
This is how I am ultimately updating the EditText:
private class FragmentHttpHelper extends AsyncTask<Void, Void, String>{
protected void onPostExecute(String result) {
contractTextTxt.setText(result);
}
@Override
protected String doInBackground(Void... params) {
return getStringFromUrl(urlReferenceTxt.getText().toString());
}
}
Check the Android API. The problem is in the following line of code:
while ((line = br.readLine())!=null)
{
//code goes here
}
The call to readLine() removes newlines, carriage returns and linefeeds. You need to use read() if you want to get everything.
Hi sorry I just did a similar thing in an app of mine
and called
((EditText) findViewById(R.id.textView1))
.setText("Original Text File here.\r\n\r\n1. First thing on the text file.\r\n\r\n2. Second thing on the text file...\r\n\r\n Finally the end of the text file. This is a long string blah blah that I like to use here. Notice the carriage return line breaks and indentation on the paragraph.");
which would be the same as your string
and I get
which looks fine to me!
my edit text was
<EditText android:id="@+id/textView1" android:layout_width="wrap_content"
android:layout_weight="0" android:layout_height="wrap_content"></EditText>
edit sorry huge image
edit2: check the debugger's string value
精彩评论