Another time-saver! Creating String resources directly from Code editor

Write a string literal in your code like

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

Then select the string literal  like this:

 1-select-text

and press ALT+SHIFT+A, a yellow menu will appear in the bottom-right corner,

2-bottom-menu

double click on “Extract Android String” , this menu will appear:

2.-add-str-res

choose a proper name for the String resource and click Ok.

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);
Advertisements

Time-saver! creating Android string resources directly from code.

Let’s say you copy and paste an XML TextView:

 

<TextView 
  android:id="@+id/whatever"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/copy_pasted_string"/>

 

You probably won’t have the “copy_pasted_string” defined in the strings.xml file, and creating it manually is slow.

On Eclipse, place the mouse over the “copy_pasted_string” text, press CTRL+1 then click “Rename Android resource”.

It will create a new strings.xml entry, plus a new R.string resource in the R.java file.

Saved me thousands of seconds of routinary work!