How to save Strings locally and then update them programmatically? - java

what is the best way to store some Strings(used in Textview as a text) locally and then update them programmatically? Because I think(researched) that data can not be changed during runtime from res/values/Strings.xml
for example, I have a Textview, EditText, and a button. I get TextValue from Strings.xml using SetText in JAVA. I want to change the single string value from Strings.xml the value that I get from EditText.
Tell me some alternative method to get and change strings value or to how to use custom strings.xml to change the value in runtime.

Values places in XML are immutable, so you cannot change them at runtime. If you want changed values then set them directly on TextView by adding TextWatcher to your Edittext or setValues from a button's click listener.

Despite the name, this is what SharedPreferences are there for.

Related

Android: How to check the TextView value that get from MySQL with string "pending"?

Currently, my EditText will display any value that store in a column named 'comment'. But, if the EditText display text "pending" which is retrieved from that column(comment), it should not display automatically. I want to know, how to avoid the EditText from displaying "pending"?. Should be used if else statement? But how?
Below is the EditText code that displays a text which is retrieved from MySQL.
etComment.setText(user.getComment());
You can use the If-Else condition to avoid display the "pending" in the edit text.
Code:
if(!user.getComment().equals("Pending")){
etComment.setText(user.getComment());
}

dynamically change the value of a dimens.xml property within activity in android app

My goal is to alter the number of columns that my gridView will display. In the gridView XML layout, i reference a value from my dimens.xml to set numColumns. Is there a way to reference this value from dimens xml to change its value? Being new to android development, maybe there is a better way to control the value for numColumns.
all help is appreciated.
thanks
You can't change the values in dimens.xml on runtime. However you can change the number of columns in gridview using code. Below is the java method that you can call from your fragment or any class to change the number of columns
http://developer.android.com/reference/android/widget/GridView.html#setNumColumns(int)

Shared preferences "bypass default value"?

So I have a few different checkboxes in my app and I'm changing their texts with the click of a button. What I'm trying to do is to save those strings in Shared Preferences but I have one problem. When creating shared preferences I have to enter a default value, but I already did set a default value in XML file, and I have too many checkboxes to set a default value for every single one of them. So my question is: is there a way to "bypass" this default value? This is my code so far:
private String getItemQuantity(String key){
SharedPreferences itemQuantitySP = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE);
return itemQuantitySP.getItemQuantitySP(key, );
}
Thank you!
You can put your default values into constants to ie. const.java file, then instead of putting default values in XML files set initial value in Activity onCreate using shared preference value. Of course read your shared preference value using default values from const.java.

How to check if a EditText field has change content under runtime in Android

How can I check if two EditTextField's have changed content under runtime in android? I want to enable a button if both EditTextField's have some content.
Thanks in advance.
EditText is a subclass of TextView, which has the method you're looking for, addTextChangedListener().

Setting TextView to new string?

I used the layout resource editor to modify the main.xml layout file to add a second
TextView control to my app. by default, its set to something like #+id/TextView01. How do I Set the text attribute of the TextView control to my newly created String resource? I tried going into main.xml and just editing the android:text to point to the name of my new string, but it didn't seem to work.
you define your strings inside resources/strings/values.xml
and you can reference them using "#string/name_of_string" syntax in your layouts.
android:text="#string/name_of_string"

Categories

Resources