Android, How can I load long text into an activity? - android

I have a long text that I want to show it page by page. For my first try, I created 10 activity and put some button that when user clicks On it, next activity shows.
I read somewhere that I can load all my text in one activity with using Intent. is it possible? How it is possible?
I don't want user scroll the page.
Thanks

Let's say you have your activity called MyTextActivity, then in your button click listener, you pass on the text you want into an intent to your activity:
Intent i = new Intent(this, MyTextActivity.class);
i.putExtra("myText", "this is the text you want to display in the next page");
startActivity(i);
Then in your MyTextActivity, you put the following in onCreate():
String textToDisplay = "";
Bundle extras = getIntent().getExtras();
if (extras != null){
textToDisplay = extras.getString("myText");
}
then you set your TextView text to this text.

Assuming you already have a TextView and a Button in your activity, when the Button is pressed you do not need to go to a new activity. Instead, you can change the content of your existing text view. Just use TextView.setText() to change it.

If the text is static,you can put it in a string resource and access it in all your activities. Otherwise, you can attach the string to the intent, using putExtra.

I think the answer is "don't do this". Android runs on a variety of devices, and you should handle the cases where it won't fit on the screen that matches the device you're using for testing. Consider tablets, for example.

Related

How can I get data of textview from one xml to another xml

can someone help me about this one? I'm trying to build a simple quiz app that uses button, checkbox and textview. I want the score of all questions appear in the last page (5 pages) but can't make it work. I want to get the score from the previous question and then send it to the result page.
Store them in SharedPreference and get the value in the last page and/or clear it depending on what you need to do.
You should be able to pass the score from one activity to another using bundle in your intent.
//From Activity A
//To pass data from one activity to another
Intent i=new Intent(context, MyTargetClass.class);
i.putExtra("somekey", scoreValue);
context.startActivity(i);
//To Activity B
//To retrieve data from an activity
Intent intent = getIntent();
String scoreValue = intent.getStringExtra("someKey");

Pass Value from First Activity to Third Activity via AsyncTask

