usage of resources in layout - android

I don't want to specify hard coded test size values in my layout.xml, hence i am using the following specification :
android:layout_height="#integer/intervalViewHt"
and
#integer/intervalViewHt value is as follows:
<integer name="medium">15</integer>
Now, the while inflating android is creating a problem saying that it cannot inflate the view. I want to actually specify value in dp so the actual value should be like
android:layout_height="15dp"
Can anyone help me here ?

What you're looking for are "dimensions" rather than plain integers.
Declaration:
<dimen name="intervalViewHt">15dp</dimen>
Usage:
android:layout_height="#dimen/intervalViewHt"
Have a look at the given link for more examples in the Android docs.

I use something like this for text size across different devices. It's java based, not xml.
tvOutput.setTextSize(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));
I haven't tested the code so you might have to play with it. I'm sure you could do something like.
button1.setHeight(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));
Though again I haven't tried it and you may need something different then TypedValue

What if you give it a String rather than an int. i.e in strings resource you have a string "15dp" that you reference

Related

Use Strings with placeholder in XML layout android

I have a string with placeholder e.g
<string name="str_1">Hello %s</string>
I want to use this in xml layout as android:text="#string/str_1". Is there any way to use this in xml layout to fill the placeholder?
Thanks in advance. I already know String.format(str,str...) in java/kotlin but i want to use this in xml layout without data binding.
You can use something like this.
Write your string in your (strings.xml) with declaring a string variable (%1$s) inside it. For decimal, we use (%2$d).
<string name="my_string">My string name is %1$s</string>
And inside the android code (yourFile.java), use this string where you want it.
String.format(getResources().getString(R.string.my_string), stringName);
This is not a good answer but it may help you get some idea to get going.
Thanks.
It's not possible to format your strings directly in your XML. Inside your code you can use both String.format and context.getString() to get and format your string.
If you want to show something just in the XML and not have it in the final build (only have it in compile time), you can also use tools: namespace like following:
<TextView
...
tools:text="Hello, this is a test"
/>
This will only appear in the layout editor and will not have any effect in the APK. It can also be used for other fields too, like tools:visiblity.

Reference another dimen in dimen

Is it possible to use the reference of a dimension resource in another dimension? What i mean is something like this:
File dimen.xml:
<dimen name="test1">18sp</dimen>
<dimen name="test2">#dimen/test1</dimen>
It works the way i posted
<dimen name="test1">18sp</dimen>
<dimen name="test2">#dimen/test1</dimen>
I would suggest to consider one more thing when using this.
I get what you said in your comment, but either there is a link between the two values or there is not and you should distinguish between these two possibilities.
If there is no link between test1 and test2 and they are independent and the fact that they hold the same value is mere coincidence, then you shouldnt suggest a link between the two. Instead just set the same value twice.
Why do you want create a second dimension with the same value as first ?
Don't create second dimension, but use first !

Can I use variables in XML for Android?

Instead of using this following line for each textview:
android:textSize="40sp"
Am I able to replace 40sp with a variable?
As a side question, I've been recommended to use SP units as it goes by user defined settings. Is this a best practice?
Am I able to replace 40sp with a variable?
You can either use a dimension resource (e.g., android:textSize="#dimen/someSize"), or use a style that does this (per AlexN's answer), or set the size at runtime via setTextSize().
I've been recommended to use SP units as it goes by user defined settings. Is this a best practice?
On newer versions of Android, the user can change the base font size via the Settings application. By using sp for units, your font sizes will scale along with the base font size. If you use other units (e.g., dp), your font size will remain the same regardless of what the user has in Settings. It is impossible to say whether any given use of sp is a "best practice" or not -- I am sure that there are cases where allowing the font size to change would be a bad thing. By default, though, sp is probably the right starting point.
As I know, using SP is a really good practice for Text Size.
And according to your first question - I think we cannot use variable, however, there is a better way. Check it out - http://developer.android.com/guide/topics/ui/themes.html
So if you'll define a style, you can declare your Views in XML like <TextView android:id = "#+id/myId" style = "#style/myStyle"/>
And this style will incapsulate all parameters you want to set to your textViews on that screen.
No, it's not possible to use code based variables in XML files. However, you can use styles for this.
Example:
<style name="MyTvStyle">
<item name="android:textSize">40sp</item>
</style>
Then apply it like this:
<TextView style="#style/MyTvStyle" ... />
A code based approach is also possible. If the TextViews have their android:id attribute defined you can retrieve them in the code with the findViewById method.
Example:
int[] id_array = {
R.id.textview1, R.id.textview2, R.id.textview3 //Put all the id's of your textviews here
}
for(int i : id_array) { //Loop trough all the id's and retrieve the Textview associated with it.
TextView textview = (TextView)findViewById(i);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,40); //Set the text size to 40sp
}
And yes it's always better to use sp instead of normal pixel values. Since sp will scale with device size and user settings.

When should you use `#+id` instead of `#id`?

I have a bunch of Views in a <merge>, and I included that <merge> into a RelativeLayout. I try to refer to the IDs of those included Views to act as anchors for my other Views, but Eclipse complains that the IDs are not resolving. I found a workaround by using #+id rather than #id when I first refer to them rather than when I actually define the objects they refer to. I've already defined the two IDs in a Style and in the included <merge> where they are declared, so it feels a bit inefficient if I keep repeating the definition of the ID.
Is this the correct way of doing it? I'm assuming it's bad cause the '+' is another initialization. My current hypothesis is that you should use #+id when you first use the ID rather than when you initialize the object that the ID is going to represent, a bit like C/C++ and how they require at least a function prototype in the lines prior to the actual code that uses the function.
Another question I have is when you use the GUI-based UI builder of Eclipse, I noticed that they always use #+id rather than #id. Is this acceptable, cause it seems inefficient to me; it's as if the application will be spending more time determining whether or not the ID has been declared in R.id.
Using #+id format tells the Android asset compiler to assign an ID to your element, it isn't actually an id itself. So if I use #+id/myNewId the asset compiler will create a new id named myNewId and provide a number for it. The actual number can be accessed from your code as R.id.myNewId.
If you use an #id, the compiler will look for R.id.id. You can define your own id's in XML files, as explained here: http://developer.android.com/guide/topics/resources/more-resources.html#Id. You could create your own file in res/values/[your_filename].xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="id_name" />
</resources>
and then refer to #id_name, for e.g.
You can also use the Id's defined in the Android namespace: #android:id/empty
This is well explained in the Android documentation: http://developer.android.com/guide/topics/ui/declaring-layout.html#id
There's also some further discussion here: android:id what is the plus sign for

Using resource values as layout attributes (config.xml)

Looking in the android sdk folders, I've found a file called values/config.xml. This seems to be somewhere that you can define values for later use in layouts and animations.
Given the config.xml:
<resources>
<string name="config_somePadding">50dip</string>
</resources>
How would I reference this to use as the layout_height in a layout xml file?
#string/config_somePadding is actually the only one I've found that doesn't throw an error in Eclipse (even though there isn't a config_somePadding in values/strings.xml), but it appears to just put an empty string.
In the sdk, they use an integer for animation duration. They reference it like this: android:duration="#android:integer/config_longAnimTime". Is there a way to use values that aren't integers in the layout_height attribute?
How would I reference this to use as
the layout_height in a layout xml
file?
You wouldn't. Use a dimension resource instead.
Is there a way to use values that
aren't integers in the layout_height
attribute?
You have to use a dimension resource for dimensions.

Categories

Resources