AndroidTips & Tricks

Android | Hardcoded String should use @String Resource

How to Fix Hardcoded string issue in android studio

Android Studio is one of the best Platform to build professional applications which is officially launched by Google. It is a huge software which extended features for developing apps. When people come to develop android apps, they might encounter many problems but today I’m gonna show the most important issue which is Hardcoded String should use @String Resource. 


Why this error comes?

Well, this error ( Hardcoded string should use @string resource ) usually comes when you try to add buttons, text views, Plain text etc in the android studio. The reason for this error is because in the android studio, probably everything has its own id through which it is recognized. Therefore when building configuration variations (like for landscape or portrait) actual text would be repeated every time and it should be updated every time when you make some changes. Another reason for that is the application can’t be able to be translated into other languages.
Note: Ignoring the error would not prevent you from building an app but it’s not a good approach!

Therefore, in order to solve this issue, A string should be declared in the strings.xml file which is located under app >> res ( resource )  >> values. To declare a string in the strings.xml file, open the strings.xml file and write the following code in between resources tag :

<string name=”nameofcomponent”>your text </string>

It might be confusing for you, but don’t worry! See the code in my case and you’ll get the idea 🙂

In my case, I used the button in my layout, therefore, I set name to “button” and the text under my button is Calculate. Hurrah! You have successfully declared a string in the strings.xml file.

Now the last step is to open the activity_main.xml file which is located under app >> res >> layout which is probably the file where you designed your app. Below the component tree section, you’ll have two options :

1. Design ( which you’re currently inside it )
2. Text

Click on the Text option and you’ll get the coded stuff of the layout. Now under your component ( in my case it is a button ), Search for android:text=”Your Text”, in my case it is android:text=”Calculate”. Now, this is wrong, we have to use a string here which we created earlier in the strings.xml file…

Change the android:text=”your text” to android:text=”@string/your_componet_name” .

In my case, I changed it to android:text=”@string/button”. you can see in the screenshot.


After changing it, you’ll see the error has been removed… 🙂

Note: If you have any questions, Feel free to mention in comments…


Sarmad Nadeem

Sarmad is a Blogger, Android developer and a Freelancer who has been working in this field for many years. He'll help and guide you in any issue. Feel free to contact him.

Related Articles


  1. hey,
    I’ve got an annoying problem. I’m using @string for my layout, but when I close the tab and open them again all parameters will back to hard code again! How can I solve this.


Leave a Reply

Your email address will not be published. Required fields are marked *