I have some problem with setting params to my view in xml file with values from dimen files.
For example when i'm adding layout_height param to my EditText in laytout xml file:
<EditText
....
android:layout_height="#dimen/et_height"
....
/>
File dimens:
<resources>
.........
<dimen name="example">16dp</dimen>
<dimen name="et_height">20dp</dimen>
.........
</resources>
It works fine. But sometimes when i open this file again AndroidStudio replace #dimen/et_height with value 20dp in layout xml file. And i must change it again to #dimen/et_height.
How can i fix this ?
This is the normal case:
But in my case AndroidStudio replaces with value:
Having the same issue. Thought it is the ConstraintLayout but the issue also appears in every other layout though..
related post from me :
Android ConstraintLayout #dimens replaced with hardcoded values
Also some other user said it just shows the dp value, i am completely in your situation i know the grey text, but it does really replace the value..
Related
I have dozens of ImageViews in my xml file and defined the length in strings.xml such as string name="cardWidth">100dp</string> and then had content_main.xml use that string in the width. Like android:layout_width="#string/cardWidth" that way I could just change the value once and see how all the images displayed.
No problems with the compile but got a run time error
Binary XML file line #XX:
You must supply a layout_width attribute.
I researched and I saw in S.O. that this will occur when you use a string in the value. I'll probably just hard code the value once I'm happy with it but is there a way around this?
Thanks!
Sorry for the formatting, it's my first question here!
Your dimension values need to be defined as (possibly in a file res/values/dimens.xml):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="cardWidth">100dp</dimen>
</resources>
Then, you can access them using:
android:layout_width="#dimen/cardWidth"
use these width with dimensions in values folder not with string.xml like below :
<dimen name="cardWidth">16dp</dimen>
Currently, am setting the background of a TextView using code like this:
textView.setBackgroundResource(R.drawable.rect_with_border_grey);
I then came to know about dimens.xml usage and how you can set through that file. Is it possible to set the background through ? i.e. I want to do the above code line through XML. Any help please?
I wouldn't do this using layout xml file as #Opiatefuchs pointed out (in the comment below). B'cos, this textview's background will change depending on the user setting in the App dynamically.
Make a style like
<style name="MyTextStyle">
Do everything you want to do with your TextView here.
</style>
and then assign that style to your textview in xml like this
style="#style/MyTextStyle"
It will work.
dimen.xml is only use for giving dimension only. for setting background you need to do it with either layout xml file or from java.
Its up-to the requirement of setting background.
I finally found that you cannot set the background of a TextView via element (i.e. XML) in Android. You need to use layout XML. But, in my case, I cannot use layout since the background will change dynamically based on user choice in the App.
create dimens.xml file like this:
<resources>
<dimen name="paddingTop">10dp</dimen>
<dimen name="paddingRight">20dp</dimen>
...
</resources>
then use it in your layout xml files like this:
android:layout_marginTop="#dimen/paddingTop"
android:layout_marginTop="#dimen/paddingRight"
I'm trying to have an image which preserves its size in all the resolution except one... what i would like is to use the "wrap_content" attribute for height and override it with a dimen attribute but just for a particular case (like values-xlarge). I did like this:
<ImageView android:id="#id/image_frame"
android:layout_width="match_parent"
android:layout_height="#dimen/overlayImageHeight" />
But of course it crashes if i don't define the overlayImageHeight in all the dimen.xml files.
I tried to put a "wrap_content" string for other dimen.xml files but again it fails since dimen files just accept numeric values inside.
Is there a way to define this behaviour inside an xml file? Other way to do it programmatically?
Any help appreciated!
You should give it a style, define the style in res/values/styles.xml, then make a different version of the style in res/values-xhdpi/styles.xml.
I have an XML file with 5 textviews, all with the same textSize. Currently, I define the size like this:
android:textSize="30dp"
However, I want a quick way to apply something like:
android:textSize="text_size"
Where textSize would be a variable I define somewhere, and can use wherever I want in my XML file. I tried defining these variables in the strings.xml file in the Android project, and calling them like this:
android:textSize="#string/text_size"
Where it would be defined like this in strings.xml
<string name="text_size">30dp</string>
It worked fine in Ecplise itself, and showed just fine in the Graphic Layout. But when I tried running the app on my phone, it crashed.
So, is there a way to define variables in my XML layout files? Thanks.
Your idea of putting it in strings.xml is close, but those values need to be set in a dimensions XML file. The documentation is here.
Using your example, you might make a file called res/values/my_dimens.xml. Then put in there
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_text_size">30dp</dimen>
</resources>
Then to use it in your layout, just set
android:textSize="#dimen/my_text_size"
I am teaching myself Android using Eclipse, the Android plug-in, and Sams "Teach Yourself Android Development" book. I have this weird little problem. I've been able to create xml files in the res/values directory that hold strings and color values (colors.xml and strings.xml). I have been able to reference these values in the properties of my Android screens (the xml in res/layout), for example setting the "Text" and "Text color" properties with references like "#string/topTitle" and "#color/titleColor," where topTitle and titleColor are defined in the xml files.
BUT: when I create a file called "dimens.xml" and have font sizes in it, Eclipse correctly puts this file in res/values, but when I try to reference these values, e.g. "#dimension/titleFont" I get an error "No resource found that matches the given name." I've tried lots of different names, I've tried "#dimens" instead of the type, still nothing. If I go into the layout xml file and set it explicitly to a font size, e.g. 22pt, it works.
So Eclipse recognized my "dimens.xml" file when I made it well enough to put it in res/values, and lets me edit it, and shows it full of (dimension) values. It just doesn't recognize my referring to it in other xml files.
The book I'm using doesn't actually show a dimension example so I must be doing something wrong. I checked the Android docs but couldn't see any problem.
Any help appreciated. Thanks.
The correct way to refer to a dimension variable (stored in your dimens.xml (don't think the name here really matters though, it's what's inside that does)) from another xml file is like this:
"#dimen/nameOfVariable"
Notice that it is neither dimension, dimensions or dimens, but dimen!
If you look inside your xml file where you have your values, this will make sense as dimen is the name of the xml elements storing dimension values:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<dimen name="someDimension">5dp</dimen>
<dimen name="anotherDimension">10dp</dimen>
</resources>