Save EditText and TextView data and display it in another activity - android

I'm developing an app which adds two numbers. The user provides the numbers via EditText and the result is displayed via TextView. What I want to do is to save the values of the numbers entered by the user and the result via button (to see them whenever the user wants) and display them in the layout of another activity (whithout EditText's). Remark that the user would be able to see the results saved whenever he/she wants.
Hope you can help me. Thank you so much.

There is multiple ways to achieve that :
Method 1:
Use static class setter and getter method:
create static class and set values from first activity and get value from second activity
Method 2:
Post your values through the intent
Method 3:
Use database to store data from one activity and get data from other activity
Method 4:
Use Shared preference

From Your question what i understand::
Your first editText1 has value1
Your second editText2 has value2
textView1 has the result of value1 and value2
Solution:: Get the values from the views and use intents to pass the data between activities
Code::
In your current Activity, create a new Intent: - OnCreate()
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("editText1",editText1.getText());
i.putExtra("editText2",editText2.getText());
i.putExtra("textView1",textView1.getText());
startActivity(i);
Then in the new Activity, retrieve those values: - OnCreate()
Bundle extras = getIntent().getExtras();
if (extras != null) {
String editText1= extras.getString("editText1");
String editText2= extras.getString("editText2");
String textView1= extras.getString("textView1");
}
Use the variables editText1,editText2 and textView1 in second activity to set the values to any views as u wish
HOPE THAT HELPS, let me know if you face any problems in debugging

make use of SharedPreferences like this:
SharedPreferences sp= getSharedPreferences("ttt", 0);
SharedPreferences.Editor editor = sp.edit();
String etValue=(EditText)findViewById(R.id.yourEditTextId).getText().toString();
String tvValue=(TextView)findViewById(R.id.yourTextView).getText().toString();
editor.putString("etValue", etValue);
editor.putString("tvValue", tvValue);
editor.commit();
// in anywhere user wants
SharedPreferences settings = getSharedPreferences("ttt", 0);
String yourEditTextValue=settings.getString("etValie", "");
String yourTextViewValue=settings.getString("tvValie", "");

Related

How to send data from Activity to Fragment?(Android)

I want to make use of some TextViews(cityField, updatedField) I have in my activity inside my fragment.
I know it would have been easier to make use of them in the activity instead, but the problem is that I have to join in with some codes on Fragment due to getting some JSON data
Already I've gotten the id for the codes on activity
cityField = findViewById(R.id.textView4);
updatedField = findViewById(R.id.textView9);
Now I want to make use of them in my fragment
So the question is - is it possible? if it's possible, how?
Already, I checked some answers on this site - Send data from activity to fragment in Android
How to pass data from Activity to Fragment using bundle
but they directly didn't solve my problem.
You should create a SharedViewModel and let the Activity set values of variables like cityField and updateField while the Fragment Observe the changes done to them, basically the Activity will Set and Fragment will Get
for more information about ViewModels check this:
https://developer.android.com/topic/libraries/architecture/viewmodel
You can access these instances from your fragment like this:
String cityFieldFragment = (activity as YourActivity).cityField;
String updatedFieldFragment = (activity as YourActivity).updatedField;
If I understood this right, the fragment lives in your activity, so you are able to access the data from it, just make sure that the instances are public.
if the fragment is not already created you can make use of the constructor of the fragment and pass arguments to it.
If the fragment is already created, create a method inside the fragment and invoke it in the activity.
Let's say you want to pass it from an activity to another activity that contains fragments. First, you need to pass the data like so :
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
intent.putExtra("name", username);
intent.putExtra("pass", password);
startActivity(intent);
In this case, the fragment is inside the SecondActivity.java but to receive the data, we need code inside the Fragment.java instead of the SecondActivity.java.
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
getUsername = extras.getString("name");
getPassword = extras.getString("pass");
}
I haven't tried to pass the data directly to the fragment, but you can try with the same code. All you need to do is changing the intent instead of intent.setClass(FirstActivity.this, SecondActivity.class); to intent.setClass(FirstActivity.this, Fragment.class);.
Have you tried with SharedPreferences?
in you MainActivity
// create sharedPrefences file with the name "info"
SharedPreferences sp = this.getSharedPreferences("info",0);
// here the name and default value
sp.getString("name", "default value");
// create editor
SharedPreferences.Editor editor = sp.edit();
// enter a new value
editor.putString("name", "new value");
// save changes
editor.apply();
in your Fragment
SharedPreferences sp = getActivity().getSharedPreferences("info",0);
String str = sp.getString("name", "default value"); //will get you the new value you entered

