Create raw text file at /res/raw/mytext.txt.
Santa Claus Is Coming To Town: You better watch out You better not cry Better not pout I'm telling you why Santa Claus is coming to town He's making a list, And checking it twice; Gonna find out Who's naughty and nice. Santa Claus is coming to town He sees you when you're sleeping He knows when you're awake He knows if you've been bad or good So be good for goodness sake! O! You better watch out! You better not cry. Better not pout, I'm telling you why. Santa Claus is coming to town. Santa Claus is coming to town.
Add a ScrollView with TextView inside, to our layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/mytextview" android:layout_width="match_parent" android:layout_height="wrap_content"/> </ScrollView> </LinearLayout>
Main code:
package com.AndroidTextResource; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView myTextView = (TextView)findViewById(R.id.mytextview); InputStream inputStream = getResources().openRawResource(R.raw.mytext); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); String myText = ""; int in; try { in = inputStream.read(); while (in != -1) { byteArrayOutputStream.write(in); in = inputStream.read(); } inputStream.close(); myText = byteArrayOutputStream.toString(); }catch (IOException e) { e.printStackTrace(); } myTextView.setText(myText); } }
No comments:
Post a Comment