Copy string to strings.xml - android

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/

Related

How to find where all the string key is used in android?

I am working in a multi language project so want to find out where exactly the strings are used from the value name in the string file programatically so once I get the Ids of the widgets (i.e textview or spinner or any widgets) where the strings are used so I can set them at once.
We are actually giving an interface for the user to change the content (label) of the textview field of android in our website so wanna set them to the received from json at all the places in android application.
Please help!!! Tysm in advance!
I want to find out where the string values are used which is declared
in the string file in android.
Steps.
1) Go to your res folder > Values > strings.xml
2) Select the String which you need to look up in project.
3) right click > find usages ( Alt + F7)
4) In bottom find tab will appear with all the string usages through out the project and it will show the usage in Java and XML resources as well.
For your reference added the screenshot
For replacing the text you can look into the #Anurag Pandit answer
I assume you are using Android Studio. In that case you can right click the key of the string in the strings.xml file and select Find usages. This will show you a list of where the selected key is used.
Simply use Command + Shift + r
A popUp will appear, place your required search string in "Text to find:"
and in Scope select Whole project .
You can see all the places where the string is used in Preview window before replacing it.
Here you could find a bunch of shortcuts for Mac/Windows/Ubuntu for faster development.

Creating a string constant to ALL language strings.xml in Android Studio

When I have to make a new string constant in a project I often use to simply write down the reference first just like: R.sring.my_new_string then I use Alt + Enter to pop up this dialog:
The problem is, as you can see only the default 'values' is selected when the dialog pops and I have to select the other 'values-it' (Italian strings) manualy to create the same string resource to that file too.
Of course this results in a deficient 'values-it/strings.xml' file because a lot of times I simply forget to select the checkbox.
Is there a way to somehow set Android Studio to always create the string resource for EVERY strings.xml in the project?
Thanks in advance
Press that to select all.
That doesn't solve your problem?
Happy coding!

no resource found that matches the given name at hint = #string/edit_message

I am new to android, I am trying to create a sample UI which is described in the developer.android website. When I add a edit text, I am getting that error message.
If you are currently using this: android:hint = "#string/edit_message", just do like that: android:hint = "edit_message". This is called hardcoded string. Its working, but its not recommended that you make strings this way. The other way is to go to resources, then to strings folder, and there should be one .xml, with strings. To make a new string, just copy the last one, change the name, and the content between >...<, where your string should be. To use that string you get it by its name. So if you name it "myStr", to use it in editText, you need to say: android:hint="#string/myStr" and you will get the content of the string. Hope this helps. Mark as answer if it does. Good luck.
Did you add the string resource?? To the left is package explorer. Select Your project and go to its res folder. In res folder select values and then click on strings.xml. Add your string resource there and save it. Then I dont think theres scope for such error messages.

Android: resource String automatic generation

I'm new to Android. When I add a button/views in Graphical layout it adds the label text this way- android:text="Button" . Why doesnt it add "android:text="#string/my_label" and add a string resource in string.xml file. Can't it be done automatically in eclipse?
I have searched a lot but I have not get any automated way to add a string to the resource file But This will save your time a lot IMHO.
Select a String, click Refactor --> Android --> Extract Android String.
Thanks to Brent Hronik. CTRL-1 on Windows works fine.
Because you don't have to use the #string resource. The purpose of the #strings resource is to make it easier to change elements about your code. For example, if you are using your application title in mutliple places, let's say in every dialog box, then if you change the title you would have to change it in all the instances that the app title is being display. So in this instance the #string/App_Title could be set to "My Program" and all of the dialog boxes can reference that. If you change the title to "Hello World" then all of these are changed. The #strings resource, while eclipse tries, doesn't have to be used for every string. Not using it is the equivalent to hard coding the value. There are plenty of reasons for and against using #string for everything.
I am not sure if there is a setting in eclipse that will automatically add a string to the resource file when the control is added.
(EDIT: Based on other users CTRL+1 is the short cut to do this.)
You can add the string to the strings.xml by clicking command and 1(on a mac, assume it would be control 1 on a Windows or Linux box) simultaneously. This will add the resource to strings.xml and then open that up in the editor.
Thanks Siddiq Abu Bakkar! I didn't think it would be there.
On Eclipse (and Windows) the shortcut is:
Alt+Shift+A (release all and then press) S
When you use Eclipse for first time it's not easy understand how to use these kind of "complex" shortcuts.
I can't vote and i can't comment answers yet (missing reputation as i'm a new user)
But i confirm :
1) hard type the string in your code like
mydlg.setTitle("hello guys");
2) select your string (e.g : "hello guys")
3) press Alt + Shift + A then press S
a dialog will appear to let you add a new string into resources. Everything should be already filled into that dialog box.

Android Strings

I wrote a big app with thousands of string in the code.... very bad idea, because now I want to translate each string.... big problem.
Copying all strings to the strings.xml takes a long time.
Eclipse has an option to take all selected strings and put them into messages.properties.
Does this work similiar like strings.xml? When, why all people use strings.xml.
Or should is use eclipse to seperate each string and than I should copy them to string.xml?
All people are using strings.xml because this is the normal way to do it on Android. You don't have to manage the load of the strings, to call any locale function in your script.
You can see the documentation here : http://developer.android.com/guide/topics/resources/index.html
BTW, you can easily transform your eclipse generated file to an strings.xml file after the extraction.
In Eclipse you can use the shortcut keys Alt + Shift A, S to extract an inline string in to the strings.xml file via a popup dialog - might be a bit easier than doing it by hand. And as the others say, yes you should ALWAYS use the strings.xml file so that you only have to look in one place when you want to change a string, instead of having to search through all your code.

Categories

Resources