Android: resource String automatic generation - android

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.

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!

Android find all hardcoded strings in code using Android Studio

I want to find all hard-coded strings in my code to move them into strings.xml file for future localization. Such as :
Toast.makeText(context,"Hardcoded text",LENGTH_SHORT).show();
I using Android Studio.
Go to Analyze > Run Inspection By Name... (Ctrl+Alt+Shift+I)
and type:
Hardcoded Text to find the hardcoded strings in
the .xml files;
Hardcoded Strings to find the hardcoded strings in the .java files.
Run it against the whole project, and you should get an inspection results panel that will show the hardcoded text instances.
This answer hasn't getting to me any result.
Nonetheless, for future searches :
In Android Studio 1.2.2 added new Option Hardcoded strings(not a Hardcoded text) and this getting to me perfect searches result.
After android studio 1.2.2 It seems pretty easy way to do it,
Go to Analyze
Run Inspection by name
Type : HardCoded Text
And then by selecting appropriate module's option you can get all hard coded Strings in your whole project.
Tip : Short key : Ctrl + Alt + Shift + I
It seems it has been already answered here, isn't it relevant for your problem ?
Edit : just don't forget to check "File mask(s)" box in the window after having typed "Hardcoded text", and select *.java, if you want to search in Java files.
And after you found all your hardcoded strings, this may help you to transfer them to XML.

How to edit arabic string properly?

<string name="message">هذا المجلد يحتويى على %1$s ملفات. الرجاء التأكد قبل الحذف. الملفات المحذوفة غير قابلة للإسترجاع.</string>
I wanna put "%1$s" in an arabic string, but as you can see here, word, notepad++, utraedit, all failed to get the right string. how you guys edit arabic string?
In android Studio 2, RTL support is not turned on by default,
Configure it Manually:
1. In your Computer, go to the [android-studio2.0]/bins/idea.properties
2. add editor.new.rendering=true to the end of idea.properties
3. restart your android studio.
This is a source of frustration when editing mixed-direction text. What counts is the logical order of the text, not how any of the editors display it. When you finally format the string at run time in the app, the %1$s will be replaced by whatever string you pass to the formatting method. The only thing that matters is how the string will be rendered after the substitution.
The easiest thing to do is to write the message without the %1$s, then position the insertion caret, paste in the format code, and simply ignore how the editors screw up the bidi analysis. (The screw-up is because the editors are using a left-to-right base level. In some editors, you can set the base flow to right-to-left, but then the xml markup ends up being unreadable.)
When I work with RTL text and I need to put a place holder(%1$s) or LTR words, I just write it in MS Word and copy to the IDE.
It works for me in Eclipse and Android Studio.
All you need to check is that the components that displays that string like TextView has the right gravity.
You can try in Activity..
String formatedString = String.format("%1$s", getString(R.string.your_string));

How should I be creating strings in AndroidStudio / intellijidea?

I'm writing my layouts and every time i type in a #string/new it just warns me that the string reference does not exist, but doesn't show any helper to create it. I have to go to my strings.xml and type in the xml tag for the new string.
The demos show that Android Studio even replaces the code to get strings with the actual strings, but the demo never shows a string being created from zero.
I know that when I'm in the visual editor I can create a new string element on the screen I get if I click the "..." button for the text property, but how do I get this functionality from the textual view of a layout?
I haven't used the new Android Studio extension, as it doesn't appear to be stable enough for production use yet, but in plain old IntelliJ IDEA 12, if you use Alt-Enter on the red R.string.foo reference, the intention menu has "Create string resource" as an item. Selecting that intention brings up the dialog to add the string. From the dialog you can type in the string and select which resource filters to use (i.e. which strings.xml to add it to).

Categories

Resources