Image View background reflect only inside the circle not outside the circle.
I tried so many things but not able do that.
Anybody please me.
Thanks!!
Make an .xml file in drawable folder with this content:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#color/black"/>
</shape>
Then set this file as your background.
circle.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<size android:width="50dp"
android:height="50dp"/>
<stroke
android:width="3dp"
android:color="#color/black"/>
<solid android:color="#color/black"/>
</shape>
in ImageView
add this in
android:background="#drawable/circle"
thank you everyone for the help.
This code is working as I want.
thank You!!
I guess you're using CircularImageView custom imageview class to make it circular. That's why background it getting apllied in cicular region only.
To solve it, do something like this,
<LineaLayout
android:width="wrap_content"
android:height="wrap_content"
android:orientation="vertical"
android:background="#color/black"/>
<!-- your circular CircularImageView here..!! -->
</LinearLayout>
Other solution you can do is, Use native ImageView and set its android:src by creating circular bitmap and use android:background black color and i would suggest to go with thise solution as you can avoid nested layouts.
I hope this hepls! Thanks.
Related
I am new to Android. I am having problem in setting up the custom shape of SliderLayout. I am using Daimajia Library for this. Can anyone please help.
I am posting the xml below (Shape I required)
<!-- Colored rectangle-->
<item android:top="0dp"
android:bottom="-10dp">
<shape android:shape="rectangle">
<size
android:width="335dp"
android:height="#dimen/dp300" />
<solid android:color="#FF0000" />
</shape>
</item>
<item
android:top="265dp"
android:bottom="-50dp"
android:right="-10dp"
android:left="-10dp">
<rotate
android:fromDegrees="-8">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
Daimajia SliderLayout is a custom 3rd party library Slider layout.. and as at now it doesn't even show the "Android:background" attribute in it for you to be able make a custom shape and set that custom shape as its background shape...
So honestly, it will remain as a Rectangle for now and cant be curved and changed to a different shape....
Maybe unless you research and use a totally different slider layout library where you can change its shape
finally after 2 hours dealing with this problem i found the answear.
first you need to add [ implementation 'com.jcminarro:RoundKornerLayouts:0.4.0'
] library which help you curve any layer u want : this is an example :
<com.jcminarro.roundkornerlayout.RoundKornerRelativeLayout
android:id="#+id/rtl_slider_advertisment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="16dp"
app:corner_radius="5dp">
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider_advertisment"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<com.jcminarro.roundkornerlayout.RoundKornerRelativeLayout
android:background="#drawable/gold_line"
android:layout_width="match_parent"
android:layout_height="200dp"></com.jcminarro.roundkornerlayout.RoundKornerRelativeLayout>
</com.jcminarro.roundkornerlayout.RoundKornerRelativeLayout>
I'm trying to use a View's elevation property to cast a shadow. It works fine when the background is a circle or a rounded rectangle drawable. However, if I use a color or a rectangle drawable as a background the shadow doesn't show.
Simply changing the radius of the rounded rectangle to zero, causes the shadow to disappear. I have tried solutions to similar issues such as adding padding and margin. I added both:
android:clipChildren="false"
android:clipToPadding="false"
to the parent View but that didn't fix the problem either.
I am using the simplest shapes possible:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/colorAccent" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#color/colorAccent" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/colorAccent" />
</shape>
And here's the very simple layout where I'm testing this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#drawable/rectangle"
android:elevation="40dp"/>
</LinearLayout>
I know there are other ways of achieving shadows in Android such as using 9-patch images, but elevation is a much simpler way and I'm hoping to make it work using only that.
These are the previews changing only the background drawable.
https://i.imgur.com/CtVhI8m.png
https://i.imgur.com/T2Ozcz7.png
This is the end result I'm going for:
https://i.imgur.com/irIMNAF.png
But I mostly just want to figure out why the shadow is not showing with a color or rectangular drawable as background.
When I use a solid color background (e.g. android:background="#fff") or a rectangle shape without rounded corners and run the app, I see a shadow.
However, I have to actually run the app. Just looking at the Android Studio layout preview doesn't show the shadow unless I use a background drawable with rounded corners. So I suspect you're just looking at a bug (or feature?) of the layout preview.
In my case helped wrap my shape with layer-list like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/avatar_ripple">
<item>
<shape android:shape="oval">
<solid android:color="#color/black_light" />
<stroke
android:width="#dimen/stroke_large"
android:color="#color/gray" />
</shape>
</item>
Then you can simple apply elevation to your view with your drawable as background, but there is bug that you won't see the shadow until you launch the app.
Particularly helped this answer: https://stackoverflow.com/a/60424218/16006976
I'm trying to create a shape for ImageView that would have round corners, the actual image and a shadow. When I created a shape and set the following variables then I got myself a rounded image with some shadow:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#42000000" />
<corners android:radius="5dp" />
</shape>
Now when I tried to add my custom image (like many people suggested I used Layer-list I got the image but the rounded corners and shadow was gone :
<?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">
<solid android:color="#42000000" />
<corners android:radius="5dp" />
</shape>
</item>
<item android:drawable="#drawable/no_cover_art" />
</layer-list>
Also I will be changing``Images` programatically so maybe that is also something that needs to be mentioned.
What am I doing wrong and how do I get the rounded corners with shadows and custom image?
EDIT
I have tried using only the shape as a drawable, but I get a result where it applies the corners to something behind it and does not change the ImageView itself.
<ImageView
android:id="#+id/song_album_cover"
android:layout_width="64dp"
android:layout_height="68dp"
android:layout_marginStart="8dp"
android:layout_marginTop="6dp"
android:elevation="18dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/no_cover_art"
android:background="#drawable/artist_cover_image"/>
On the left side is a picture of what Android Studio shows me and on the right side is the image from when I actually run the app.
Use the drawable as the background for the image view and then set the src to whatever image you want.
/use need to use this below thing in the imageview and put your code in the drawable file and then add in the below way/
android:background="#drawable/nameofdrawablefile"
Well.. I have some ImageButtons in my Android application. I want them to display the full picture. I mean: I want the ImageButton to be JUST the picture, you know?
Ok... so far, so good... I can do it use the property "background".
However I also want the ImageButton to have the corners rounded.
I have the following xml in my layout folder:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFFFF"/>
<corners android:radius="10dip"/>
<stroke android:width="8dip"/>
<size android:height="8dip"/>
</shape>
It works perfect when I don't want the picture to fill the WHOLE ImageButton... however when I try to use the picture to use the whole imagebutton... it doesn't show the rounded corners.
Can anyone help me, please?
Bonus question: Also, how can I have a beautiful black line (rounded corners) around the imagebutton?
I can suggest you to use ImageView instead of ImageButton and catch its onClick().And you can use this xml file to give it a fine black rounded edges outline to your image:
image_bg.xml:(paste this xml file in drawable folder)
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#000000" />
<stroke android:width="3dp" android:color="#776da8" />
<corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" />
<padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" />
</shape>
You can use it (in xml file where you have added the image) like:
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:src="#drawable/icon"
android:background="#drawable/image_bg" />
What is the use of the image button? I'm currently working on a soundboard and i ran into the same issue. Then I realized that it doesn't have to be an "imagebutton" for you to be able to use it like a button. Now i'm just using imageview and then using onclick. As for the aesthetics of the black line i don't know but my images have rounded corners because i brought the images in by creating icons under new/other/android icons. It automatically makes one in each diff size category. Hope it helps!
I believe this will help you out. You might be able to tweak it slightly to add in black borders when painting the new bitmap.
How to make an ImageView with rounded corners?
You can then just attach an onClick handler to the imageview.
I have a list view structure, with Relative layout that uses
alternating background images for odd/even elements. I'm trying to set
the background drawable dynamically by calculating the position. It
worked fine with the normal bitmap. But when I tried to use the
ninepatch image it breaks the UI, all the elements get distorted. What
am I doing wrong? Could it be how the ninepatch image is created or is
there a different way to use a ninepatch image compared to a normal
bitmap.
My List View XML goes like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/id01"
android:background="#drawable/my_9patch_bg_image">
<ImageView />
<RelativeLayout>
<ImageView />
<TextView />
<TextView />
</RelativeLayout>
</RelativeLayout>
May be the solution here might work for my problem. It's exact though I have to try it.
Since nobody answered you for a year now...
I had the same issue you need to setpadding of zero (even if you don't need it).
Good luck.
You can use nine patch in the same way you use a normal drawable. I assume you have a problem with the 9 patch itself. In the /tools directory of your android sdk you have the draw 9 patch tool. It will help you to correct your 9 patch drawable and to preview it for different sizes of your view.
select all sides except corners as the stretching edges
you can use this xml as background to show as cardview.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:dither="true" android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="#e2e2e2" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:dither="true" android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="#color/white" />
</shape>
</item>