Monday, April 25, 2016

Android Studio: easier text string extraction from a layout file into a string resource XML file

All the while in Android Studio, I individually create and populate a strings resource XML file, then link the named strings to the layout XML file using the @string/string_name construct. Then I found that Android Studio has a convenient helper function to extract the text string from the layout XML file and placed it into the strings resource XML file. That function is the Extract String Resource command, and the following steps show how to use it.

  1. In Android Studio, open up a layout XML file. Place the cursor on the line with the text you want to extract e.g. android:title="My title", as shown in the screenshot below.


  2. Wait for the light bulb to appear, then either click on it or press ALT+ENTER.

    A context menu appears.

  3. Chose Extract String Resource.

    The Extract Resource dialog appears.

  4. In the Resource name field, type in a label e.g. my_title.

    Optional. Change the file name, resource directories, or create new resource directories.
  5. Press OK.

    The text in the layout is extracted into the selected string resource XML file.

No comments: