16

Can I have Eclipse adding my string resources as I code or do I have to switch to string.xml all the time and add each string?

4 Answers 4

25

Eclipse will sort of do it for you. So if you have a field:

android:text="hello"

Select "hello" and then goto Refactor-->Android-->Extract Android String, Eclipse will change the line to:

android:text="@string/hello"

and automagically add the line to strings.xml as:

<string name="hello">Hello</string>

JAL

Sign up to request clarification or add additional context in comments.

3 Comments

Note that the "refactor" option on right-clicking the string does nothing. You have to use "Refactor" from the main menu.
CTRL+1 is even easier. see my comment below:)
Indeed, see voghDev's answer for an easier solution in current Eclipse+adt
8

Eclipse has wonderful time-saving shortcuts for this!

1.- in XML editor:

Say you have a Button,TextView, or any View with a hard-coded string as text:

<Button    
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Text to add as string resource"/>

With the cursor over the string literal, press CTRL+1, then choose "Extract Android String". Choose the desired resource name (e.g. my_string_resource) then click OK. You will get a string resource in your strings.xml file:

<string name="my_string_resource">Text to add as string resource</string>

And your Button is now gonna look like:

<Button    
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/my_string_resource"/>

All automatically and without a single context-change :)

2.- In Code editor:

Write a string literal in your code like

mButton.setText("Text to add as String resource");

Then select the string literal (from " to "), and press CTRL+1, a yellow menu will appear, double click on "Extract Android String" (the S key does not work for me in this case, i just double click on the option). Choose the desired name (e.g. my_string_resouce), and click Ok. Again, you will get a new strings.xml entry:

<string name="my_string_resource">Text to add as string resource</string>

And your Button's setText line replaced by:

mButton.setText(R.string.my_string_resource);

Hope it helps and saves you as much time as it did for me! :)

3 Comments

I get "The selection is not inside an actual XML attribute", no matter if I select the text, place my cursor in it, hover my mouse over it, etc. I always get this exception
Do you have any additional errors in your code or XML files? The CTRL+1 always works when code compiles fine (Build succeeds), but may not work if there are additional errors.
My fault there is a general option to add it to strings and an android option (a little further down in the list). +1
6

The best practice is too have strings.xml inside values folder which keeps all string constants. Because later on if you want to make any change, it will easy for u if u keep in strings.xml. Otherwise you will have to always remember the file where u have wrote that constant.

1 Comment

Yes, that is the file which OP is referring to. He is asking if it is necessary to manually place strings in that file. Not sure why people are upvoting this, since it doesn't answer the question...
4

You have to switch to string.xml: its unfortunate, but right now Eclipse doesn't give you a clean way of popping into the string editor directly from the code you are typing. Optimally you would type a string constant (like R.string.new_string and I guess hotkey or double click or something and jump directly into the strings.xml editor with the existing entry selected (if new_string exists) or a new entry created (if new_string doesn't yet exist).

Wouldn't that be nice.

1 Comment

Not sure if it was true then, but this is no longer true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.