Passing Value in 3 activities in Main activity

I have 3 buttons in main activity, this 3 buttons opens another activity, it is easy, medium and hard game type. i want to get the scores of each level of difficulties and store it in main activity, any example for this one? any help would greatly appreciated thank you!
The easiest way to do this would be to pass variable you want to share to the main activity in the Intent you're using to start the activity:
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra("EXTRA_SESSION_ID", variable);
startActivity(intent);
Access that intent on next activity
String variable= getIntent().getStringExtra("EXTRA_SESSION_ID");
The docs for Intents has more information (look at the section titled "Extras").
You can store variable to any variable you want in MainActivity and use it.
You can create a Util Class, in that Class create a static variable named difficulty.
Assign it according to Button pressed.
Inside OnClickListener assign it, later retrieve it.
Thanks.
Setting values in Preference:
SharedPreferences.Editor editor = getSharedPreferences("MyPref",
MODE_PRIVATE).edit();
editor.putInt("begginer", 120); // put begginer level score here
editor.putInt("medium", 37); // put the medium level score here
editor.putInt("hard", 12); // put hard level score here
editor.apply();
To Retrieve data from preference:
SharedPreferences prefs = getSharedPreferences("MyPref", MODE_PRIVATE);
int begginer_score = prefs.getInt("begginer", 0); //0 is the default value.
int medium_score = prefs.getInt("medium", 0); //0 is the default value.
int hard_score = prefs.getInt("hard", 0); //0 is the default value.

How to get results in new activity from the previous activity results in android?

