Why does the android:src behaves weirdly on padding in drawable? - java

I recently did some test on android drawable and I found that the android:src behaviors to padding is extremely weird compare the android:background.
Here is my drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/colorAccent"/>
<padding
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
android:left="1dp"
/>
</shape>
</item>
<item android:top = 2>
<shape android:shape="oval">
<solid android:color="#color/white"/>
</shape>
</item>
</layer-list>
Inside of the mainActivity layout I have another image view which I used to test. I set the image view to 40dp width and height. Then I tested the android:src vs the android:background attribute by setting them to my drawable above. Here is the result:
Android:src
Android:background
The android:background as you can see is the expected behavior because I move the white circle down 2 and it has padding that obeys the pink oval padding. Now the android:src is straight up unexpected because 1st of all the white oval which is the layer that is above the pink oval is not shown. Second of all, the shapes change to a flatten oval.
Any clue to why this is the case??????

src works with android:scaleType where you can give different options ScaleType
whereas background always takes scaleType of FitXY covering full region for that view.
if you want the same behaviour as background with src,
put android:scaleType="FitXY". there are other options you should check also.

Related

How to use image as backgroung image using only View Tag in android studio?

i need to show images in layout but only using view tag not image view tag in android studio.but i have already use background tag for oval shape?
I don't know your use case because you didn't post any code. ;-)
Delete the solid to make the oval transparent.
Move the item (oval/image) which you would like to be in the background to the first position.
Try this xml for your drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_launcher_background"/>
<item>
<shape android:shape="oval">
<solid android:color="#color/colorAccent"/>
</shape>
</item>
</layer-list>

Create drawable programmatically

How can I create this exact drawable in java code only?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="1dp" />
<solid android:color="#ff0000" />
<padding
android:bottom="-15px"
android:top="-5px" />
</shape>
I need the padding values to be variable so I can't do it as an xml.
If you wanna draw something custom, you should open a Canvas and make your drawing there.
Read this:
https://developer.android.com/training/custom-views/custom-drawing.html
And it seems a simple shape. So you can add a layout view (View,TextView, ImageView, etc.) set background with this drawable and adjust padding through this view.

layer-list shape + bitmap on top with padding

I Try to center a drawable bitmap within a shape with padding, using layer-list.
But i can't get padding around the image.
THis is not working:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="270"
android:centerColor="#ffffffff"
android:endColor="#ffbab8b9"
android:startColor="#ffcfccce" />
<stroke
android:width="1px"
android:color="#A0A0A0" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
</item>
<item
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp">
<bitmap android:src="#drawable/logo"
android:gravity="center" />
</item>
</layer-list>
Does someone have an idea how to center a bitmap (for scaling) with padding?
Right now the bottom,left,... parameters are just ignored. The bitmap is scaled 100% within the Button.
I need it for a Button and can't use an ImageButton here.
Also the Button don't have text in that special case, so compoundDrawable will also not work. I need to do it with a Button.
Best regards,
Juergen
Ok, looks like theres no way using padding of a bitmap within layer-list. I just decreased the bitmap size and now it fits in the button perfectly. But i don't know if it is looking same sized on every device...
You can put icon straight in item element, like this:
<item
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp"
android:drawable="#drawable/logo"
android:gravity="center"/>

How create border around visible image in ImageView

It is possible to create contour around or shadow under VISIBLE pixels in ImageView.
In case this image in ImageView (outside from green figure is transparent):
I need next pic with border:
Please any code or references.
Dunno you just want to add a color as stroke to the background then you can do like this
If you are using color then you can create an xml file in the drawable in the res of your project
and create a drawable like this
Image.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="2dp"
android:color="#007cea" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
<solid android:color="#007cea" />
<corners android:radius="4dp" />
</shape>
And to your imageview you can call the xml file as
android:background="#drawable/login_button"
In the xml file you specify like this
<stroke
android:width="2dp"
android:color="#007cea" />
the android:color="#007cea" defines which color you want to give as border
I don't think you will find exactly what you want.
If it was drawable possibly you could use layer-list option.
But it won't work with Imageview.

Changing Spinner Highlight AND line color

This XML below results in my Spinner having an orange line across the bottom and an orange rectangle on the right-hand side. I have two questions:
How can I make it so that the orange rectangle on the right-hand side is an orange triangle, like the default one associated with Theme.Dialog?
Now that I changed the look of the Spinner, when I click on it it no longer gets highlighted. I want to make it look the way I want AND highlight to a lighter color of orange when I press on it.
Do I have to accomplish both things in the same XML file? I've already got the drawable below set to the background of my spinner, so I can't declare the background twice right?
Any ideas? Thanks in advance!
My Drawable Custom Spinner Layout:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape >
<solid android:color="#color/base_orange" />
</shape>
</item>
<!-- main color -->
<item android:bottom="1.0dp"
android:left="0.0dp"
android:right="10.0dp">
<shape >
<solid android:color="#color/white" />
</shape>
</item>
<item android:bottom="25.0dp">
<shape >
<solid android:color="#color/base_orange" />
</shape>
</item>
</layer-list>
You could refer to this and get the spinner drawables according to your wish.
Hope that helps

Categories