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");
Related
So basically I'm trying to pass an EditText String to another fragment which is in the same Activity. Then pass that data to another Activity, depending on which button the pressed. My problem is the application works fine but I just don't see the string being created. I've tried to use a textview just to check if it works when I pass it through the first data, but nothing shows.
I just want to pass the string to another fragment then depending on the button they press pass it on the whichever Activity.
This is my code
Passing my Data to the next Fragment and going to the fragment.
mNextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ItemStorageFragment.class);
intent.putExtra("itemName", addName.getText().toString());
((AddInventoryActivity)getActivity()).ToExpiration(null);
}
});
Grabbing the data from my First Fragment
Intent intent = getActivity().getIntent();
value = intent.getStringExtra("itemName");
Then Sending it to the Activity
mToFreezer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent in = new Intent(getActivity(), FreezerActivity.class);
in.putExtra("itemNameToFreezer", value);
startActivity(in);
}
});
Then Adding it to my RecyclerView
Intent i = getIntent();
String string = i.getStringExtra("itemNameToFreezer");
mDataset.add(string);
mAdapter.notifyDataSetChanged();
Intent extras are fine to share data between Activities, but you cannot use them for Fragments directly, as you did in your code. I assume, you are a beginner, so I recommend you read in depth about Intents here: Android - Intents and Filters
There are several ways of passing data between Activities and Fragments, most common of which is passing arguments. Yet, in your situation I'd recommend using SharedPreferences. You can store String data at any point inside your Fragment or Activity and then easily take it out with these simple steps:
Input data into SharedPreferences:
SharedPreferences.Editor editor = getSharedPreferences("YourPrefsFile", MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.apply();
Get data from SharedPreferences:
SharedPreferences prefs = getSharedPreferences("YourPrefsFile", MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "");
int idName = prefs.getInt("idName", 0);
}
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", "");
I am novice programming on Android. I have one question, I have 2 activities. Firs I pass a parameter from the Activity A to the B like this:
Intent intent = new Intent(getBaseContext(), ActivityB.class);
intent.putExtra("ALMACEN_ANTES", almacen.getText().toString());
startActivity(intent);
And now in the activity B y get the extras. Later since the Activity B, I pass parameters to the A too.
My question is, is it possible to pass parameters to another activity without doing
Intent intent = new Intent(getBaseContext(), ActivityB.class) because I donĀ“t want to open 2 times the same activity.
Thank you!
Yes . You can save the value using SharedPreferences
It will be stored in an internal xml file and you can save it and retrive whenever you want. It need not start the activity when value passing.
An example shows below
SharedPreferences sharedpreferences;
Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.commit;
you can retrive this value from the activity where you need to access these values. It can be done by:
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String name=sharedpreferences.getString("Name","");
String ph=sharedpreferences.getString("Phone","");
You want to update a value in the activity A , you mean? because if you don't open it why sending a value to it ?
you can share data between two activities by other ways. In your case the best one would be a Singleton class:
you create a class which is inheriting from Application and containing your data, and from every activity you can update you data or get it...
Your singleton class:
import android.app.Application;
public class MyApplication extends Application {
private String data;
public String getData() {return data;}
public void setData(String data) {this.data = data;}
}
From activity B , you can set your data:
MyApplication app = (MyApplication) getApplicationContext();
app.setData(someData);
And teh Ativity A can have them like this:
MyApplication app = (MyApplication) getApplicationContext();
String data = app.getData();
See this answer
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. (:
I developed one app in that I want to send URI from Class1 editText to another class containing editText.
Can anyone tell me how to do that?
SharedPreferences are the wrong way to do that. Use the Bundle feature every Intent can have: http://developer.android.com/reference/android/content/Intent.html
On the second activity you can call getExtra() and there you go...
Assuming you want to use the SharedPreferences to transfer the URI, you could try this:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putString("my-uri", "http://google.com/").commit();
And to retrieve the URI:
prefs.getString("my-uri", "default URI");
If your two classes are Activities, and if one of them starts the other one, you should probably pass the URI as an intent extra.
Also, read the FAQ and accept some answers!
you can use System.setProperty/get Property as well.
don't you like to add putExtra in intent
like this
Intent i = new Intent(getApplicationContext(), Audit_FSD_Tab.class);
i.putExtra("UsrID", UsrID);
i.putExtra("Store", Store);
i.putExtra("location", location);
startActivityForResult(i, 0);
now in other activity access these extra
Bundle UsrVal = null;
UsrVal = this.getIntent().getExtras();
UsrID = UsrVal.getString("UserId");
Store = UsrVal.getString("Store");
location = UsrVal.getString("location");
Try to store Uri in the edit text inside shared preferences in first activity and then on create method of second activity retrieve Uri value from the shared preferences and display that in edit text.simple...
It can be possible by using Shared Preferences, for example
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
Editor editor = pref.edit();
data=pref.getString("key_name5", null);
editText.setText(data);
You can follow tutorial here
http://firstcode.info/android-sharedpreferences-basics/