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"/>
Related
I would like to put a fade black covering my image, just like this one, with a text, a fade background and then my image. I just want to know how to implement this fade. Thank you!
Just create a drawable file, I'll name it gradient_background (you can set any name)
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:startColor="#000000"
android:centerColor="#00000000"
android:endColor="#00000000"/>
</shape>
then inside your target xml, create a View like this one, look that inside it, we're setting the background as your drawable file. In this case is gradient_background
<View
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/gradient_background"
app:layout_constraintBottom_toBottomOf="post_image"
app:layout_constraintEnd_toEndOf="#+id/post_image"
app:layout_constraintStart_toStartOf="post_image"
app:layout_constraintTop_toTopOf="#+id/post_image" />
So it should look like this:
:)
First, Make bottom_transparent_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:angle="270"
android:endColor="#64000000"
android:startColor="#android:color/transparent" />
</shape>
</item>
</selector>
Then, add that drawable as your view background in which you want to show the fade background effect.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bottom_transparent_gradient"/>
And that's it! You can see the result you wanted. :)
I have two nested circular indeterminate progress bars but I cannot set their thickness to an equal size. I have tried to customize it using shapes. However, that removes the default behaviour of the circular indeterminate progress.
I customize like this and it made thickness equal but not getting default behaviour.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/progress">
<rotate android:toDegrees="360">
<shape
android:shape="ring"
android:thickness="4dp">
<solid android:color="#color/white" />
</shape>
</rotate>
</item>
</layer-list>
<ProgressBar
android:id="#+id/download_progress4"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_gravity="center"
android:indeterminate="true"
android:indeterminateDrawable="#style/ProgressThem" />
I want like this with default behaviour of circular indeterminate progress bar
Try this
USE
android:innerRadiusRatio="3"
android:thicknessRatio="20.0
SAMPLE CODE
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/progress">
<rotate android:toDegrees="360">
<shape
android:shape="ring"
android:thickness="4dp"
android:innerRadiusRatio="3"
android:thicknessRatio="20.0">
<solid android:color="#color/white" />
</shape>
</rotate>
</item>
</layer-list>
If you want the modern animation rather than just rotation, you can copy and alter https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/drawable/progress_indeterminate_anim_medium_material.xml using it as the indeterminateDrawable.
<ProgressBar
android:indeterminateDrawable="#drawable/new_progress_indeterminate_anim_medium_material"
To change the width in vector_drawable_progress_bar_medium.xml android:strokeWidth="1" for example.
It's a bit painful to do this as you also need to copy over the animation, interpolator and animator xml files to your project as they are private in the Android SDK.
There is actually an even easier way to do that with the material components:
<com.google.android.material.progressindicator.CircularProgressIndicator
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
app:trackThickness="3dp" />
More details in material docs -> https://material.io/components/progress-indicators/android#circular-progress-indicators
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.
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.
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.