Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning in android xml file

when I add any element in android layout, compiler(eclipse) shows a warning on element's attribute android:text="Text" What does mean of following warning.

"[118N]Hardcoded string "Text", should use @string resource"

when we add @ with string like "myString%" it gives error.

like image 386
FAISAL Avatar asked Jun 29 '26 12:06

FAISAL


2 Answers

The warning is a Lint Warning. Its warns about possible bugs. You can ignore it if you want to. But i feel its a useful info.

Instead of hardcoding string values to textview define strings in strings.xml and refer the same like @strings/mystring

Quoting from

http://tools.android.com/tips/lint

Android Lint is a new tool introduced in ADT 16 (and Tools 16) which scans Android project sources for potential bugs. It is available both as a command line tool, as well as integrated with Eclipse (described below), and IntelliJ (details). The architecture is deliberately IDE independent so it will hopefully be integrated with other IDEs, with other build tools and with continuous integration systems as well.

Here's a list of Lint Checks

http://tools.android.com/tips/lint-checks

To solve

Define the resource in res/values/strings.xml

  <string name="hello_world">Hello world!</string>

And in xml

  android:text="@string/hello_world"  
like image 173
Raghunandan Avatar answered Jul 02 '26 02:07

Raghunandan


It means that in your res/values folder you should create a strings.xml file and inside it put a definition like:

<?xml version="1.0"?>
    <resources> 
        <string name="my_string">Text</string>
    </resources>

and then use @string/my_string to reference it inside an xml attribute or getResources().getString(R.string.my_string); to use that string inside your code.

like image 31
type-a1pha Avatar answered Jul 02 '26 02:07

type-a1pha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!