I have two activities say X and y. In x there is edittext n 6 radiobutton if user clicks button the value gets retrieved from database based on input from edittext n radiobutton. the values should be displayed in next activity y. can yu pls help in giving snippet..thanks in advance
You can easily pass data from one activity to another using Bundle or Intent.
Lets look at the following example using Bundle:
//creating an intent to call the next activity
Intent i = new Intent("com.example.NextActivity");
Bundle b = new Bundle();
//This is where we put the data, you can basically pass any
//type of data, whether its string, int, array, object
//In this example we put a string
//The param would be a Key and Value, Key would be "Name"
//value would be "John"
b.putString("Name", "John");
//we put the bundle to the Intent
i.putExtra(b);
startActivity(i, 0);
In "NextActivity" you can retrieve the data using the following code:
Bundle b = getIntent().getExtra();
//you retrieve the data using the Key, which is "Name" in our case
String data = b.getString("Name");
How about using only Intent to transfer data. Lets look at the example
Intent i = new Intent("com.example.NextActivity");
int highestScore = 405;
i.putExtra("score", highestScore);
In "NextActivity" you can retrieve the data:
int highestScore = getIntent().getIntExtra("score");
Now you would ask me, whats the difference between Intent and Bundle, they seems like
they do exactly the same thing.
The answer is yes, they both do exactly the same thing. But if you want to transfer alot of data, variables, large array you would need to use Bundle, as they have more methods for transferring large amount of data.(i.e. If your only passing one or two variable then just go with Intent.
You should put the values into a bundle and pass that bundle to the intent that's starting the next activity. Sample code is in the answer to this question: Passing a Bundle on startActivity()?
Bind the data that you would like to send with the Intent you are calling to go to the next activity.
Intent i = new Intent(this, YourNextClass.class);
i.putExtra("yourKey", "yourKeyValue");
startActivity(i);
In YourNextClass activity, you can get the passed data by using
Bundle extras = getIntent().getExtras();
if (extras != null) {
String data = extras.getString("yourKey");
}

How to ensure the data remain in an activity after clicking a button to another page then return to current page?

I've an application where a user creates events. The user need to retrieve a certain name from an activity which is a ListView of names list.
I'm having an issue with making sure that a name should remain in an activity after clicking a date button which links to another activity(calendar activity), then return back to the current activity.
My codes of the 3 pages:
Create_Events.java - codes for getting a certain name from ListView activity and the btnDate onClickListener which links to the another activity(calendar activity)
Bundle bundle = getIntent().getExtras();
if(bundle != null)
{
String date = bundle.getString("date");
txtDate.setText(date);
}
Bundle b = getIntent().getExtras();
if(b != null)
{
String name = bundle.getString("name");
txtName.setText("Create an event for:" +name);
}
buttonDate = (Button) findViewById(R.id.btnDate);
buttonDate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent calIntent = new Intent(Create_Events.this, Calendar_Event.class);
startActivity(calIntent);
}
});
ContactsList.java -- the ListView of the names which is passed to the Create_Events page.
Cursor cursor = null;
cursor = (Cursor) l.getItemAtPosition(position);
Intent intent = new Intent(ContactsList.this, Create_Events.class);
intent.putExtra("name", cursor.getString(cursor.getColumnIndex(buddyDB.KEY_NAME)));
startActivity(intent);
I need help with this. Any help provided will be greatly appreciated. Thanks in advance! =)
you can get this behavior by saving you current screen state,
you can either use shared preferences or other ways (xml,data base, ..),
this way before you leave the activity (onPause) you save any information you need..
and on (onResume) if the information exists (its not the first time the activity loads),
collect the data and put it on screen..
if this is too much for you and you only need the name string to save,
try doing this :
How to declare global variables in Android?
hope it helps...
okay what i understand from your question is you want to retain your data on screen after coming back from another activity.
like Activity A--> Activity B--> Activity A
so, set in menifest file for activity A
android:launchmode="singletop"
and, when you are coming back from Activity B to Activity A
set
Intent intent=new Intent(ActivtyB.this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
you can use SharedPreferences to store the name while use bundle to store the date.
From contactLists.java add these codes
private void SavePreferences(String key, String value)
{
SharedPreferences sharedPref = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(key, value);
editor.commit();
}
private void LoadPreferences()
{
SharedPreferences sharedPref = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String name = sharedPref.getString("name", "");
}
Then set the name to the textView which will show on the listView. And then load the preference in the create_events page and the name will be shown even when you go to another activity.
Do inform me if you still have any questions. (:

getting data from first activity to fourth activity in android

i want to get text written in the EditText first activity and set that text to the another EditText which is fourth activity.
Use this code in your first activity
Intent intent = new Intent(context,Viewnotification.class);
intent.putExtra("Value1", youredittextvalue.getText().toString());
startActiviy(intent);
And in your fourth Activity
Bundle extras = getIntent().getExtras();
String value1 = extras.getString("Value1");
yourfourthactivityedittext.setText(value1);
1- use SharedPreferences
2- set in apllication class
3- pass to using intent from 1-> 2 ->3 ->4
Simple way to do is,
You can assign one static variable which is public inside first activity like,
public static String myEditTextContent;
Set this after you set the value from your edit text like,
myEditTextContent = editText.getText().toString();
Use the same in fourth activity like
FirstActivityClass.myEditTextContent and set it in this(fourth) activity.
Later on you can use intent's putExtra,SQLLite Database,Shared Preference also, as suggested by others
You can do it in two ways
First Use SharedPreferences like
// declare
SharedPreferences pref;
SharedPreferences.Editor edit;
in On create
//initialize
pref = this.getSharedPreferences("myPrefs", MODE_PRIVATE);
edit = pref.edit();
// add data in it
edit.putString("USERNAME", EditText1.getText().toString());
edit.putString("PASSWORD", EditText1.getText().toString());
edit.putString("USERID", Text1.getText().toString());
// save data in it
edit.commit();
to get data
// access it
String passwrd = pref.getString("PASSWORD", "");
String userid = pref.getString("USERID", "");
And the second way
Send data from 1 to 2 and 2 to 3 and 3 to 4 activity
with intents like
Intent i = new Intent(First.this, Secondclass.class);
i.putExtra("userid", EditText1.getText().toString());
i.putExtra("username",EditText2.getText().toString());
startActivity(i);
and recieve in each activity like
Intent i = getIntent();
String ursid = i.getStringExtra("userid");
String ursername = i.getStringExtra("username");

Categories

Resources