I just started learning developing android app using Android Studio, and I made a sample app that will store a value from two ExitText on one activity (firstActivity) and load these two values on the other activity (secondActivity) when I click the 'load' button on the secondActivity. However I couldn't load them on the secondActivity right from the firstActivity. Can someone help me with this.
Thanks in advance.
Edit:
Here is the scenario.
I have three activities say ActivityOne, ActivityTwo and ActivityThree
On ActivityOne I have EditText1, EditText2 and Button1
When I click Button1 data I entered into EditText1 and EditText2 should be saved. (Sharedpreferences)
On ActivityTwo I have another button called ButtonShow.
When I click ButtonShow it should open ActivityThree with the values I stored previously from ActivityOne (EditText1 & EditText2).
Thank you very much for your help.
If you want to pass values between activities you can use SharedPreferences
For example:
Class A
String username2= "AAAA";
//to store the value use
SharedPreferences userDetails = A.this.getSharedPreferences("userdetails", MODE_PRIVATE);
Editor edit = userDetails.edit();
edit.clear();
edit.putString("username", username2);
//if you need to store more values you can add here
edit.commit();
Class B
//to get the value just do this
SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);
String USERNAME = userDetails.getString("username", "");
//if you need to get more value do it here
//now you have your value username2 in USERNAME, now you can use it everywhere
You should use the Bundle feature.
You can put variables into this bundle, then attach it to an activity, and once that activity is started, you can get the variables you put into it, out of it again and then use them.
Here's an example (this code is called in FirstActivity, probably when clicking on a button):
Intent i = new Intent(getActivity(), SecondActivity.class);
Bundle variablesBundle = new Bundle();
Bundle.putString("EditText1Data", string1);
Bundle.putInt("EditText2Data", int1);
i.putExtras(variablesBundle);
startActivity(i);
And then this code is called in SecondActivity (perhaps in onCreate(), or wherever you want it)
Bundle bundle = getIntent().getExtras();
String myString = bundle.getString("EditText1Data");
That way you'll be able to pass data from 1 activity to another.
Hope the example helped clear it up a bit :)
Related
I want to send a string variable that is entered into an EditText on ActivityOne to pass the data and be displayed as a TextView on ActivityThree. However, I am having problems as the only solutions to do this that I can find cause the activity to switch to ActivityThree while doing this. I want to avoid this or maybe even send the data to ActivityThree and switch to ActivityTwo all on the click of a button. Any help or redirection to a current solution would be greatly appreciated.
If you want to send data from ActivityOne to ActivityThree by avoiding ActivityTwo then save that data in a static variable then use that variable in ActivityThree to set TextView data.
You can simply do this using SharedPreferences.
Setting EditText values in Preference from ActivityOne:
// EditText
EditText editText = (EditText) findViewById(R.id.editText);
SharedPreferences.Editor editor = getSharedPreferences("Your_Preference_Name", MODE_PRIVATE).edit();
editor.putString("KEY_VALUE", editText.getText().toString());
editor.commit();
In ActivityThree, retrieve value from Preference:
SharedPreferences prefs = getSharedPreferences("Your_Preference_Name", MODE_PRIVATE);
String editTextValue = prefs.getString("KEY_VALUE", null);
// TextView
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(editTextValue);
Hope this will help~
Please use a global variable in Application class and set it's value in ActivityOne and read the same value from ActivityThree.Global variables are available to entire project Activities.
The best way is using input extra. Do this in Activity one
Intent i = new Intent(ActivityOne.this, ActivityThree.class);
i.putExtra("label", "label_text");
startActivity(i);
Then receive the string in Activity three as such:
EditText input = //intialize it in OnCreate
Intent intent = getIntent();
String data = intent.getExtras().getString("label");
input.setText(data);
There are so many ways to do this.. But it heavily depends on what you plan to do with data and your situation..
If you want to display the data in activity three than you can make data persist and later show it when activity three is created or resumed, now:
1- If you want to display the data in activity three and you want the value to persist only in current session you can use a Global Variable or even Static one, if you define the desired value as a static variable inside your activity three than you can easily access it and use it without any need for the activity to be even created:
public ActivityThree extends Activity {
public static String myValue;
2- If you want to display the data in activity three and you want data to persist even if the app is closed you can use SharedPreferences as described here:
https://developer.android.com/training/basics/data-storage/shared-preferences.html
3- If you want to run a background task in activity three on value getting determined you can use LocalBroadcastManager:
https://www.intertech.com/Blog/using-localbroadcastmanager-in-service-to-activity-communications/
I am using Retrofit2.
I am accepting an Input date from user in first activity and when user presses OK button on the first activity, then displaying a list from which he chooses what he wants to do on the second activity. I need to pass this date to the third activity which will be shown when user chooses an item from the list.
I used Intent for connecting the first activity to the second activity containing the list.
How to send data from first to third activity. Is it possible to use multiple intents on same activity ??
There are many ways.
Using intent extra - from activity A pass data to activity B and same data you can pass to activity C
Using shared preference - save data to preference and get it from anywhere
Using Database - save data to database and use it anywhere
Using public static object - init static object and use it anywhere in project
You can do it in various ways.
Pass the data from 1st Activity to 2nd Activity. Then from 2nd to 3rd Activity.
You can use SharedPreference to store the value in 1st activity and fetch the value in 3rd Activity.
Here's an example for SharedPreference :
SharedPreferences pref;
// Editor for Shared preferences
SharedPreferences.Editor editor;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "app_data_preference";
pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
editor.putString(DATE, "Your Date"); //DATE is a string
editor.apply(); //Done. Your date has been saved
//Now to retrieve the data back, use this:
return pref.getString(DATE, "");
I'm new to Android and I want to send strings present in editText on the first activity to the 3rd activity's textview, where my second activity should not be affected.
In your first activity
SharedPreferences preferences = getSharedPreferences("preference_name",
Context.MODE_PRIVATE);
preferences.edit().putString("key", "your value").commit();
And in third activity
SharedPreferences preferences = getSharedPreferences("preference_name",
Context.MODE_PRIVATE);
String value = preferences.getString("key", "");
Encode all EditText values to json, then pass json to second activity and third activity or save it to SharePreference and read in third activity
I have this settings section where I allow users to change the languages displayed within the app. When the user chooses a different language, the activity is reloaded so that the change of language can be applied. But the problem is, when the user clicks back right after changing the language, the language shown in the background activity is still the same.
So my question is, what should I do to apply the change of language when I get back to some activity on the background? I suppose I should do something to detect the change in onResume method, but I'm not sure what it is. If you have any suggestions, please let me know.
Thank you.
After several attempts, I have found the solution to my problem. On my onCreate method, I get the SharedPreferences that contains the value of current language, and get the current language:
SharedPrefrences languagepref = getSharedPreferences("language",MODE_PRIVATE);
String language = languagepref.getString("languageToLoad", Locale.getDefault().getDisplayLanguage());
Then, in my onResume method, I assign the value of the above mentioned variable language to a local variable, and update the value of language. Then I compare these two variables - if they are different, I will destroy the current activity and start another:
#Override
public void onResume(){
super.onResume();
String oldLanguage = language;
language = languagepref.getString("languageToLoad", Locale.getDefault().getDisplayLanguage());
if (!oldLanguage.equals(language)){
finish();
startActivity(getIntent());
}
}
And voila, that did the trick!
I would suggest using SharedPreferences. You can store a lang key with the associated value in there and update it when necessary. In your onResume() methods you can get the lang value and then populate views according the value stored.
SharedPreferences sharedPreferences;
sharedPreferences = this.getSharedPreferences("MyActivity", Activity.MODE_PRIVATE);
String lang = sharedPreferences.getString("lang", "en-GB");
SharedPreferences.Editor editor;
editor = sharedPreferences.edit();
editor.putString("lang", "en-US").commit();
That's the basics you need to get going.
Have you tried restarting the Activity after the change is done ?
You can Simply use
finish();
startActivity(getIntent());
to refresh an activity whenever it detects a preference change.
The built in back pressed function does not refresh the code,so do this after u you change the language.
#Override
public void onBackPressed()
{
//new MainActivity();
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
and in Main Activity class do this
public MainActivity() {
//1- This code is responsible for updating the change of all Strings from a language to another
//it will be called every time this activity is instantiated (da,since it is a constructor) , or when this activity gets
// called by an intent.
//2- Every String inside this Activity(ever fragment inside it ) will also be under the effect of change of language
LocalUtils.updateConfig(this);
}
Please review the following answer and add this to it. To get a full answer written by Roberto.B and LarsH:
Changing Locale within the app itself
okay i am making a Quote Application and was wondering how can i have an intent that can bookmark a specific activity?
So what i want is when the user clicks a button it bookmarks the activity into another activity that holds the bookmarks/favortites.
Can someone explain this to me?
Or a simple tutorial?
Here is the code i have:
i want to know how i can add this code to program a button to create a new button in another activity?
:
`Button btn=new Button(this);
btn.setId(btn);
btn.setBackgroundResource(R.drawable.image);
btn.setMinimumHeight(150);
btn.setMinimumWidth(150);
Relativelayout.addView(btn); `
Thanks, any help is highly appreciated,
Im just a noob wanting to learn.;)
It needs some logic, jsut make when the Bookmarking button is clicked, create an intent inside the Bookmarks activity which refers to bookmarked activity.
Example:
Activity 1 has a button called Bookmark. when I click the button, in Activity 2 which is the bookmarks a new button is created that refers to Activity 1.
I think you're making the situation too complicated. It sounds like you want to simply create and store a list of classes, then access that list from another activity.
First, when the user clicks the button to record a bookmark, I would recommend storing the name of the class in SharedPreferences. SharedPreferences allows you to store name-value pairs into a file to be accessed at a later time from any activity.
SharedPreferences sp = this.getSharedPreferences("file_name", MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("class_name", "your.class.path.TestClassActivity");
editor.commit();
Later on, you can access the all the saved class names. See here for a way to get all the keys in the SharedPreferences file.
Finally, once you have all the class names, you can use them to build your intents.
String myClass = "TestClassActivity";
Class<?> cl = null;
try {
cl = Class.forName(myClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Intent myIntent = new Intent(getApplicationContext(), cl);
startActivity(myIntent);
EDIT:
I've created an example project where the above is used. It can be downloaded from www.sourceforge.net/projects/androidbookmark/
Not sure I got your problem, but it looks like you got wrong app design. I suspect you display many quote (citation of someone?) in single activity. So all you need to store is id of that quote to display and then, when user'd hit the button, fire your QuoteActivity and pass stored quote in bundle.