Using layer-list to display some drawable images - android

Android studio 2.0 Preview 3b
Hello,
I have created the following layout that I want to use for a background for my app. I am using the layer-list and I want to display a bowl of peas in 2 locations. Everything looks ok in the preview, but when I run on genymotion or some cheap Chinese devices the image stretches across the screen. However, on the Android AVD, everything looks fine and on my Nexus 5 (real device) everything works ok.
This is what I want and this is how it's displayed in the AVD and Nexus 5. As you can see there is no problem.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:centerX="0.5"
android:centerY="0.3"
android:endColor="#08e25b"
android:gradientRadius="300dp"
android:startColor="#b7e9c9"
android:type="radial" />
</shape>
</item>
<item
android:width="48dp"
android:height="48dp"
android:left="350dp"
android:top="400dp">
<bitmap android:src="#drawable/peas" />
</item>
<item
android:width="68dp"
android:height="68dp"
android:drawable="#drawable/peas"
android:left="-20dp"
android:top="480dp" />
</layer-list>
I have placed peas.png file in drawable-nodpi and just add the width and height in the layer-list
And when I run on the genymotion and some cheap smart devices I get the following:
Just quick summary.
Nexus 5 real device and AVD devices works ok
Genymotion and cheap smart devices doesn't display correctly
I am just confused in what I should believe. I have tried to use the bitmap as well to see if that would make any difference.
Many thanks for any suggestions.

Update your layer-list as follows
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:centerX="0.5"
android:centerY="0.1"
android:endColor="#08e25b"
android:gradientRadius="300dp"
android:startColor="#b7e9c9"
android:type="radial" />
</shape>
</item>
<item
android:width="48dp"
android:height="48dp"
android:bottom="68dp"
android:right="-20dp">
<bitmap
android:gravity="bottom|right"
android:src="#drawable/peas" />
</item>
<item
android:width="68dp"
android:height="68dp"
android:bottom="-20dp"
android:left="-20dp">
<bitmap
android:gravity="bottom|left"
android:src="#drawable/peas" />
</item>
</layer-list>

Place your images in all density folders (xxhdpi, xhdpi, hdpi).
The system picks image resources based on screen resolution.

Related

<layer-list> not looking same on different screen densities

I am applying my own background on edittext to achieve same solid color of underline without changing it's thickness on focus.
Next to my xml file I can see in brackets it's specified as xhdpi (see image attached)
When i run it on different density device the underline doesn't look perfect.
Is it because the xml file is specified as xhdpi? What does it mean? How to make it scale perfectly on all densities?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="1dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape android:shape="rectangle" >
<stroke
android:width="1px"
android:color="#color/purple" />
<solid android:color="#00FFFFFF" />
<padding
android:bottom="5dp"
android:top="5dp" />
</shape>
</item>
</layer-list>
Please read Android Developers: Support different pixel densities
1. Use one resource for all densities
Select the file and press F6 (move file)
Then delete -xhdpi from the To directory field and click on OK
2. Create resource for each screen density
You will need to create other XML files with that same name and place them in the appropriate folder while modifying android:width and maybe padding to suit your taste

Background different than XML preview

I have a drawable that creates a gradient in the background:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="radial"
android:gradientRadius="75%p"
android:startColor="#252A32"
android:endColor="#0C0D10"
/>
</shape>
However it looks completely off when running on an emulator:
XML preview:
On emulator:
Why is it showing that ugly circle display?
it may be because of different screen density. Emulator and AS showing differently best you can do is trying on an actual device.

Confused about the Gradient Display different in Nexus 7 ( 2012 Model) and Other phones

This is my Gradient drawable xml file and expected output for nexus 7 but the gradient is not correct ( Linings between shades )!
It look like this
Correct Output is in phone.
Is there any pixel format definition?
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="45"
android:endColor="#00a484"
android:startColor="#00407e"
android:type="linear" />
</shape>

Android screen resolution 540px x 960px drawable folder issue

My code has 5 drawable folders (drawable, drawable-mdpi, drawable-hdpi, drawable-xhdpi, drawable-sw330dp). Each one is used for different screen densities. I have tested each resolution and adjusted accordingly. The only problem so far is that when I test the application on a Droid RAZR, my shapes are drawn incorrectly. The screen size for that particular phone is 540px x 960px.
I have tried three different drawable folder names:
1) drawable-sw330dp
2) drawable-sw540dp
3) drawable-sw225dp << This was suggested by #CommonsWare below
None are adjusting to the xml files that are stored within. It seems like the phone is using the drawable-hdpi instead of going into the specific (sw) folder. Is there another way to make sure that the phone references the right folder?
Example xml file that draws the shapes (used as a background):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/menu_active" />
<padding android:left="1dp" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="135"
android:pivotX="96.5%"
android:pivotY="82%"
android:toDegrees="45" >
<shape android:shape="rectangle" >
<solid android:color="#000" />
<padding android:left="1dp" />
</shape>
</rotate>
</item>
The DROID RAZR's screen density should be ~256ppi. Motorola probably put it in the -hdpi density bucket as a result. The shortest width, therefore, would be 225dp, which is smaller than any of your -swNNNdp resource sets.
It looks like drawable-w330dp-normal works for the Droid RAZR. Just create the folder and store the modified drawable there.
Put image resources for MOtorola RAZR in folder: drawable-hdpi
Android supported next formats for drawable assets:
1)drawable-ldpi
2) drawable-mdpi
3)drawable-hdpi
4)drawable-xhdpi
5)drawable-xxhdpi

Padding issue on Nexus 7 tablet

I'm noticing some issues with padding on the Nexus 7 tablet running Android 4.1. Specifically, I have a background drawable with padding that creates a outer stroke:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#59000000" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
This is supplemented with a 1dp white inner stroke. So it should be a square 2 dp border around the entire image:
I have tested it on the actual nexus device and on several other devices and the 7 inch jelly bean emulator:
First, here is the working version from the 4.1 7 inch emulator:
The border around the outside is even and goes around the entire image. This is how it works on all the other devices i have as well.
Nexus 7:
The gray is the background color, but you can see the right and bottom padding is not being respected. The border is being obscured by the the image now.
If it matters, these are children of a GridView. Wonder if anyone has seen these issues or has ideas on how to resolve them.
This appears to be a bug when generating the stroke in the XML drawable. After multiple attempts, I've narrowed it down to that; it seems to be making the stroke size incorrectly on the lower and right sides of the shape, even if specified in pixels. If I simply change the stroke width to 2dip instead of 1dip (all other things staying as they are), this is the result:
This is the border.xml I used for the background of the after image:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#59000000"/>
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp"/>
</shape>
Which, again, is just yours with the stroke width enlarged. The good thing is, this won't change the expected result at all since the padding remains the same. In fact, you can change the stroke tag to solid and still have the same result (however, if your images contain transparency you'd see the solid background in the transparent areas).
Give this a try; let me know how it works for you!

Categories

Resources