Reputation: 666
I trying to load text from shared preferences to a text field - I can get the data out fine, The problem I am having is I don't know how to set the input fields to receive values dynamically. I have not code to show as I don't even know where to start with doing this. I have searched the internet and have got nowhere.
Could anyone help or guide in the right direction?
Upvotes: 1
Views: 5534
Reputation: 1
If you want to change text in TextView with click a bottun,you can write this:
public void ChangeText(){
//ChangeText is metode for onClick the button
TextView Hello=(TextView) findViewById(R.id.textView);
//textView is an id for TextView on your window
Hello.setText("Hello world!");
}
check this video for better apperception : https://www.youtube.com/watch?v=6rH1xPgD9S0
Upvotes: -1
Reputation: 549
SharedPreferences prefs = getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
String yourText = prefs.getString("your_key", "
EditText yourEditText = (EditText) findViewById(R.yourEditText.setText(yourText);
try like this.
Upvotes: -1
Reputation: 12919
Just use setText()
:
SharedPreferences prefs = getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
String yourText = prefs.getString("your_key", "Empty");
yourEditText.setText(yourText);
Upvotes: 2
Reputation: 4976
Check out the docs for the TextView (EditText extends TextView, so, the code works for both)
Your code should like like something like this
String text = "Retrieve this from shared preferences".
TextView textView = findViewById("mytext");
textView.setText(text);
You will probably use that on the onCreate of your Activity.
Upvotes: 1