Error by including drawable xml-file - android

Hello community,
I want to iclude different drawable xml-files into my activity_main.xml
However,when I want to includ following code :
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<size
android:layout_height="110dp" />
</shape>
</item>
</selector>
the name is button_height.xml into my main-file:
<Button
android:id="#+id/cmda1"
android:layout_width="110dp"
android:layout_height="#drawable/button_height" />
It does not work and Eclipse says only:
You must supply a layout_height attribute.
and
error in an XML file: aborting build.
I searched an answer in the internet but dont found one.
But I think its an easy mistake lots of people are doing.
so, I hope for usefull answers
and sorry for my awful english.

Your whole idea of drawable, selector and dimension is messed up. Can't put height in selector and also selectors (which are drawable) as height.
Do this.
If you want to use xml to get height
Add a file in res folder with name res/values/dimens.xml and add this line in that
<resources>
<integer name="btn_cmda_height" >110dp</integer>
</resources>
and change the button cmda1 height property as
android:layout_height="#dimen/btn_cmda_height"
Make it dynamic for various screen sizes by creating these files which will hold different values. Although this is the old way.
Read more about new way to do this here
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
If you don't want to use xml to get height
just do this android:layout_height="110dp" in the button cmda1 height property
NOTE:
Your selector code is wrong and not required at all. Selectors are used to define the background state of a view

Related

Why Android layer-list drawable so intelligent?

