So I have a string in my strings.xml file declared like so:
<string name="welcome">Please hit the menu to begin</string>
And I have a TextView in my main.xml that uses it like so:
<TextView
android:id="#string/welcome"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/welcome"
/>
Now, is that the proper way to give a TextView an ID? It seems strange to use a string resource as an ID like that.
Now, is that the proper way to give a TextView an ID?
No. Use android:id="#+id/whatever".
To add an id directly to a textview you must append a + sign
android:id="#+id/welcome"
alternatively you can have an id set up in a resource file
<resources>
<item name="welcome" type="id"/>
</resources>
android:id="#id/welcome"
http://developer.android.com/guide/topics/ui/declaring-layout.html
is the manual page related to this topic
Related
As I asked in the title, is there any way to do so?
Now, when I put all styles into one file it looks a little crowded, I would like to separate styles.
For example:
res/values/styles_for_main_screen
res/values/styles_for_set_screen
And then in main_screen layout
<TextView
style="#styles_for_main_screen/text_view_custom_style">
</TextView>
This example obviously doesn't work, but it shows what I'd like to achieve.
I read in every tutorial that we need to put our custom styles into styles.xml file, but I wonder if there is a possibility to diversify styles in few xml files?
Every question I read was something like "how to do .... in styles.xml".
I can't find question similar to mine.
Example how it should be done, thanks #Frank N. Stein for the answer:
This how looks my custom xml res/values/styles_for_main_screen
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="custom_back">
<item name="android:background">#E81C1C</item>
<item name="android:text">whatr</item>
</style>
</resources>
and then to retrieve this style I just write:
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
style="#style/custom_back"
/>
so the convention looks like:
style="#(what I want to retrieve)/(name of style)"
from the android developers site
In XML: #[package:]style/style_name
You can call your style files whatever you want (if you respect the naming conventions and you put them all in the values folder/s), as you do with strings and colors.
Therefore, YES! You can have multiple ones, if so you desire.
Obviously, you will NOT specify the path to each file.
Referencing the style/s by using R.style.your_new_style is enough.
Remember that, android scans the files found int the /values directory by reading their content. For styles, every <style name="styleName" > ... </style> will be parser and a style object reference will be created.
Then, as Frank said, Yes. You can use whatever file name to write your custom styles.
When I have a source with this:
TextView localTV = (TextView)findViewById(2312345);
is there a way to kindly ask the compiler to transform it into the resource with its real name, like:
TextView localTV = (TextView)findViewById(R.id.mytext);
When I have a source with this
The most likely reason for having that code would be that the code was decompiled from an APK.
is there a way to kindly ask the compiler to transform it into the resource with its real name
No, for the simple reason that there may not be a resource corresponding with that number. As soon as you loaded this decompiled app into an IDE, all the resources would be assigned numbers that may or may not match the hardcoded values from the decompiled code.
You should open the layer.xml of the activity or the fragment you are working on and then find the textView and add it an id like this.
<TextView
android:id="#+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My local TV" />
this will create a variable in the R.java with an int that you will not have to handle by your self. you only call
TextView localTV = (TextView)findViewById(R.id.mytext);
But if you do not have Xml layout and it is created programaticaly, you should go to res/values/ids.xml (if you do not find ids.xml you can create it ) and add an ID like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="mytext" type="id" />
<item name="navBarText" type="id" />
<item name="navBarImg" type="id" />
</resources>
and when you create the textView programaticaly you set an Id for the TextView like this
menuLayout.setId(R.id.mytext);
and then you can call the TextView by using
TextView localTV = (TextView)findViewById(R.id.mytext);
just started programming android and I am trying to make a simple xml file but it seems it got some problem with the android:text=#string/..
I have got this error:
error: Error: No resource found that matches the given name (at 'text'
with value '#string/ false_button').
For each of the strings..
The code is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding= "24dp"
android:text="#string/question_text"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button"/>
</LinearLayout>
</LinearLayout>
You need to have the resources that you want to reference in your layout in strings.xml under res/values/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="true_button">True Button</string>
<string name="false_button">False Button</string>
</resources>
#string/true_button refers to a resource in strings.xml
You can also have hardcoded string but not recommended
android:text="True Button"/>
add following to your string.xml file under values folder
<string name="true_button">true</string>
<string name="false_button">false</string>
Make sure that you are created string value with the name of false_button in the string.xml
<string name="false_button">false</string>
or just give name within the quotes "" in the android:text attribute
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="false_button"/>
Click on res folder then click on values and then open strings.xml and add following content.
<string name="true_button">whatever text you want to write to your button</string>
<string name="false_button">whatever text you want to write to your button</string>
and save your file. Then clean your project by clicking Project -> clean and then run it again.
Hope this will help you.
make sure that in res/values/
you have all the resources present there as follows
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="true_button">True</string>
<string name="false_button">False</string>
</resources>
otherwisely you can hardcode values at yours layout where you specified button as
android:text="True "/> or android:text="False"/>
In your android project, look for the res directory. Open it and inside it look for values directory. In values, open strings.xml and check if it has a flase_button value stored in it . If not you can add a new value using name as false_button and value as what you want to display on the button.
When you open the strings.xml in IDE like eclipse, you can switch views of the .xml file by selecting proper tab given at the bottom of your eclipse screen. Select strings.xml view and
modify the current strings.xml to
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">app</string>
<string name="false_button">FALSE</string>
</resources>
Hope this was helpful.
You just need to add this in your Strings.xml class
just add <string name="false_button">False Button</string> line in your strings.xml
so that it will look like :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="false_button">False Button</string>
</resources>
All of these responses that refer to adding the string to the string.xml file make sense and work, but fail to address the original problem: this book ("Big Nerd Ranch: Android Programming") goes through these steps one at a time, mentions referencing the string in the layout XML file, but fails to mention adding to the strings.xml file. Fail.
I have the following xml code:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Button" <!--Warning -->
android:textSize="45dp" <!--Warning -->
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay" />
In the xml code i found two warning first that dp contains that which i got the waring to use sp indeed. What is the reason it showing so?
Second warning and may be error is that i am using android:text="Press Button" it tell me to use #string indeed. If i uses the same #string is displayed in text which look awkward. What is the reason for it!
Hardcoded String value in View is not recommeded by developer.android.com as making of Android Application compatible with different languages is twisted up to.
Referenced from
To add support for more languages, create additional values directories inside res/ that include a hyphen and the ISO country code at the end of the directory name. For example, values-es/ is the directory containing simple resourcess for the Locales with the language code "es". Android loads the appropriate resources according to the locale settings of the device at run time.
Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:
MyProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
Add the string values for each locale into the appropriate file.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
For example, the following are some different string resource files for different languages.
English (default locale), /values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources>
Spanish, /values-es/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mi Aplicación</string>
<string name="hello_world">Hola Mundo!</string>
</resources>
Referring to your OP:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="press">Press Button</string>
</resources>
This layout XML applies a string to a View:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/press"
android:textSize="45dp" <!--Warning -->
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay" />
This application code retrieves a string:
String string = getString(R.string.hello);
Use sp for setting size as suggested by developer.android.com
sp : Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
XML file saved at res/values/dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="font_size">16sp</dimen>
</resources>
This application code retrieves a dimension
Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);
This layout XML applies dimensions to attributes:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Button" <!--Warning -->
android:textSize="#dimen/font_size"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay"
/>
Recommended dimension type for text is "sp" for scaled-pixels (example: 15sp)
from the developer.android
Android stander TextView size is use SP and you are hardcode String that i give warning.
Please User your String.xml in value folder and select this String then it do not give any error.
Thanks
It's better to use SP instead of DP.
It's recommend to use always string resources file, because if you need to change a single #String used in multiples xml files, you have to change only one time. Vice-versa, if you write your strings inside the xml layout files, if you need to change a string you need to search for the string, search for the xml file and then change as many occurrances you need.
In conclusion, it is not a good practice to hard code strings. You should specifies them to a string resource file and then reference them in your layout.This allows you to update every occurrence of a single word in all layouts at the same time by just editing your strings.xml file.
It is also necessary for supporting multiple languages definitions as a separate strings.xml One file for one language!
I am a super newbie to Android Development and wanted to start slow with an Icon Theme for 3rd party launchers. I was wondering if there is any way to add an image (from inside my "drawable" folder) to a string inside "strings.xml"
This is what I have:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Theme properties -->
<bool name="enableIconPack">true</bool>
<bool name="enableDockPack">false</bool>
<string name="authorName">Clay Cauley</string>
<string name="developerName">Clay Cauley</string>
<string name="authorLink">MY SITE</string>
<string name="theme_title">MY TITLE</string>
<string name="theme_description">This is some text explaining who created the theme and why, etc ... This is also where I would like my logo to appear.</string>
</resources>
My problem lies in the last string, "theme_description" --- If it's possible I wanted to get my logo in there so people see it when on this screen:
Hopefully wanting to get it where the "Hey" currently is.
I've tried 2-3 different approaches but they were all guesses and none worked so I thought I would try here. Any help is greatly appreciated, even if it's telling me it isn't possible :)
This is my "main.xml"
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="65dp"
android:gravity="center"
android:horizontalSpacing="1dp"
android:listSelector="#000000"
android:background="#000000"
android:numColumns="3"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="-1dp" >
</GridView>
Thanks!
What you probably want to do is to add an image to a TextView. There are attributes for that: "android:drawableLeft", "android:drawableRight", "android:drawableStart", etc.
<TextView android:drawableLeft="#drawable/my_drawable" .../>
If you want to define it in a resource file, then do it in styles.xml:
<style name="my_text_view_with_drawable">
<item name="android:drawableLeft">#drawable/my_drawable</item>
</style>
<TextView style="#style/my_text_view_with_drawable" .../>
I think the original poster https://stackoverflow.com/users/1344453/clay-cauley is asking how would you specify an image to be inserted along with the text from a string in string.xml
Since you can specify links and email inside a string, it would be nice to reference a drawable.
Asked differently how would you do something a la html img tag, something like this :)
<img src="#drawable/icon" >
I too am interested in this possibility.
I know how to dedicate the layout with an ImageView and load a bitmap of choice via code.
I think the problem is trying to use TextView (it probably is not designed to display images, how about if the layout has a WebView control? Could a string resource reference a bitmap in the drawables, in local storage?
Thank you.
You do this with a kind of layout, like linearlayout or relativelayout.