Dynamically create pages/activity in Android? - android

Okay, so i just created an activity that queries post titles from a database and post it on a view using an adapter.
I would like to implement the following:
Once I click the title, I should be taken to a separate page/activity that displays the content section of that title. Similar to a blog app that goes to a blog post when we click on the title.
I'm going through a block right now. How does one get this done?

In Intent you can use add extra e.g id from each:
First: and in activity you can change it Dynamically setContentView(xxx);
xxx: change depend on extra you add previously
Second: you can create standard layout and depend on extra you can fill
e.g id==1 ===> fill textview to "zzzz"
id==2 ===> fill textview to "mmmm"
... and so on

Well actually you're not dynamically creating an activity. The content of your second (detail) activity just depends on the user's input on the first activity.
As soon as the user click's on a title on your first activity, you create a new intent and append the post's to the intent's extras.
When you startup your second activity using this intent, you will be able to get the post's id from the extras and load the content from your database.
FirstActivity
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("POST_ID", <post_id>);
startActivity(i);
SecondActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Bundle bundle = getIntent().getExtras();
String postId;
if(bundle != null) {
// process your id.
postId = bundle.getString("POST_ID");
}
}

Related

Spinner choice to be used across multiple activities

Having tried several of the methods suggested by other posts on this site I can only assume I'm thick, so if someone can have a glance over my code I would be grateful.
I have the selected item in a spinner in one activity and stored that item as a string, then passed the string to the next activity and displayed that in a
Text View in that activity. That works a treat.
What I would like to do now is pass that string on to a third activity and display it in another Text View in the third activity.
To achieve this here is the code from my first activity after a button click.
Intent recordissues2 = new Intent(RecordIssue.this, RecordIssue2.class);
Bundle sitename = new Bundle();
sitename.putString("txt1a",spinnerbuilding.getSelectedItem().toString());
recordissues2.putExtras(sitename);
startActivity(recordissues2);
And for the second activity in onCreate
Bundle sitename = this.getIntent().getExtras();
String txt1a = sitename.getString("txt1a");
((TextView) findViewById(Sitelbl2)).setText(txt1a);
if you can help me with some example code to achieve the next step and where it should go and preferably exactly how it is working that would be perfect.
Thank you in advance.
Rick
you can use shared preferences to store data and use them throughout the application in any activity class:
SharedPreferences myprefs= this.getSharedPreferences("shared_key", MODE_WORLD_READABLE);
myprefs.edit().putString("spinner_value", value).commit();
You can retrieve this info across your app like this:
SharedPreferences myprefs= getSharedPreferences("shared_key", MODE_WORLD_READABLE);
String spinner_value= myprefs.getString("spinner_value", null);
In your RecordIssue2 activity, you have already stored your text as txt1a. Using the same concept, add it to your Intent's bundle when navigating to the third activity.
Intent recordissues3 = new Intent(RecordIssue2.this, RecordIssue3.class);
Bundle sitename = new Bundle();
sitename.putString("txt1a",txt1a);
recordissues3.putExtras(sitename);
startActivity(recordissues3);

How would I display edittext field dynamically

I have a string array and I have to display the string array in a edittext box showing one value at a time.
For Instance,
String str = new String{"abc", "def", "ghi"};
I have to show these values in a edittext box such that the value keep changes periodically as soon as a user opens this activity.
Please help me out, thanks in advance.
You can put this string array into parent activity of this activity. And wen you are starting intent of this activity(which contains edittext), put extra values i,e in your case array[i] and then start the activity.
starting intent from parent activity :
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
i.putExtra("STRING_you_NEED", string[i]);
and then get it on your activity that contains editText !
Bundle extras = getIntent().getExtras();
edittext.setText(extras.getString("STRING_you_NEED"))
There are lots of ways to store an index to the string array and retrieve it whenever you want. Depending on the use case, you can try the following,
Use SharedPreferences and use a key-value pair to store the index and retrieve it when you want to show the string in the Activity(Persistent)
Extend your Application(or create a static class) and use a static int variable to store the index(Non-Persistent)

