user2029541
user2029541

Reputation: 666

Add text dynamically to an input field in Android Studio

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

Answers (4)

bloOodyrain
bloOodyrain

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

ealvess
ealvess

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

FD_
FD_

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

andreban
andreban

Reputation: 4976

Check out the docs for the TextView (EditText extends TextView, so, the code works for both)

http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence, android.widget.TextView.BufferType)

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

Related Questions