How to show different text in the same activity? eclipse - android

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");
}

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.

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

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.

How to set new window

I am thinking, how to set up the new window in Android after pressed button. I have the first window, when I have a few buttons. After pressed one of these buttons (for example on the button for adding new name) I want to show new window, when will be only Edit text for new name...
Any ideas, how is possible to do or where to search inspiration?
Thanks a lot,
Manny
You need to start new activity.
Intent i = new Intent(ActualActivity.this,ActivityB.class);
String name = "example"
i.putExtra("NAME", name);
startActivityForResult(i, ID);//or startActivity,see javadoc for your preferences
With "startActivity" or "startActivityForResult" you call the new activity. For that you need to create a "Intent". If you want to send information like a name, or a number or something else you can "putExtra" information like the example.
(Ofc ActivityB must to extend Activity and must be declare in AndroirManifest)
EDIT:
You have a lot of example of how to start new activity in the SDK examples (sdk directory/samples/android X )

Sustaining EditText data through activity changes

I can't seem to use data that is in an EditText box.
I have one activity for entering a url and a submit button.
When the user clicks submit, a new activity opens as a WebView and should display the url that
the user typed in the EditText box.
This should be the simplest of tasks but I can't find how to do it anywhere...
You can write below code
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("Url", edittext.getText());
And you can fetch this string in NextActivity using
String url = this.getIntent().getExtras().getString("Url");
when you create the Intent to start the next activity, you will use Intent.putExtra(...) to bundle the url and set the action to VIEW.
see the examples here.

Categories

Resources