iBEK iBEK -3 years ago 117
Android Question

Why does Android Studio give me warnings to add code that Android Studio already added automatically?

As soon as I drag/drop a button from Palette into the XML file, Android Studio gives me the following error when I click on "Show Warnings and Errors":

Message: [I18N] Hardcoded string "Button", should use `@string` resource Suggested Fixes:
- Extract string resource
- Suppress: Add tools:ignore="HardcodedText" attribute


Inside the Button code in Text the following Android Studios enters itself automatically when I dragged/dropped button into XML file:

android:text="Button"


Do I just always have to click "Suppress" choice because I don't know what they want me to change for "Extract String Resource" choice. Or is it asking me to change Android Studio's code that it sets itself automatically?

Answer Source

You can do this instead of suppressing:

  1. Add a String resource in res/values/strings.xml:

    <string name="button_label">Button</string>

  2. Set the above resource as text for your Button:

    android:text="@string/button_label"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download