Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 833
yatot13 Avatar asked Oct 01 '12 17:10

yatot13


2 Answers

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.

like image 161
Dinesh Venkata Avatar answered Oct 14 '22 12:10

Dinesh Venkata


Main.xml file (The xml file which relates to the main activity)

Notice the id of the button, which is rounded in red. You have to use this id when you want to call it in a method, for an example

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

Furthermore, check whether your graphical layout matches with the image I have provided.

Graphical view of the Main.xml file

Just try your code again with these changes. Your main.java would look like this.

Main.java file

like image 42
chinthana Avatar answered Oct 14 '22 14:10

chinthana