How To Fill An Activity With Text Stored In /raw
I have an app for a project. my idea was to create a 3 tab app, in each tab there are several buttons representing a topic. each topic has a txt file containing some information I
Solution 1:
You can open an asset txt file and read line by line like this:
InputStreaminput= context.getAssets().open(fileName);
DataInputStreamin=newDataInputStream(input);
BufferedReaderbr=newBufferedReader(newInputStreamReader(in));
String strLine;
//Read File Line By Linewhile ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
} catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
This is a good start for you, display the lines you read in a TextView
. Once you do this, you can move on to next step.
EDIT
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
publicclassHelloAndroidextendsActivity {
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This is how you get the contextContextm_Context= getApplicationContext();
// This is how you show text programatically, there is also xml option, which I recommend instead.TextViewtv=newTextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
http://developer.android.com/resources/tutorials/hello-world.html
EDIT2
publicclassContentHolderextendsActivity {
String fileName;
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Contextm_Context= getApplicationContext();
TextViewtextview=newTextView(this);
textview.setText("This is the content view");
InputStream input;
try {
input = m_Context.getAssets().open(fileName);
DataInputStreamin=newDataInputStream(input);
BufferedReaderbr=newBufferedReader(newInputStreamReader(in));
String strLine;
// Read File Line By Linewhile ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println(strLine);
textview.setText(strLine);
}
// Close the input stream
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
setContentView(R.layout.main);
}
Post a Comment for "How To Fill An Activity With Text Stored In /raw"