This is the drawable file :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
<item>
<color android:color="#ffffffff"/>
</item>
<item>
<bitmap android:gravity="top|center_horizontal"
android:src="#drawable/splash_screen"/>
</item>
</layer-list>
I want the bitmap to have 20px margin-top, I didn't find how to set margin-top to bitmaps element
You can try with android:top.
The top offset in pixels.
<item
android:top="20dp">
<bitmap android:gravity="top|center_horizontal"
android:src="#drawable/splash_screen"/>
</item>
Related
I need to set size for item in layer-list. For using as windowBackground in launch activity. I wrote this XML file and Android Studio displays it correctly. But in application the logo always full screen. How to make it?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:endColor="#000004"
android:gradientRadius="1000"
android:startColor="#1f3371"
android:type="radial" >
</gradient>
</shape>
</item>
<item>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/texture_5"
android:tileMode="repeat" />
</item>
<item android:drawable="#drawable/logo"
android:height="100dp"
android:width="77dp"
android:gravity="center"
>
</item>
</layer-list>
I found solution, but I expected better way.
Here is final code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:endColor="#000004"
android:gradientRadius="1000"
android:startColor="#1f3371"
android:type="radial" >
</gradient>
</shape>
</item>
<item>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/texture_5"
android:tileMode="repeat" />
</item>
<item android:drawable="#drawable/logo"
android:bottom="214dp"
android:top="214dp"
android:left="100dp"
android:right="100dp"
android:gravity="center"
android:tileMode="repeat"
>
</item>
</layer-list>
At least it works at xxhdpi and xhdpi without distortion.
Starting from API 23, it is possible to set width and height right within the "item" tag:
<item android:drawable="#drawable/logo" android:width="..." android:height="..." />
You can do the followin
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/blanco"/>
<item
android:width="200dp"
android:height="200dp"
android:gravity="center">
<bitmap
android:gravity="fill"
android:src="#drawable/your_image"
android:mipMap="true"/>
</item>
</layer-list>
Just change width and height
I had the same problem and found the solution here.
I had to use my vector drawable as the source to a bitmap object:
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_logo_splash"/>
</item>
I Have read a few tutorials but all I was able to
achieve was a border in one side, or both sides with
the same color.
I'm trying to create a style that will apply a border
in top with a different color and width, than the bottom border.
So I want to have a border with 2dp on top with a blue color
and a border of 3dp on the bottom with red color.
This is the style I'm using to apply border on top and bottom
but I can't change the color on the top or the bottom one.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#FF000000" />
<solid android:color="#FFDDDDDD" />
</shape>
</item>
<item android:top="1dp" android:bottom="1dp">
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#FFDDDDDD" />
<solid android:color="#00000000" />
</shape>
</item>
</layer-list>
It is little dirty but it works :).
Your Layer-List drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- TOP STROKE-->
<item>
<shape android:shape="rectangle">
<solid android:color="#color/top_stroke_color" />
</shape>
</item>
<!-- BOTTOM STROKE-->
<item android:top="#dimen/top_stroke_width">
<shape android:shape="rectangle">
<solid android:color="#color/bottom_stroke_color" />
</shape>
</item>
<!-- MAIN SHAPE -->
<item android:top="#dimen/top_stroke_width" android:bottom="#dimen/bottom_stroke_width">
<shape android:shape="rectangle">
<solid android:color="#color/main" />
</shape>
</item>
</layer-list>
Color definitions:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="top_stroke_color">#0000FF</color>
<color name="bottom_stroke_color">#FF0000</color>
<color name="main">#00FF00</color>
</resources>
And finally dimens:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="top_stroke_width">10dp</dimen>
<dimen name="bottom_stroke_width">20dp</dimen>
</resources>
In my example i have 3 rectangles which have set correct "Margins". Every of my rectangles is smaller than this under and covers it. According to my solution u can create 4 different stroke for every side of your main shape.
I'm going to create a drawable like below image that is included an arrow image.
When I use layer-list , arrow image appear in middle of my drawable!!!
Please let me know how can I achieve it ...
UPDATE:
This is my Layer-List:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle">
<corners android:radius="5dp"/>
<solid android:color="#d5d5d5"/>
<stroke android:width="1dp" android:color="#242425"/>
</shape>
</item>
<item android:drawable="#drawable/ic_arrow"></item>
Make a drawable resource file set bitmap as the root element, then set the android:gravity="left" and use this bitmap file as drawable in your layer-list item.
<?xml version="1.0" encoding="utf-8"?>
<bitmap
android:gravity="left"
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ic_launcher"/>
layer-list:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#color/blue" />
<solid android:color="#color/silver" />
</shape>
</item>
<item android:drawable="#drawable/stack" />
</layer-list>
Use case:
If you have multiple buttons to design and you want to have different images in them and a background gradient.
what I have now
ImageButton with background the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" >
<layer-list>
<item>
<shape android:shape="rectangle">
<gradient
android:type="linear"
android:startColor="#color/green_yellow"
android:endColor="#color/green_mantis" />
</shape>
</item>
<item>
<bitmap
android:antialias="true"
android:gravity="center"
android:src="#drawable/more_button_check_rates_img"/>
</item>
</layer-list>
</item>
</selector>
This is working, but now i want another one with a other bitmap.
Question
Can i create three files (1 shape and two buttons) and then include the shape into the two buttons.
something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" >
<layer-list>
<include shape="background"> <!-- this line -->
<item>
<bitmap
android:antialias="true"
android:gravity="center"
android:src="#drawable/more_button_check_rates_img"/>
</item>
</layer-list>
</item>
</selector>
yes you have to create multiple xml for multiple background button and just set that xml on those button.:)
i am using the following layer-list file to make a background image.
Layer-list:
<?xml version="1.0" encoding="utf-8"?>
<!-- <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/bgpattern"
android:tileMode="repeat"
/> -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
>
<item >
<bitmap
android:src="#drawable/bgpattern"
android:tileMode="repeat"
/>
</item>
<item android:drawable="#drawable/trans"
>
</item>
<item android:bottom="2dp" android:left="2dp" >
<bitmap
**android:src="#drawable/colordots"**
android:gravity="bottom"
/>
</item>
</layer-list>
but the android:src="#drawable/colordots image is appearing at center bottom of the screen i need it to be appear at the left-bottom of the screen. how can i do that can any one help me in doing this. the android:left="2dp" is not taking.
try <bitmap android:gravity="left|bottom"/>