Thursday, July 1, 2010

Display text file in /res/raw

Android can handle text file as raw.

Display text file in /res/raw

Create a text file, named hello.txt, and save it in the folder /res/raw. The files inside /res/raw will be traded as raw files, they will not be compiled. They will be moved to application package as they are.

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/hellotxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="22px"
/>
</LinearLayout>


AndroidTxt.java
package com.exercise.AndroidTxt;

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 AndroidTxt extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView helloTxt = (TextView)findViewById(R.id.hellotxt);
helloTxt.setText(readTxt());
}

private String readTxt(){

InputStream inputStream = getResources().openRawResource(R.raw.hello);

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();
}
}


Download the files.

12 comments:

  1. Very useful tutorial indeed. I am glad that I was able to find this. I have a question though and hope you can help me with. The question is,

    How can I read a file that has different independent paragraphs and display using textview and also be able to use shownext() and showPrevious(). Or how can I read more than one file in the res/raw folder and use showNext(), showPrev() methods to display in the view.

    Thanks in advance. I am new to Java/Android and trying to learn.

    ReplyDelete
  2. Hi this is good but i'd like to display dynamic text i mean whenever the contents of the file change the textview is updated to show new contents.. how to do that please help me

    ReplyDelete
  3. Hi this is good but i'd like to display dynamic text i mean whenever the contents of the file change the textview is updated to show new contents.. how to do that please help me

    ReplyDelete
  4. Great tutorial, I would like to know how to do one of two things though?
    #1 what if I wanted to make a certain part of the text clickable how would I do that?
    or
    #2 what about adding a button that the user would click after reading the text?

    ReplyDelete
  5. hello George,

    #1: You can make two TextView, one of with is clickable.

    #2: You can make a Button.

    ReplyDelete
  6. hi friends,if any one knows how to read text from more than one file...please tell me...


    Thanking you.
    Surya Bondada

    ReplyDelete
  7. What if I have two textviews in a layout and want to populate these with two different text files? How do you code these???

    ReplyDelete
  8. Thanks for the upload, its working fine, i m very much thankfull to you.

    ReplyDelete
  9. i want to display textview with viewflipper. text is continue with next flipper page

    ReplyDelete
  10. How can I display html files stored in asset folder to the text view?
    Am new in android application development
    Please your help will push up

    ReplyDelete