Reputation: 11
i want to click on button in first activity and in second activity load text from raw resource into textview. i think its possible via putextra
, getextra
method. my text reader code to textview is this.
TextView textView = (TextView)findViewById(R.id.textview_data);
String data = readTextFile(this, R.raw.books);
textView.setText(data);
}
public static String readTextFile(Context ctx, int resId)
{
InputStream inputStream = ctx.getResources().openRawResource(resId);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(inputreader);
String line;
StringBuilder stringBuilder = new StringBuilder();
try
{
while (( line = bufferedreader.readLine()) != null)
{
stringBuilder.append(line);
stringBuilder.append('\n');
}
}
catch (IOException e)
{
return null;
}
return stringBuilder.toString();
}
any one can help me
Upvotes: 1
Views: 2396
Reputation: 399
So on button click in first activity you call an intent starting second Activity. If you want in your first activity to choose the resource id, then i advice you to use something like this
Button button = (Button) fragmentView.findViewById(R.id.button_id);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("resource_id", R.raw.books); //R.raw.books or any other resource you want to display
startActivity(intent);
}
});
Then in your second activity you get this data like this
int resourceId = getIntent().getIntExtra("resource_id");
And you use this resourceId
instead of R.raw.books
Upvotes: 1
Reputation: 22291
Please Use below code for Read file data from raw folder.
try {
Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.books);
byte[] b = new byte[in_s.available()];
in_s.read(b);
textView.setText(new String(b));
} catch (Exception e) {
// e.printStackTrace();
textView.setText("Error: can't show help.");
}
Upvotes: 2