How to get original text from EditText - android

I am new to Android, my question is how to retrieve the original value from the activitMain.xml of the android : text (without saving it to a file)
I mean that in the original value was
android : text="aaa"
then it was changed by the user to "bbb"
if i do textView.getText I get the current value (bbb) i want to retrieve aaa
Thanks

(New answer with more knowledge of question)
Maybe you can do it with Strings.xml??
<string name="player_1">Rahav</string>
<string name="player_2">Martin</string>
EditText in your xml file: android:text="#string/player_1"
Then programatically you can set it to the first player.
yourEditText.setText(getResources().getString(R.string.player_1);

Related

how can i change string value dynamically

such as:
strings.xml:
<resource>
<string name="my_title">Train Video</string>
</resources>
i use this string in Java code many cases, such as:
String title = resources.getString(R.string.my_title) // title="Train Video"
textView.setText(title)
Now i get the new string valued "Train Video Demo" from server, i want to update the strings value so that when i using the following code to get new string:
String title = resources.getString(R.string.my_title) // title="Train Video Demo"
if i can't change Java code, how can i achieve it?
i know Facebook Android App can do it dynamically, i don't know it's program. Is there some blogs to explain this?
so you want to change value dynamically for that you can add %1$s in your string file
string.xml
<resource>
<string name="my_title">Title: %1$s</string>
</resources>
Activity.kt
resources.getString(R.string.my_title, title)
Here the title is dynamic value can be from API or static list.
you can't change the values in strings.xml, so you should just start off by getting all the values you need from the server to begin with, then you can cache those by using a database, you could even use a pre-populated database when releasing your app, then read those values out of the db when you need them. to update your values, make an api call, check if there are new values available, update your local db, app continues to work as normal
You can set the value of textview directly to the string you got.
textView.setText("Train Video Demo")

Display the value '??' in strings.xml

I have a little problem but I have no answer. I use the strings.xml to store all texts from my application. If i want save the next value : '??' so I have an error when I execute my app.
<string name="unknown_text">??</string>
error: Error: No resource found that matches the given name (at 'unknown_text' with value '??').
Is there a solution to save this value in xml or I must use setText() for all widgets which need it ?
Thx
It's good habit to always quote whole string:
<string name="unknown_text">"??"</string>
use <string name="questions">\??</string>
check FormattingAndStyling and this link for more detail.

Copy string to strings.xml

Everybody knows that if we have:
ekran3.setText("VAT Tax:");
We may (or even we SHOULD) convert it to:
ekran3.setText(getString(R.string.kwotaVat));
and add in strings.xml:
<string name="kwotaVat">VAT Tax:</string>
But is there some kind of trick to do it automatically? For example by clicking RMB on text and selecting some option? It would be nice to know it in fact it will save us a lot of time than while we're doing it manually.
If you are using Eclipse you may extract the string directly into the strings.xml file by placing the mouse within the string and hitting Ctrl + 1. It will bring up the dialog as followed and you may select "Extract String". You then give it a name (Ex: kwotaVat) and you're done.
hey you do not need to use getString() to convert it to string the values xml file is already having data in string form so you just need to use the following code to set the string
ekran3.setText(R.string.kwotaVat);
where ekran3 is the object of your text view
and kwotaVat is the id of your value string
for more detail od android codes have look here http://grabcodes.blogspot.com/

How to check if string tag contained text is being updated

I am showing data of a <string name="whatsnew"> </string> in a textview and i have to show this textview only when the text inside this being changed.
for eg:
If previous state is:
<string name="whatsnew">Hi Stackoverflow Collegues </string>
If current state is this data is being updated as:
<string name="whatsnew">Hello Stackoverflow Collegues</string>
I have implemented a logic that i will retrieve the previous and current stringlength of
<string name="whatsnew"> </string>
containing text.But this logic has a loop hole if the updated text is of same length of previous string length.
Please help me with a better and efficient logic.
use hashCode() of your string to compare, for example:
int hash = "this is my string".hashCode();
or simply compare with another string:
boolean isSame = "first string".equalsIgnoreCase("second string");
Could you just compare the strings and set it visible if they are different?
if(oldString != newString){
view.setVisibility(VIEW.Visible");
}
getText() method may help .. check if the same text is in the TextView of it changed
just compare the whole text

Write data in String.xml Android

I want to write some values from editText of activity in the file string.xml.
I will explain with an example:
In the activity I have 2 editText and 1 button to submit the information. Imagine that the editText are for inserting NAME and SURNAME.
I have the following code in string.xml
<string name="person_name"></string>
<string name="person_surname"></string>
At the begining, those fields will be empty but after submiting the info, I want to write the values obtained from the editTexts in that file.
Any suggestion?
Thanks in advance
That's not possible. Strings.xml is only for storing predefined strings. Use SharedPreferences to store this data.
Although what you want is not possible but can be done through SharedPreferences http://developer.android.com/guide/topics/data/data-storage.html#pref

Categories

Resources