-2

I want to change the hint property of an edittext field from my activity layout in a function I created in mainActivity.kt. The previous value of hint was from string.xml in resources -> values -> string.xml file.

How do I access string in main and change its value to something else?

3
  • 2
    AFAIK you cant change the value in strings.xml Commented Mar 17, 2018 at 7:03
  • 1
    you can get the value as getString(R.string.yourString); Commented Mar 17, 2018 at 7:03
  • try myEditText.setHint("NEW HINT"); Commented Mar 17, 2018 at 7:06

3 Answers 3

0
editText.setHint(getString(R.string.simple_text));
Sign up to request clarification or add additional context in comments.

3 Comments

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
i know, you don't need to worry about me, do your work don't put you leg between us. ok ??
Reviewing low quality posts is my work. If you don't want to include an explanation, there's no need to reply to my review.
0

Let's see. If you have in your string.xml value like this

<string name="simple_text">Simple text</string>

And you want to use the text from your MainActivity, you can write this code

textView.setText(getString(R.string.simple_text));

In you case you can change the hint like this

editText.setHint(getString(R.string.simple_text));

But you can't from MainActivity change any value in Strings.xml.

Comments

0

You can't change strings.xml dynamically since it's a compiled resource. There are other ways for saving data in Android, here's a nice post that covers this topic: Data Storage. Hope this helps. But you can change the hint value dynamically.

EditText etUsername;
etUsername = (EditText) findViewById(R.id.etUsername);
etUsername.setHint("Your Hint");

1 Comment

This is already answered above.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.