22

I know there is Alt + Enter combination to extract single String to string resource. But I am wondering is there anything that I can extract all string of my project into string resource?

Also Android studio not making same String to string resource if I make one of them.

String s = "Hello World";
String s2 = "Hello World";

For example. I make "Hello World" to string resource still another remain Hardcoded in same file as well in the project too.

String s = getString(R.string.helloworld);
String s2 = "Hello World";

If anyone know something like that.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
  • Just use Find and Replace. It will be more efficient ! – Shree Krishna Feb 23 '16 at 05:51
  • 4
    There is no need to close this one, as it neither fall in any of these, `duplicate of, off-topic because,unclear what you're asking, too broad, primarily opinion-based`. This is just a question to know if there is any option/tool/feature available or not. – MKJParekh Feb 23 '16 at 05:56
  • you have to spend a little time for that. As I know there is no such options except opening each file and replacing the same String.. – Shree Krishna Feb 23 '16 at 06:00
  • @ShreeKrishna try to find some better option. This type of option available in xcode so I think something like that in android studio too. – Shabbir Dhangot Feb 23 '16 at 06:05
  • Ok I will but [this](http://www.jetbrains.com/idea/help/find-and-replace-in-path.html) may help you little bit on the secondary option which was finding and replacing. – Shree Krishna Feb 23 '16 at 06:09

5 Answers5

27

As your requirements and as I know there is no such feature in android studio you were really searching for, But here are some alternative ways that can help you.

  • Go to "Analyze > Run Inspection ..", and type "Hardcoded strings". Run that in your whole project, and you will get an inspection results panel that will show all the hardcoded text of projects. Then hit Alt + Enter and you'll get an option to automatically extract that Strings.

  • Another approach is to Find and Replace But It's not better because of time consumption. To simplify the approach you can have a look at here for flexibility.

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • 1
    yes that help me better. but it not finding in the xml. – Shabbir Dhangot Feb 23 '16 at 06:27
  • 5
    @CreativeAndroid Analyze - Run inspection by name - "Hardcoded text Android Lint" and choose file mask *.xml – MKJParekh Feb 23 '16 at 06:31
  • 2
    @CreativeAndroid Yah thats so. `hardcoded texts` will show all hardcoded strings of xml and `hardcoded strings` of internationalization issues will show strings of java files. – Shree Krishna Feb 23 '16 at 06:37
  • 1
    It only works for me in the layout file, not in my java classes – A P Feb 18 '17 at 17:17
  • This is not working in with latest version (2.3.3). That option has been removed. So bad! Does anyone have any clue? – sud007 Jul 27 '17 at 11:26
  • @AmeerHamza, brother, that is working only in some places like @ Avi Parshan mentioned. Not all places like before. – sud007 Oct 23 '17 at 07:47
4

For hardcoded strings in XML layout files:

Click on Analyze -> Run inspection by name -> enter Hardcoded strings -> select the whole project -> OK. Here, you will get all the hardcoded strings from your xml layout files only that u can extract to strings.xml by:

Click on the hardcoded text -> Alt + Enter -> extract from resource -> enter your corresponding resource name for that string -> OK

For hardcoded strings in Java class code:

Click on Analyze -> Run inspection by name -> enter Hardcoded texts -> select the whole project -> OK. Here, you will get all the hardcoded strings from your Java class files only that u can extract to strings.xml by:

Click on the hardcoded text -> Alt + Enter -> extract from resource -> enter your corresponding resource name for that string -> OK -> add getString for each string resource created in your java code.

Hence, Hardcoded strings -> Java class code whereas Hardcoded texts -> Xml layout files in Android Studio.

Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20
3

In Android 3.2.1 you can go to Edit->Find->Find In Path... and then search for android:text=" . This will give you a list of all the hardcoded strings in xml files.

Search for Toast.makeText to find all the toast items from the java files.

Search for setText(" to search for text sets in java files and so forth.

You can do searches like this for items you would like to find throughout the project and replace.

DragonFire
  • 3,722
  • 2
  • 38
  • 51
1

I wrote the following script to extract all hardcoded strings from an activity.xml file and add them to strings.xml. (Bash/Linux):

Usage: extractAll.sh activity_main.xml strings.xml
Original files are backed up before changes are made.

#!/bin/bash
################################################################################
#  Extract All Hardcoded Strings From an Activity XML File and Save New 
#  Versions of the activity.xml and strings.xml
################################################################################

#check the number of arguments supplied, print usage if not 2
if [ "$#" -ne 2 ]; then
    echo "extract all hardcoded strings from activity.xml and update .xml files"
    echo "original files are saved with .bak extension"
    echo "usage: $0 activity.xml strings.xml"
    exit
fi

#backup input files
#TODO: save these backups in another folder so they don't cause build error
cp $1 $1.bak
cp $2 $2.bak

#grep for hardcoded strings, for each one sed out special characters, change 
#all to lower case, replace space characters with underscores, truncate string
#variable names to 30 characters. The result will be the name of the new string
#variable entered in strings.xml
grep -Po "(?<=android:text=\")[^\"]+(?=\")" $1 | while read -r HARDSTRING ; do
    STRINGVARNAME=`echo $HARDSTRING | sed 's/\([\d0-\d31]\|[\d33-\d35]\|\$\|[\d37-\d47]\|[\d58-\d64]\|[\d91-\d96]\|[\d123-\d126]\)//g'|sed -e 's/\(.*\)/\L\1/'|sed 's/ /_/g'|head -c 30`
    #substitute each hardcoded string with the string variable name
    sed -i "s/$HARDSTRING/@string\/$STRINGVARNAME/" $1
    #get the number of lines in strings.xml file
    NUMLINES=`wc -l < $2`
    #insert string definition at second-to-last line of strings.xml
    let "NUMLINES++" #I had to increment mine to get the desired result
    #add an entry to the strings.xml defining the newly extracted string
    sed -i "$NUMLINES""i\\    <string name=\""$STRINGVARNAME"\">$HARDSTRING</string>\\" $2
done

Final note: wc -l returned one less than expected in my test case using a strings.xml file created by Android Studio (I suspect because the last line had no newline). This caused sed -i to insert the new line at the third-to-last position instead of the desired second-to-last. My fix was to increment the result of wc -l using let.

References:
What are invalid characters in XML
sed one-liner to convert all uppercase to lowercase?
How to process each output line in a loop?
How can I truncate a line of text longer than a given length?
Linux Shell Script - String Comparison with wildcards
How to insert a string into second to last line of a file
Check number of arguments passed to a Bash script
https://askubuntu.com/questions/385528/how-to-increment-a-variable-in-bash
https://docs.oracle.com/cd/E82085_01/150/funtional_artifacts_guide/or-fasg-standards.htm
https://tldp.org/LDP/abs/html/refcards.html#AEN22828

JB0x2D1
  • 838
  • 8
  • 20
0

I believe the shortcut you are looking for is (on a Mac) Alt + Command + C

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
AllDayAmazing
  • 2,383
  • 1
  • 24
  • 25