Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract all hardcoded strings to string resource

Tags:

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.

like image 757
Shabbir Dhangot Avatar asked Feb 23 '16 05:02

Shabbir Dhangot


2 Answers

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.

like image 64
Shree Krishna Avatar answered Nov 09 '22 20:11

Shree Krishna


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.

like image 34
DragonFire Avatar answered Nov 09 '22 19:11

DragonFire