So, I’m making my first Android application, which basically contains ~250 .txt files in the asset folder. I list them all in a ListActivity, so you can choose the one you want to read and then open it in a TextView. The app is working just fine in the emulator, no problemo. However, when I install the .apk on my phone, the longest .txt files get cut off, mid-sentence. This does not happen on the emulator. I’m at a loss, I have no clue what could be causing this, but if anyone has any ideas, I would be happy to explore them!
Here’s the code, I’m still very much learning, and I realize this probably isn’t the optimal way to do what I want to do, but anyway, here goes:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String path = fikBog[position];
try {
bog = getAssets().open("skyrimbooks/" + path);
int size = bog.available();
byte[] buffer = new byte[size];
bog.read(buffer);
bog.close();
String bookInput = new String(buffer);
Bundle taske = new Bundle();
taske.putString("bog", bookInput);
Intent g = new Intent(GotSkyrimBooks.this, BookReader.class);
g.putExtras(taske);
startActivity(g);
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
}
So, in the ListActivity, the user can browse all the different .txt files, and when he/she picks one, it gets opened in the BookReader class. And in the BookReader class, the textview gets populated like so:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bookreader);
reader = (TextView) findViewById(R.id.reader);
Bundle fikTaske = getIntent().getExtras();
fikBog = fikTaske.getString("bog");
reader.setText(fikBog);
}
Here is my .xml. Simple as can be:
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/reader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#000000" />
</LinearLayout>
</ScrollView>
Some handsets out there seem to set a low maximum default length for TextViews. I would suggest setting the maxLength property in the XML for the TextView to something larger than the files you’d like to view.
If you want to do it programmatically, this answer should help you with that:
How to programmatically set maxLength in Android TextView?