How can I access a string defined in a .java file in the corresponding .xml file?

in myapp.java, I define a string based off user input from another activity. How can I access that string in activity_myapp.xml and show it in a TextView? I've tried android:text=myapp.stringname, but it doesn't seem to work.
Edit: The problem is not that I'm unable to get the text into the .java file, but I'm unable to get it into the corresponding .xml file.
well you can do this in java. as you have got the value from previous activity just put it in the current activity TextView.
TextView example = (TextView)findViewById(R.id.yourtextview);
example.setText("yourstring");
The way I suggest you do is put that text as an extra in the intent. In the other activity find the text view by id that you want to populate the data with. Then set the text based on that Extra into from the Intent.
String val = editText.getText();
Intent i = new Intent(this, OtherActivity.class);
i.putExtra("some_string",val);
startActivity(i);
And in the other Activity, onResume you can do the following
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("some_string");
}
newEditText.setText(value);
Hope this helps.

Text Field and List Views

Can I have a text field in an Activity other than your main Activity. I have a list that on click uses an activity which is the same for everyone. However, I want the text fields to change based on which thing you click on the list. For example, if you click 'gym' in the list, I want the name text field to say 'Gym'. How do you do this? Are variables in Main Activity public to other activities?
Thanks!
Intents can be used to send data from one activity to another as shown below
Intent intent = new Intent(getApplicationContext(), Activity2.class);
intent.putExtra("MY_VAL","value");
startActivity(intent);
and it can be retrived at Activity2.class as shown below (write this code in oncreate on your second activity).
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("MY_VAL");
}

ListView with a dynamic intents that change a TextView and WebView on each call

Activity that is sending the putExtra()
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent();
// Set up different intents based on the item clicked:
switch (position)
{
case ACTIVITY_0:
intent.putExtra("Value1", "Display this text!");
intent.setClass(this, com.a.someclass.class);
}
startActivityForResult(intent, position);
}
Activity receiving the putExtra()
#Override
protected void onCreate(Bundle bundle) {
// TODO Auto-generated method stub
super.onCreate(bundle);
setContentView(R.layout.information);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String value1 = extras.getString("Value1");
if (value1 != null) {
informationTitle = (TextView)findViewById(R.id.informationTitle);
informationText = (WebView)findViewById(R.id.informationText);
informationTitle.setText(value1);
}
Original Message:
i have been searching everywhere for a good tutorial on this, i have posted my code online so people can look at it but have not found the help i needed.
I am new to this and what i am basically trying to do is just have a list of items which are all linked to one class that has a dynamic TextView that will be used for the title and a WebView for content, And so basically when a item is clicked on the list it will open up the new activity/intent and it will also pass arguments to change the TextView and WebView accordingly.
I know how to open a new activity by making a new class for each item on the list but i am pretty sure there is an easier way where i can reuse one class and just keep changing the TextView and WebView. The reason i say this is because i have 15 items on my list, but that will expand overtime so i dont want to be making 50-60 different classes to open up each new item.
If some could point me to the right tutorial or give me some insight on here i will really really appreciate it!
Thank you
To accomplish that, you would, instead of using a different Intent for each list item, call the same Activity with the same Intent, but pass extras along with it.
Let's say, for instance, that you want to pass a different String depending on which list item is clicked. You would want to
myIntent.putExtra(String key, String value);
startActivity(myIntent);
The Activity that you start with this Intent will then be able to grab these extras in its onCreate() using
Bundle extras = getIntent().getExtras();
and access the extras you put in the Intent using the methods outlined here. This way you can use a single Activity for all list items but have the Activity display different information based on the extra values.
I'll give you a link where you can get the best tutorial for that...
http://www.vogella.de/articles/Android/article.html

Categories

Resources