-1

How to Change the value of R.string programmatically?

nameEditText = (EditText) findViewById(R.id.name); 
name = nameEditText.getText().toString();

string.xml

<string name="name">name</string>

I had declared a string in my strings.xml file.

public class MainActivity extends AppCompatActivity {

    private String name;
    private EditText nameEditText;     

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameEditText = (EditText) findViewById(R.id.name); 
        name = nameEditText.getText().toString();
   }
...

I want to get the edittext value and set to <string name="name">name</string>.

This code get the text from Edittext.

nameEditText = (EditText) findViewById(R.id.name); 
name = nameEditText.getText().toString();

I want to change the value of <string name = "name"> name </string> to the value of the string name.

How can I do this?

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
Pradeep
  • 19
  • 1
  • 6
  • R.string values are designed to be constants, you are using it the incorrect way. Maybe you should explain what you are trying to achieve. – mrek Jul 28 '20 at 06:23
  • I think you cannot change or modify the resources in Android. Instead you could use a SharedPreferences and update or modify those variables. – KalanaChinthaka Jul 28 '20 at 08:05

1 Answers1

0

First of all you can not modify resuorces in android. If you want to keep track of edited value then use global variable to store it. For initializing global variable you can use resource string

alokHarman
  • 289
  • 1
  • 8
  • Can you give more information? – Pradeep Jul 28 '20 at 05:46
  • I don't know why you want to do this. You might want to keep updated value to read some where else(other activity) then put variable at application level. If you can explain your use case then it may help to reach solution – alokHarman Jul 28 '20 at 05:52