- masuk ke folder res dan buat folder baru dengan nama raw
- kemudian masukan file txt anda di folder raw tersebut
- berikut desain layoutnya:
activity_main.xml
<RelativeLayout 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" >
<TextView
android:id="@+id/hellotxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Coba"
tools:context=".MainActivity" />
</RelativeLayout>
- sintak ActivityMain.Java
package com.example.txtread;
import android.os.Bundle;
import android.text.Html;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.view.Menu;
import android.app.ActionBar;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;
import android.widget.TextView;
public class MainActivity extends Activity {
String labels="caption";
String text="";
String[] s;
private Vector<String> wordss;
int j=0;
private StringTokenizer tokenizer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.io1));
backgroundsetTileModeX(android.graphics.Shader.TileMode.REPEAT);
actionBar.setBackgroundDrawable(background);
wordss = new Vector<String>();
TextView helloTxt = (TextView)findViewById(R.id.hellotxt);
helloTxt.setText(Html.fromHtml(readTxt()));
}
private void backgroundsetTileModeX(TileMode repeat) {
// TODO Auto-generated method stub
}
private String readTxt(){
InputStream inputStream = getResources().openRawResource(R.raw.textsaya);
// InputStream inputStream = getResources().openRawResource(R.raw.internals);
System.out.println(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
}
kemudian jalankan dan lihat hasilnya.
sekian tutorialnya semoga bermanfaat
0 comments:
Post a Comment