Android: resource String automatic generation

11,300

Solution 1

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.)

Solution 2

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.

Solution 3

Thanks to Brent Hronik. CTRL-1 on Windows works fine.

Solution 4

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.

Solution 5

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.

Share:
11,300
Tanvir
Author by

Tanvir

Updated on June 04, 2022

Comments

  • Tanvir
    Tanvir almost 2 years

    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?

  • math
    math over 11 years
    You need to select the text in the xml file while beeing in text-mode.
  • Pratik Butani
    Pratik Butani over 8 years
    What about Android Studio
  • Laur Ivan
    Laur Ivan about 8 years
    IMHO you should update this as a comment if possible.