-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?

ADM
  • 20,406
  • 11
  • 52
  • 83
Saqib Farooq
  • 9
  • 1
  • 3

3 Answers3

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
0
editText.setHint(getString(R.string.simple_text));
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
SahdevRajput74
  • 754
  • 7
  • 18
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) 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. – Rosário Pereira Fernandes Mar 17 '18 at 09:27
  • i know, you don't need to worry about me, do your work don't put you leg between us. ok ?? – SahdevRajput74 Mar 17 '18 at 09:37
  • 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. – Rosário Pereira Fernandes Mar 17 '18 at 12:40
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");
Salman
  • 9
  • 4