I can't dig deep into how android implements its layer-list drawable. But I find it interesting and I can hardly know why this happens.
Here are some drawables:
the nine-patch xml
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/cam2tst_ripple_bg_img">
</nine-patch>
the shape xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<padding
android:left="#dimen/cam2tst_ripple_horizontal_padding"
android:top="#dimen/cam2tst_ripple_vertical_padding"
android:right="#dimen/cam2tst_ripple_horizontal_padding"
android:bottom="#dimen/cam2tst_ripple_vertical_padding" />
<solid android:color="#android:color/holo_green_dark" />
</shape>
the ripple xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/holo_green_light">
<item android:drawable="#drawable/cam2tst_ripple_shape"></item>
</ripple>
the layer-list containing all above
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/cam2tst_ripple_bg_img" />
<item android:drawable="#drawable/cam2tst_ripple_base" />
</layer-list>
Unfortainately I still can't get my screenshot thing work on my L preview, but I can describe it.
What I get is the shape (which i obviously didn't set its size explicitly) doesn't cover over the whole nine-patch! The un-streched part of the nine-patch is magically considered as some kind of "auto padding thing". What I was expecting (ok I was expecting exactly what android has done for me, I mean what I was... supposing...) is something not so positive: the not-particularly-sized shape drawable covering the entire nine-patch just as if the latter is a normal png.
But the shape does magically avoid the un-stretched part of the nine-patch and overlays only above the streched-part of the nine-patch.
This is awesome...but confusing, why? I may not able to dig that deep into the source but this do sounds anti-intuition (but nice). I want to know the reason though. So I post this here.
Since I tagged this as android-L because I am working on one. But I think this shall be working from something like gingerbread.(just to replace the ripple drawable with something else, maybe a inset drawable etc.)
This effect is caused by the combination of two things:
All nine-patch drawables have a padding area defined automatically from the edges of the content area. The content area can be defined either explicitly, using the right and bottom lines at the border, or implicitly from the stretchable area defined by the left and top lines.
Layer-list applies the padding on each layer cumulatively to the next layer by default*, effectively treating each layer as the content of the previous layer.
* Lollipop has introduced a new attribute for disabling this behavior.

Hiding views declaratively based on screen size in Android

In android xml:ish
Is there any way to change a visibility attribute based on the layout size/orientation in the xml directly?
I have a button in a fragment that should be visible for small screens sizes only. On larger sizes, let's say layout-large, I want it to be hidden.
Sure, I can write code for this without any problem but for academic reasons I would like to know it it's possible to do something like this.
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="<magic expression here>" />
Thanks
// Johan
This answer is based off the explanation provided here by Flávio Faria.
The visible, gone, etc can be values mapped to their corresponding enum values in a string resource - which means you can create a visibilty.xml with string resources for each layout you want, and Android will automatically resolve the one you want for each screen type.
I'd recommend the following:
/res/values/visibilty.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Enum values pulled from Android source -->
<string name="visibility_visible">0</string>
<string name="visibility_invisible">1</string>
<string name="visibility_gone">2</string>
<string name="product_info_footer_button_visibility">#string/visibility_visible</string>
</resources>
/res/values-large/visibilty.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="product_info_footer_button_visibility">#string/visibility_invisible</string>
</resources>
And then you can reference the visibility as follows for your button:
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="#string/product_info_footer_button_visibility" />
Warning: This depends on the device having the same enum values for visibility (0/1/2) as defined in the AOSP source. Device manufacturers and custom ROM creators can change these values, in which case this code will likely not work as desired.
The android:visibility attribute is an int (like many attributes) so you can do the following :
Define a resource file named visibility.xml in values-port and values-land resource directories. The content of this file is like this :
values-port/visibility.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="buttonvisibility">0</integer> <!-- 0 is the value for android:visible -->
</resources>
values-land/visibility.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="buttonvisibility">1</integer> <!-- 1 is the value for android:invisible -->
</resources>
and in your layout.xml :
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="#integer/buttonvisibility" />
It works : btn_check_availability is visible in portrait and invisible in landscape.
Note : this example use layout orientation as discriminator, but you can of course do it with any resource qualifier (like dimension, density, ...)
There is no magic expressions available in XML. If only.
There are two approaches to this problem:
a/ use the drawable folder system. Drawable folders can be copied and named to be DPI aware following the conventions dictated here: Supporting Multiple Screens.
b/ Do it programmatically. On runtime check for screen DPI and show/hide view accordingly.
Have you looked at using includes and multiple layouts organized into the appropriate size/orientation layout folders? Some layouts could either simply not have the button or have it hidden by default.
Re-using Layouts with include
Providing Alternative Resources

What is the different between the folder "drawable" and the file "drawable.xml"?

In the activity_main.xml, there are two rows code android:background="#drawable/border_ui" and android:background="#drawable/my",
but the one use the border_ui.xml located the folder res\drawable, and the other use the file drawable.xml located the folder res\values.
What different? Thanks!
And more, an error will occur if I remove the file drawable.xml to the folder res\drawable and rename it as my.xml.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#drawable/border_ui"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:background="#drawable/my"
/>
</RelativeLayout>
border_ui.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="1dp" android:color="#000000" />
<solid android:color="#ffffff" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
drawable.xml
<resources>
<drawable name="my">#f00</drawable>
</resources>
The difference is one, the border, is involving a shape which is a drawable entity. The other, the values resource, is referencing a color which is acceptable as a drawable as shown in a note here - like so =>
Note: A color resource can also be used as a drawable in XML. For
example, when creating a state list drawable, you can reference a
color resource for the android:drawable attribute
(android:drawable="#color/green").
I would imagine the background in question looks something like this. (#f00 represents a websafe color)
Edit:
Please see this article about color drawables.
What it says, and what the Android Drawable Resources shows, is that files used for specific purposes like any of the drawable types shown require certain tags to be the root. Just removing the <resource> tag as has been suggested is not going to work. If you're wanting to force the color drawable into another resource folder other than where it belongs (values), which I can't really see why, then you're going to have to hack it.
Final word:
You asked why they're in different places and why you can't just rename and move it into another folder of your choosing - both of which have been answered. I believe if you're expecting a solution on moving the file to the drawable folder then another question asking such would be in order. Please consider upvoting or accepting an informative answer.
You can change the name of the file, but don't move it to the drawable folder. Keep it where it is currently, and it should work fine. drawable.xml is not a keyword. But you can't have this file, which contains a list of resources in the drawable folder.
Since your drawable is a resource, it should be inside the values folder instead of the drawable folder.
it's from android documention about String but it's can explain about :
Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). So, you can combine string resources with other simple resources in the one XML file, under one element.
the syntax of drawable is without element but regular xml file
In fact, the drawable.xml file is a file containing resources, but not only drawables.
So, if you want to move it to the folder drawable and rename it my.xml, it is ok. You should just remove the <resources> & </resources> tags and it should work.

dimension resource cannot be found in Xamarin for Android

In a drawable XML file, I want to use a dimension resource "background_radius".
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="radial"
android:startColor="#FFFFFF"
android:endColor="#00000"
android:gradientRadius="#dimen/background_radius"
/>
</shape>
The dimension resource is defined in Values\Dimens.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<dimen name="background_radius">400dp</dimen>
</resources>
I got the following error when I build it.
No resource found that matches the given name (at 'gradientRadius' with value '#dimen/background_radius'). ...\Resources\drawable\background.xml
But when I design it in VS, I can see the correct background radius. It seems VS just cannot build it.
What's wrong?
Thanks.
+++++++++++++++++++++++++
For some unknown reason, the project did not pick up the new dimens xml file. I re-created the files and rebuild it. Now the dimen can be found.
In addition, the radial gradient does not like the dimen value with dp. so I update the dimen resource to:
<item name="background_radius" format="float" type="dimen">400</item>
Give your dimension a dimension unit (dp, sp, etc.)
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp.
So, change it to:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<dimen name="background_radius">400dp</dimen>
</resources>
for example.
I came across a similar thing; created a Colors.xml file and everything was as it should be but the resource could not be found. After a while I forgot about it and found that it had finally been picked up. Today made a Dimens.xml and the exact same thing happened.
I had to restart Visual Studio in order for it to be picked up.

rollover android

I want to make a rollover with android.
I saw that it is necesarry to write a XML like :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/verde" />
<item android:drawable="#drawable/rojo" />
</selector>
I saved it with the name button.xml.
I don't know in which folder save it.
Then I tried to used the reference in my layout.xml
I change the background attribute for my button in my layout.xml from
android:background="#drawable/red"
and use the
android:background="#drawable/button"
Save button.xml in the folder res/drawable if it's applicable for all screen sizes/devices, otherwise put it in the folder with the appropriate qualifier suffix (e.g. drawable-hdpi, drawable-ldpi etc).
See here (http://developer.android.com) for a full example, and explanation of other states you could include.

Categories

Resources