Search code examples
android-layoutandroid-activity

hardcoded string “Button”, should use @string resource


I am new in Android app development and using Java language. My problem is every time I make a TextView or Button there is a triangle with the exclamation mark below them. and when I click it I saw a message saying:

hardcoded string “Button”, should use @string resource

I have two activities, in my main activity there is a Button that when you click it you will go in second activity. But when I go to my main.java to make a code for the button. There's always the above shown error. I think the eclipse can't find the id of my button and same for my TextView they have same error message.

Here is the code I made:

Button b = FindViewById(R.id.button1);

I also add:

Button b = (Button) FindViewById(R.id.button1);

I am using the latest eclipse classic and ADT august issue. The platform is Android 4.1 API 16.


Solution

  • You shouldn't hardcode the "text" on the widgets use the strings resources ie., strings in the strings.xml to set the text. Declare the "text" you want to display as a string in strings.xml and access it using @string/your_string_name in the layout file.