i am writing a piece of code where it creates a button and put a text on it. here is the piece of code
<Button android:id="#+id/start_client"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Client" />
but the part "android:text="Start Client" />" is being highlighted (yellow), not allowing me to build the project.
Can anybody help me out??
Thanks
//that yellow line is indicating as warning
you need to use res/string file to store text values.
[I18N] Hardcoded string "Start Client", should use #string resource
this is because of Lint tool.
your project has some other error.
open your problems tab and check whats wrong there.
//clean and build again will also work fine.
The yellow part is a warning to you to not use a hard coded string for text. means you cant give just any string directly to any content like button.
whatever string you want to use, first you will add that in string.xml (check in "values" folder in left side).
after adding any string, you can use in you project anywhere without getting any warning.
Related
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?
You can do this instead of suppressing:
Add a String resource in res/values/strings.xml:
<string name="button_label">Button</string>
Set the above resource as text for your Button:
android:text="#string/button_label"
It is not an Error, this is just Warning that say you should use string resources, but not hardcoded strings in your code. But you can use hadrdcoded Strings and all will work fine.
Using String resources simplifies your life. For example when you want to localize your app. See screenshot.
using string resources
You can set string res. to TextView.Text, then create few android resource directories with names values, values-ru, values-uk etc.
In this direcroties you should create file strings.xml for each.
and Override your string like
<string name="message"> Message </string> in values
<string name="message"> Сообщение </string> in values-ru etc.
Now if you change your device language, from en to ru , in your app in textView will be written "Сообщение"
It is also useful in several other cases.
Message: [I18N] Hardcoded string "Button", should use #string
resource
Suggested Fixes:
Extract string resource
You should always pick string from string.xml in res/values/strings.xml. It's not the right way to hard code the strings, putting it into the string.xml file helps for localization in future.
Add tools:ignore="HardcodedText" attribute
When you use tools, it doesn't actually set the text to view, but it just shows you in preview window as how it visually appears, when you run the app, you won't the text added in tools.
[I18N] Hardcoded string "Happy Birthday Debashish", should use #string resource less... (Ctrl+F1)
Hardcoding text attributes directly in layout files is bad for several
reasons: * When creating configuration variations (for example for landscape or
portrait)you have to repeat the actual text (and keep it up to date when
making changes) * The application cannot be translated to other languages by
just adding new translations for existing string resources. In Android Studio
and Eclipse there are quickfixes to automatically extract this hardcoded string
into a resource lookup.
Ths is not an error but a warning. As a general rule, you should never use hard-coded strings in your layout but always use string resources instead (which means that all the strings are stored in one separate file where they are easily changeable for different languages and so on).
To convert a hard-coded string into a string resource:
Put the curser on the hard coded string;
Press ALT + Enter;
Enter a name for your ressource;
Click OK.
After doing this the warning will be gone.
This is just a warning.
Define your string in string.xml file
Happy Birthday Debashish
and in textView use this string as
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/string_name"
/>
This is only a warning. The function will still work as intended. It is just recommended to place your text in the strings.xml file. This makes future changes much simpler and is easier to reference across multiple pages.
First, place the <string> element in values/strings.xml like this:
<string name="your_string_name">Happy Birthday Debashish</string>
Then, you can reference the string in the .xml file as follows:
<TextView
android:text="#strings/your_string_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Again, it is not required to do it in this method. It just makes things simpler to manage and change in the future if needed.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title"
tools:text="Happy Birthday Debashish" />
When you are in the 2019 version. Go to the strings.xml and Add this in to it
<string name="Your text">Your Text</string>
Or
In the warning it has the Fix button you can click it for fix
I'm new to Android. When I add a button/views in Graphical layout it adds the label text this way- android:text="Button" . Why doesnt it add "android:text="#string/my_label" and add a string resource in string.xml file. Can't it be done automatically in eclipse?
I have searched a lot but I have not get any automated way to add a string to the resource file But This will save your time a lot IMHO.
Select a String, click Refactor --> Android --> Extract Android String.
Thanks to Brent Hronik. CTRL-1 on Windows works fine.
Because you don't have to use the #string resource. The purpose of the #strings resource is to make it easier to change elements about your code. For example, if you are using your application title in mutliple places, let's say in every dialog box, then if you change the title you would have to change it in all the instances that the app title is being display. So in this instance the #string/App_Title could be set to "My Program" and all of the dialog boxes can reference that. If you change the title to "Hello World" then all of these are changed. The #strings resource, while eclipse tries, doesn't have to be used for every string. Not using it is the equivalent to hard coding the value. There are plenty of reasons for and against using #string for everything.
I am not sure if there is a setting in eclipse that will automatically add a string to the resource file when the control is added.
(EDIT: Based on other users CTRL+1 is the short cut to do this.)
You can add the string to the strings.xml by clicking command and 1(on a mac, assume it would be control 1 on a Windows or Linux box) simultaneously. This will add the resource to strings.xml and then open that up in the editor.
Thanks Siddiq Abu Bakkar! I didn't think it would be there.
On Eclipse (and Windows) the shortcut is:
Alt+Shift+A (release all and then press) S
When you use Eclipse for first time it's not easy understand how to use these kind of "complex" shortcuts.
I can't vote and i can't comment answers yet (missing reputation as i'm a new user)
But i confirm :
1) hard type the string in your code like
mydlg.setTitle("hello guys");
2) select your string (e.g : "hello guys")
3) press Alt + Shift + A then press S
a dialog will appear to let you add a new string into resources. Everything should be already filled into that dialog box.
I've tried searching for it, but I can't seem to find anything, since searching with either > or &gt; doesn't really give you the intended search result.
The problem I'm facing is, that whenever I put a string into strings.xml, for example I typed in,
1 > 2 > 3
It changes it to 1 &gt; 2 &gt; 3
I've tried hardcoding the string in the layout file itself, but it seems to automatically change to &gt;, and I've tried running it on the emulator, as well as it showing on the preview, it seems to only show the html number of it.
Any solution would be appreciated. Of course, I would like to avoid typing all of the text manually by using Html.fromHtml
I don't understand, because I've made a test project, and it's working as intended :
in my res/values/string.xml file :
<string name="test_string">This is > a string <</string>
in my res/layout/main.xml file :
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/test_string" />
And on my Android device, it's showing :
This is > a string <
Can you post an extract of your own string.xml and layout file, and give a screenshot of your app ?
Can I have Eclipse adding my string resources as I code or do I have to switch to string.xml all the time and add each string?
Eclipse will sort of do it for you. So if you have a field:
android:text="hello"
Select "hello" and then goto Refactor-->Android-->Extract Android String, Eclipse will change the line to:
android:text="#string/hello"
and automagically add the line to strings.xml as:
<string name="hello">Hello</string>
JAL
Eclipse has wonderful time-saving shortcuts for this!
1.- in XML editor:
Say you have a Button,TextView, or any View with a hard-coded string as text:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text to add as string resource"/>
With the cursor over the string literal, press CTRL+1, then choose "Extract Android String".
Choose the desired resource name (e.g. my_string_resource) then click OK. You will get a string resource in your strings.xml file:
<string name="my_string_resource">Text to add as string resource</string>
And your Button is now gonna look like:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/my_string_resource"/>
All automatically and without a single context-change :)
2.- In Code editor:
Write a string literal in your code like
mButton.setText("Text to add as String resource");
Then select the string literal (from " to "), and press CTRL+1, a yellow menu will appear, double click on "Extract Android String" (the S key does not work for me in this case, i just double click on the option). Choose the desired name (e.g. my_string_resouce), and click Ok. Again, you will get a new strings.xml entry:
<string name="my_string_resource">Text to add as string resource</string>
And your Button's setText line replaced by:
mButton.setText(R.string.my_string_resource);
Hope it helps and saves you as much time as it did for me! :)
The best practice is too have strings.xml inside values folder which keeps all string constants. Because later on if you want to make any change, it will easy for u if u keep in strings.xml. Otherwise you will have to always remember the file where u have wrote that constant.
You have to switch to string.xml: its unfortunate, but right now Eclipse doesn't give you a clean way of popping into the string editor directly from the code you are typing. Optimally you would type a string constant (like R.string.new_string and I guess hotkey or double click or something and jump directly into the strings.xml editor with the existing entry selected (if new_string exists) or a new entry created (if new_string doesn't yet exist).
Wouldn't that be nice.