Resolving Hardcoded String, should Use @String Resource in Android Application

Resolving Hardcoded string, should use @string resource in Android Application

Android Sample.JPG

When working with Android Application we might have faced this error after placing a button or other controls on the Form indicating that Hardcoded string should use @string resource.

Here we will learn how to resolve this issue when working with our Android Application.

Structure of our Project

Structure Menu.JPG

Here we, need to declare string variable for Button with the value that should be displayed in the Label of Button control.

This can be done by navigating to values Menu in the Structure displayed.

Values Menu.JPG

Double-click on the string.xml the Android Resource Elements is displayed,

Android Resources.JPG

Now, it's time to declare a string variable for the Button control. Click Add button a screen pop-ups showing the creation of new element, select string and click OK.

Create new Element.jpg

Next, we declare the Name and value of the Element to be defined as shown,

Declaration of Element.JPG

Now we have created a new element of string type with name Btn_Cancel and value Cancel. We now have to switch from design view to XML view using activity_main.xml in the screen.

Screen Switchover.JPG

Replace the Button Text property with the element name with the @string variable we have defined as like this,

android:text="@string/Btn_Cancel" />

And then, the XML File looks something like this,

Error Clearing.JPG

Now, once we switch over to Graphical Layout, the screen is as shown below.

Final Screen Shot.JPG

Hope this might be useful a little.

Ebook Download
View all
Learn
View all