My app takes data from 4 EditTexts, puts them in an ArrayList and with a press of a button (using Intents) passes the data to second activity which then prints that data in 4 TextViews and has a button that returns me to first activity for next input. Is there any way of keeping all of my input data so I can show everything I've inputted in a TextView or Toast message?
Let's assume your first Activity is A and second activity is B
so in the A activity's button press you can call the 2nd activity using following code.
Intent intent = new Intent(A.this, B.class);
String input1 = txt1.getText().toString();
String input2 = txt2.getText().toString();
intent.putExtra("key1", input1 );
intent.putExtra("key2", input2 );
startActivity(intent);
From the B activity you can access these values using following code
Intent intent = getIntent();
String input1 = intent.getStringExtra("key1");
String input2 = intent.getStringExtra("key2");
yes you can. you can pass your data to second activity like this.
Intent intent1= new Intent(first.this, second.class);
Bundle bundle1=new Bundle();
bundle1.putString("data", et1.getText().toString());
intent1.putExtras(bundle1);
startActivityForResult(intent1, 0);
or you can restore them in a temporary database.
Related
I want to change a picture from another activity and display it as a picture or button, but it makes me a problem when I want to run the program, I'd love to help
imageView10 = (ImageView) findViewById(R.id.imageView10);
String s;
SharedPreferences mySharedPreferences2 = enter.this.getSharedPreferences("two", Context.MODE_PRIVATE);
s = mySharedPreferences2.getString("USERNAME2", "");
s = "R.drawable.add1";
imageView10.setBackgroundResource(s);
the s value is red...
I am passing value from Activity A to Activity B. It works fine.
For Example, if I pass String "Harvey Brown", Then in next activity it also shows "Harvey Brown".
But I would also like to add "Author: " text before "Harvey Brown" and show the sum result in TextView such that now instead of only
"Harvey Brown".
It will show
Author:Harvey Brown
for every intent.
Here is the code, I'm using to pass String value:
intent.putExtra("Title",mData.get(position).getTitle());
and in next activity I received value by
Intent intent = getIntent();
String author_name = intent.getExtras().getString("author_name");
and I set the value accordingly as
author_name = (TextView) findViewById(R.id.author_nameid);
author_name.setText(author_name);
Solution:
Just change this line:
intent.putExtra("Title",mData.get(position).getTitle());
To
intent.putExtra("Title", "Author: " + mData.get(position).getTitle());
You will get your desired result.
You can add a 'Author:' Prefix like below -
author_name = (TextView) findViewById(R.id.author_nameid);
author_name.setText("Author: " + author_name);
Im creating a simple app which is for example i got 10 table row with check box and user click on 3 box and press next, i wan to print out only the 3 row the use clicked and adding extra button/icon beside it. Is it possible? please help ! Thank you !
to pass data to another Activity you can use Intents and putting your data as extra :
here is a sample :
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("data", "My data in second activity");
startActivity(intent);
and for retrieving data in SecondActivity :
String data = getIntent().getStringExtra("data");
Yes, use putExtra and pass an ArrayList of String:
Intent intent = new Intent(Recipes2.this, XMLParser.class);
intent.putStringArrayListExtra("myList", (ArrayList<String>) myList);
startActivity(intent);
Here is the answer:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent)
For more information check this link.
I have a main activity which has two fragments and I am trying to pass some data which I want to append on above whatever texts is already on a edittext on the next fragment.
Activity with two separate Tabs:
The following works fine:
Fragment #1:
String y = "TEST 1";
SharedPreferences prefs; // shared preferences
prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("someId", y);
editor.commit();
Fragment #2:
SharedPreferences prefs; // shared preferences
prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE);
String someId=prefs.getString("someId","");
showLog.setText(someId + "\n HERE"); //this overwrites the text and is multiline
What I am looking to do is I want the showLog to append above what's already there.
My showLog is the following:
<EditText
android:id="#+id/showLog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Nothing to display"
android:inputType="textMultiLine"
android:lines="12"
android:paddingLeft="2dip"
android:singleLine="false"
android:textColor="#999999"
android:textSize="14dip"
android:textStyle="normal"
android:gravity="top" />
For example:
showLog already starts out with having "THIS IS A TEST" in the textbox
When the SharedPreference is called, the showLog should display the following:
TEST 1
HERE
THIS IS A TEST
But that's not happening. I tried using .append() which didn't have any affect.
I think I understand what you're trying to do now, you might want to try:
int start = showLog.getSelectionStart();
int end = showLog.getSelectionEnd();
String toIns = someId + "\n HERE";
showLog.getText().replace(Math.min(start, end), Math.max(start, end), toIns, 0, toIns.length());
Technically it's not appending, rather replacing the end of the string with new text.
EDIT: in light of the new issue arising, here are my edits to your project, let me know if there's anything wrong still.
My edited version is on the right, original on the left
CurrentTrip.java
DisplayTrip.java
What I'm doing with the text might not be exactly what you want, so make sure to let me know if it's not.
EDIT 2: And to delete the stored values:
final SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.commit();
EDIT 3: Having understood exactly what it is you want to do, here's one way to do it by storing the last trip added, and keeping track of the text you need in your TextView.
prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE);
// Find the string we want
String someId = prefs.getString("someId","");
final Editor editor = prefs.edit();
// To stop a trip being added in onResume etc.
if(someId != prefs.getString("previous-trip", "")){
showLog.setText(someId + prefs.getString("previous", ""));
} else {
// Without this else, we'd have a blank box again
showLog.setText(prefs.getString("previous", ""));
}
// Store the latest trip that was added
editor.putString("previous-trip", someId);
// Store everything that's in the box so far for next time
editor.putString("previous", showLog.getText().toString());
// Commit to the prefs
editor.commit();
If I understand what you are trying to get then you should be able to do
String text = showLog.getText().toString();
showLog.setText(someId + "\n HERE" + text);
If you already have text in it then you just get that text and after opening up your SharedPreference put it all in with the original text last.
Intent extras
In first Activity
String someText = someTextString;
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("key", someText); // add some extra to the Intent to pass to next Activity
startActivity(i);
In SecondActivity
Intent intent = getIntent();
String myText = intent.getStringExtra("key"); // use same key value used in first Activity
About the part where you need to display the String:
// From what you have given, the TextView `showLog` is already displaying some text
// and you want to place another another String in front of it(prepend)
showLog.setText(stringToAdd + showLog.getText());
// "stringToAdd" is the string you trying to pass between activities(or fragments)
How to display the text of a textfield in a url section in android?
Here is the code
final Text q = (Text) findViewById(R.id.q);
Intent i = new Intent("android.intent.action.VIEW", Uri.parse("http://www.google.com/search?q="));
startActivity(i);
i want to use as query this (Text) findViewById(R.id.q); . How??
What you are looking for is actually this.
final EditText q = (EditText) findViewById(R.id.q);
Intent i = new Intent("android.intent.action.VIEW",
Uri.parse("http://www.google.com/search?q=" + q.getText().toString()));
startActivity(i);
Note the use of EditText. Text is not the correct class you were looking for.
Intent i = new Intent("android.intent.action.VIEW", Uri.parse("http://www.google.com/search?q=" + q.getText()));