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?
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>
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 :
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…