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 wish to display in an activity that is opened when clicking a particular button. so the same activity is opened, but the text filling it will depend on the button pressed.
I have everything except the ability to display the text, currently stored in /raw as a txt.
I’m looking at input readers and txt vs xml (former seems easier), and I’m still struggling.
Even if I manage to get it to display a particular txt file, I don’t know how to get it to work dependant on which button is pressed.
The content holding activity has a textview which I planned to fill with text from the .txt file.
Any help would be appreciated.
I get a syntax error on that catch. spent the last 15 minutes trying to figure it out
EDIT
activity with buttons in it,
public class MartialTab extends Activity {
Button btn1, btn2;
String choice;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the martial arts tab");
setContentView(textview);
setContentView(R.layout.martial_tab);//takes layout from martial_tab.xml
btn1 = (Button) findViewById(R.id.kickboxingButton);//instantiates a button called btn1 one from the xml
btn1.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
kickBoxingBut(v, choice);
}//calls the method
});//end of method
btn2 = (Button) findViewById(R.id.grecoroman);//instantiates a button called btn1 one from the xml
btn2.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
grecoromanBut(v);
}//calls the method
});//end of method
}
// private void kickBoxingBut() {//button1 method
//new AlertDialog.Builder(this).setTitle("AlertNotification").setMessage(
// "This is an Alert Dialogue Toast").setNeutralButton( "Click Me!", null).show();
//creates an alert notification with the above text
//}
public String kickBoxingBut(View view, String s) {
startActivity(new Intent(this, ContentHolder.class));
this.choice = "kick";
return choice;
}
public void grecoromanBut(View view) {
startActivity(new Intent(this, ContentHolder.class));
}
}
activity that holds the info
public class ContentHolder extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context m_Context = getApplicationContext();
TextView textview = new TextView(this);
textview.setText("This is the content view");
setContentView(textview);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value = extras.getString("choice");
}
InputStream input;
try {
input = m_Context.getAssets().open("choice");
DataInputStream in = new DataInputStream(input);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read File Line By Line
while ((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);
}
This setContentView is a problem. As above doesn’t work as it expects a return type. changing it to ContentHolder doesn’t work either.
You can open an asset txt file and read line by line like this:
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
http://developer.android.com/resources/tutorials/hello-world.html
EDIT2