30

Is there a way I can suppress individual warnings about hardcoded strings in layout files?

I often put placeholder text into TextViews so that I can see them in layout at design time. The downside of this is getting a ton of these warnings about hardcoded strings. But without them I wouldn't see the TextViews at all in the layout.

2
  • I get the principle of using strings.xml, but sometimes there are instances where you know you're only using the string once..
    – Jacksonkr
    Commented Sep 2, 2012 at 20:48
  • 1
    Good question. Seems like it would be nice to have some sort of escape character(s) to allow text as a placeholder, e.g. "<User name goes here>"
    – Daniel
    Commented Dec 1, 2012 at 1:16

4 Answers 4

36

You can add the following to the text view element:

tools:ignore="HardcodedText"

Example:

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a hardcoded text"
        tools:ignore="HardcodedText" />

Note there is a shortcut in Eclipse for adding this easily: just press CTRL + 1 and select the relevant option.

Unfortunately, I couldn't find a way to do this for a whole layout, you will have to do it for each element.

Note that you must also add the xmlns:tools="http://schemas.android.com/tools attribute to the root element

4
  • This is what I want. I dont want to ignore all android lint warnings. They are useful anyway, but sometime they make me crazy because I know it and I don't need it :)
    – Jacob Dam
    Commented Apr 13, 2013 at 15:12
  • 4
    Note that you must also add the xmlns:tools="http://schemas.android.com/tools" attribute to the root element, otherwise you'll get errors like "Error parsing XML: unbound prefix". Commented Apr 20, 2013 at 5:23
  • Perfect solution, short, simple, and sweet. Thanks!
    – SMBiggs
    Commented Apr 8, 2018 at 6:21
  • to make it apply for the whole layout you gonna need to include it inside parent view. Commented May 6, 2018 at 12:14
31

The other way is to use tools:text instead of android:text:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:text="This is a hardcoded text" />

Note that you must also add the xmlns:tools="http://schemas.android.com/tools attribute to the root element

7
  • 2
    I find this better than tools:ignore="HardcodedText" because the preview is actually displaying it
    – Shine
    Commented Feb 4, 2015 at 22:56
  • 1
    Whaou, I didn't know about that, great find
    – Redwarp
    Commented May 30, 2015 at 18:42
  • 1
    I like this answer, but the linter complains about tools:text being hardcoded too, which is silly since it's just sample text that doesn't get compiled into the app... doesn't seem to be any way to turn it off...
    – Andrew
    Commented May 4, 2017 at 14:46
  • @Andrew I'm not getting linter errors, perhaps due to a more recent version of the tools.
    – ThomasW
    Commented Feb 6, 2018 at 2:28
  • 1
    This method make the text disappeared from the design preview. Commented May 6, 2018 at 12:13
19

In Eclipse, go to Window->Preferences->Android->Lint Error Checking.

Scroll down to and select Hardcoded Text (under Internationalization). On the Severity drop down box, select Ignore and click on Apply.

1
  • 3
    Similar for android studio as well. Preferences -> Android -> Lint ->Internationalization -> "Hardcoded Text" and "Text Internationalization". I toggle those on and off for toast's as well
    – Diesel
    Commented Oct 1, 2018 at 15:14
1

Use string.xml file to remove this warning....

You have to put your string in string.xml file and then give like android:text="@string/mytext"

And in res-->value->string.xml add <string name="mytext">Your Text</string>

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

2
  • good link, never noticed the android tools site before.. looks like some useful info is there
    – ycomp
    Commented Feb 20, 2012 at 14:27
  • thanks for suggestion but this adds more problems than it solves for me... I like to keep strings file relatively clean
    – ycomp
    Commented Feb 20, 2012 at 14:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.