0

I'm trying to learn how to create apps and got stuck at the very beginning. I read lots of topics about this issue and still couldn't manage to find out how to do this. I think I got the reading from text file part right but don't know how to display it. Can someone please help me? Thanks.

MainActivity.java

public class MainActivity extends AppCompatActivity {

public void main(String[] args) throws Exception
{
    FileReader file = new FileReader("C:/Users/Eothen/AndroidStudioProjects/FutbolBorsasi/app/src/main/res/infobank.txt");
    BufferedReader reader = new BufferedReader(file);
    String line = reader.readLine();
    while (line != null)
    {
        line = reader.readLine();
        System.out.println("line");
    }
    reader.close();
}

activity_main.xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/nameTextView"
    android:id="@+id/nameTextView"
    android:layout_below="@+id/ppImageView"
    android:layout_centerHorizontal="true"
    android:textSize="25sp"
    android:layout_marginTop="25dp"
    android:background="@drawable/backgroundtextview"
    android:gravity="center_vertical|center_horizontal" />

strings.xml

 <string name="nameTextView">DISPLAY HERE</string>

infobank.txt

READ HERE

Edit: This Worked

public class MainActivity extends AppCompatActivity {
String line;
TextView view;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AssetManager am = getAssets();
    try {
        InputStream is = am.open("test.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        line = reader.readLine();
        view = (TextView) findViewById(R.id.nameTxt);
        view.setText(line);
    } catch (Exception e) {
        e.printStackTrace();
    }


}

2 Answers2

0
line = reader.readLine();
System.out.println("line");

You should display it in TextView, not console.

Like:

TextView view = (TextView) findViewById(R.id.counter);
view.setText(line);

And System.out.println("line"); will display "line", not text of that file, you must remove " ".

EDIT: dont forget to change R.id.counter to R.id.<id of your textview>

Edit2:

Try this:

public void main(String[] args) throws Exception
{
    FileReader file = new FileReader("C:/Users/Eothen/AndroidStudioProjects/FutbolBorsasi/app/src/main/res/infobank.txt");
    BufferedReader reader = new BufferedReader(file);
    String line = reader.readLine();
    while (line != null)
    {
        line = reader.readLine();
        TextView view = (TextView) findViewById(R.id.nameTextView);
        view.setText(line);
    }
    reader.close();
}

How do you want to read file which is on C drive from emulator? Try Reading a simple text file

Community
  • 1
  • 1
antoninkriz
  • 966
  • 4
  • 18
  • 36
  • http://stackoverflow.com/questions/6716748/how-to-change-a-textviews-text-by-pressing-a-button - there is more info abut it. – antoninkriz Sep 13 '15 at 08:50
  • Place your text file in the /assets directory under the Android project. Use AssetManager class to access it. `AssetManager am = context.getAssets(); InputStream is = am.open("test.txt");` Or you can also put the file in the /res/raw directory, where the file will be indexed and is accessible by an id in the R file: `InputStream is = getResources().openRawResource(R.raw.test);` Credits to @shihpeng http://stackoverflow.com/questions/5771366/reading-a-simple-text-file – antoninkriz Sep 13 '15 at 09:18
  • If you need to read file from external place (internal storage, not app) try this: http://stackoverflow.com/questions/12421814/how-can-i-read-a-text-file-in-android – antoninkriz Sep 13 '15 at 09:22
0

There are many errors here. The first one is that in android there isn't public void main(String[] args) but instead you have to declare in your manifest what is your main activity and override the onCreate, onStart, etc. methods provided by the Activity class. Second, the filesystem of an android device (or android emulator) is different from your pc filesystem, then C:/Users/Eothen/AndroidStudioProjects/FutbolBorsasi/app/src/main/res/infobank.txt is wrong.

I suggest you to start reading some tutorial first (there are many available online), and then try again.

SimoV8
  • 1,382
  • 1
  • 18
  • 32