How to recall the username previously entered in another activity - android

I am passing a value for username in one activity through XML. I want to recall the value in the next screen, like, 'Hello/Welcome, '. How is it possible using GetExtra and PutExtra?
Pls. find the below code, which I have tried for the same.
For storing the value of username in one activity:
Intent myActivity2 = new Intent(this,Activity1.class);
myActivity2.putExtra("nickname", R.id.mynickname3);
startActivity(myActivity2);
For recalling the value in other activity:
Bundle extras = getIntent().getExtras();
String val = extras.getString("nickname");
TextView tv=new TextView(this);
tv.setText(val);

If you want to reuse a username, use sharedPreference.
It is not a good idea to pass a userName to another activity then use that.Field like userName may need to use again and again.

In the first activity, you are passing in an integer value (R.id.mynickname3). Instead you need to give it a String. Use this instead:
myActivity2.putExtra("nickname", getString(R.id.mynickname3));
As a side note, you are trying to get a Bundle unnecessarily. Try this:
String val = getIntent().getStringExtra("nickname");
TextView tv = new TextView(this);
if (val != null)
{
tv.setText(val);
}

You're nearly right.
It is not enough to just pass the id of the EditText. You have to use below code -
((EditText)findViewById(R.id.mynickname3)).getText().toString();
int putExtra. Everything else should be fine.
UPDATE: Don't forget to set your TextView as contentView in OnCreate via:
setContentView(..)

Just a silly mistake in your code:
myActivity2.putExtra("nickname", R.id.mynickname3);
Where you have written ID of the EditText (nickname), instead it should be as a:
txtNickName = (EditText) findViewById (R.id.mynickname3);
myActivity2.putExtra("nickname", txtNickName.getText().toString());

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 to replace the edit text value in android?

I have stored value in First screen and pass that value and set the value in Edit Text of Second screen. While i am back into first screen and change the value and pass again to second screen and set that value in edit text. Here, it shows previous value only doesn't replace that value.
EditText mobilenumber = (EditText) rootView.findViewById(R.id.mobilenumber);
mobilenumber.setText(mobile);
Above mobile is a variable and I stored the value from first screen and pass it to second screen to set that value in mobilenumber edit text.
1)if you are using activity(first screen and second screen). you can send value using intent.
intent.putExtra("mobile",mobile);
and access it in second screen using
getIntent().getExtras().getString("mobile","defaultvalue");
OR
2) Make mobile as global and static variable in first activity
and in second activity(screen)
in onResume()
setText(firstactivity.mobile);
As you said both the screen are fragment you can share a global variable in activity(if both fragment part of same activity) or
can communicate via passing value in bundle would be safest and
reliable way
Using the Bundle
From your first activity :
public void onCreate(Bundle b){
super.onCreate(b);
String number = "" + R.id.IDInYourFirstActivity;
b.putString("mobilenumber", number);
}
in your next activity
public void onCreate(Bundle bd){
super.onCreate(bd);
String mobileNo = bd.getString("mobilenumber");
EditText mobilenumber = (EditText) rootView.findViewById(R.id.mobilenumber);
mobilenumber.setText(mobileNo);
}
You can use a static variable in second screen. Then in first screen when you change the value then
Second.mobile=your value.
It will change the value accordingly.

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.

findViewById() works in one activity but not in the other

I am trying to pass EditText from Activity1 to Activity2.
Activity1 code:
public void openNextActivity()
{
Intent intent = new Intent("com.abc.xyz.ImageActivity");
EditText myEditText = (EditText)findViewById(R.id.myEditText);
int myEditTextId = myEditText.getId();
//For Test purpose ----- starts
// **Point1: next line of code works fine in this Activity1**
EditText myEditTextTest = (EditText)findViewById(myEditTextId);
//For Test purpose ----- ends
intent.putExtra("myEditText", myEditTextId);
startActivity(intent);
}
Activity2 code:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.comments_detail);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
int myEditTextId = extras.getInt("myEditText");
// Point2: next line of code displays the correct Id
Log.d("tag","myEditTextId"+ myEditTextId);
// Point 3: Next line of code not works in this Activity2
EditText myEditText = (EditText)findViewById(myEditTextId);
if(myEditText != null)
{
Log.d("tag","Not null");
}
else
{
Log.d("tag","null");// **Point4: this condition executes**
}
}
}
The problem is that the line : EditText myEditText = (EditText)findViewById(myEditTextId); works fine in Activity1 but its not working in Activity2.
EDIT:
Note: Both activities are using different layouts
Thanks for your valuable time & help.
The only views you have accessible to you are those in the layout you loaded at the start of Activity 2, i.e those in R.layout.comments_detail. I'm guessing that Activity 1 loads a different layout with its setContentView(..) and it's in that layout where 'myEditText is defined and in scope.
You can't pass a view as an extra. You can pass the string in the view (if that's your purpose).
It seems like you're trying to get the EditText id before it's been assigned:
int myEditTextId = myEditText.getId();
EditText myEditText = (EditText)findViewById(myEditTextId); **// Point1: Works fine**
Try this instead:
EditText myEditText = (EditText)findViewById(myEditTextId); **// Point1: Works fine**
int myEditTextId = myEditText.getId();
Edit
Does the EditText in question even exist in the set layout? (R.layout.comments_detail)
This can not be done.
If your attempt is to manipulate activity 1 from activity 2 then you should be returning something to activity 1 from activity 2. By passing the ID of view you made in activity 1 to activity 2 shouldn't resolve to anything due to nothing being created in activity 2. findViewById is invoked on the current activity. As you didn't set anything in the view there is nothing for it to find.
I think it can work if you use the same layout R.layout.comments_detail in Activity1 and Activity2 because findViewById() return the unique id and this id only belong layout comments_detail

Categories

Resources