I have an Online Ordering System from where users can complete the order in, at least, three steps. Each step below is a separate activity:
Make selection of a Plan
Select the Starting Date
Select Contents based on selected Plan in Step 1.
Now what I have done in step 1 is, once user clicks on a Plan and Order, below code runs onClick.
Intent intent = new Intent(context, SelectDate.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle extras = new Bundle();
extras.putString("pass_selected_meal_plan_id",mp_id_pass);
extras.putString("pass_selected_meal_name",mp_name_display);
intent.putExtras(extras);
context.startActivity(intent);
Toast.makeText(context, mp_name_display+" Selected",
Toast.LENGTH_SHORT).show();
No in the second Activity, I am asking the user to select the order starting date. That is also simple enough and have no issues in it. But the issue begins when I am trying to switch to the third activity.
Since my third activity content is depending on the Plan selection made in the first activity, therefore I have to transfer the selected ID to the third activity. I have it in Extra Bundle in second activity as "pass_selected_meal_plan_id".
The way I am loading the third activity is also crucial. There are two possible ways to start the third activity. First is very simple by putting onClick on the next button. I assume that it will be very simple to transfer the required selection as well. But in this case, as far as I know being a beginner in Android App Development, I will have to give a button to click and load content based on the selected Plan.
The second way is what I want to use. I am running an AsyncTask in the second activity onClick Next. It is working perfectly fine. The only this is I am unable to getExtras in onPostExecute from the previous Activity, i.e. first activity.
I am open to even using Global Variables, but don't really know how to do. Knowing what Global Variables are, I am not too keen to use them instead.
Here is my onPostExecute code:
#Override
protected void onPostExecute(String result) {
Intent intent = new Intent(ctx, SelectMeals.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle extras = new Bundle();
//Instead of "1" I want the value from previous Activity, which already available in Extras from previous Activity
extras.putString("pass_selected_meal_plan_id", "1");
//This is the second value I want to be transferred from first activity to the third activity
extras.putString("pass_selected_meal_name","Test");
intent.putExtras(extras);
ctx.startActivity(intent);
}
Would really appreciate if anyone can help.
You can use below code
In your second activity
// declare global variables
String id;
String name;
Use below code inside onCreate
Bundle bundle = getIntent().getExtras();
//Extract the data…
id = bundle.getString("pass_selected_meal_plan_id");
name = bundle.getString("pass_selected_meal_name");
Now in your Asynctask, write the code below before launching 3rd activity.
#Override
protected void onPostExecute(String result) {
Intent intent = new Intent(ctx, SelectMeals.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle extras = new Bundle();
extras.putString("pass_selected_meal_plan_id",id); // first activity data
extras.putString("pass_selected_meal_name",name); // first activity data
intent.putExtras(extras);
ctx.startActivity(intent);
}
Finally, you can get these values in third activity.
One important point I would like to highlight is that avoid using generic variable names, e.g. id. It is used just for reference in this answer but it is not a good idea. One obvious problem you may face doing so is with Refactor > Rename. It will change even the #+id in XML files.

Is it Possible to Pass Edittext Value from one activity to another activity without using Intent

Intent intent = new Intent(this, Page.class);
intent.putExtra("arg", getText);
startActivity(intent);
//second activity:
String passedArg = getIntent().getExtras().getString("arg");
enteredValue.setText(passedArg);
To get value from one activity to another activity without using
intents....?? I want to show edittext passed data in other activity
edittext but without using intent...?? Kindly help me is it possible
or not....??
There are many ways: event buses, SharedPreferences, through global object (application class), database, simple file. But why?
Try direct and readable style first.
if you can't bundle the values you want to save in the Intent, then you could use the SharedPreferences. More information here

How to show different text in the same activity? eclipse

I am making an app in which, dialog pops up and user selects an option. based on this option, next activity opens up in which there is some text. There are lots of options for the user to choose from in the dialog. Every option is intended to have the same layout just the text is different. Do I need to make these many new activities or can I somehow change the string based on user's choice by adding some conditionals somewhere? Please help
You need not create separate activity for each user input, as the layout is same. You can achieve this by using a TextView and changing its text according to user input like this:
TextView textView = (TextView) findViewById(R.id.<YourTextViewID>);
textView.setText("SetTextAccordingToUserInput");
One Activity is sufficient. Set the text on selection event in dialogue.
You can use only one Activity with content and create this activity with parameter depend on user selection in dialog.
To open activity with parameter:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("yourParameter", 1);
intent.putExtras(b);
startActivity(intent);
Then to use it in your SecondActivity.class onCreate method
onCreate(Bundle instance)
{
// some stuff like setting layout ant etc.
Bundle b = getIntent().getExtras();
int value = b.getInt("yourParameter");
}

android switchstatement question

I was wondering, how would I make a switch statement, that when that certain case was triggered, it would open a new screen with text. Would I use an intent? And if so, which one?
Thank you for your help in advance.
When you want to open a "new screen", you probably want to open a new activity. You would create a second Activity-derived class and use the following overload of the Intent constructor with startActivity:
Intent intent = new Intent(this, MySecondActivity.class);
startActivity(intent);
this will explicitly attempt to open a new Activity with the class name MySecondActivity
to pass a String of text from one Activity to another in this way, you can add it to the intent.
String someValue = "Some Value";
intent.putExtra("Some Key", someValue);
and in the code of your other Activity, you can get at this string via the Intent
getIntent().getStringExtra("Some Key");
Obviously you want to do null checks to make sure the key exists in the Intent, and you want to put a proper constant String somewhere instead of using a literal String for a key, but this is the basic gist.
Kriem... As Rich said you can launch a new activity using intents and push data to the new activity using extras. You can push data back to your main activity using startActivityForResult instead of startActivity. You can return to your main screen by calling finish() in the new screen. Finally, you can put the new screen event handlers into the NewScreen.java file.
The overall effect is a near full separation from dependency between the two activities, so that you might be able to easily re-use the NewScreen.java class in another project. I have some code here.

Categories

Resources