I am trying to set a backgroundimage to my ImageButton. The ImageButton should be full filled. Currently it looks like:
ImageButton
<ImageButton
android:id="#+id/imgBtn_OpenUserPicture"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center_horizontal|center_vertical"
android:src="#drawable/ic_camera"
android:background="#drawable/round_imagebutton"
android:scaleType="centerCrop"
android:tint="#android:color/transparent"
android:padding="20dp"/>
#drawable/round_imagebutton
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent"/>
<corners android:radius="90dp" />
<stroke
android:width="2dp"
android:color="#FFF"
/>
</shape>
</item>
</selector>
One problem could be the padding of 20dp. After removing it, the problem was the same. I think I have to set the image as the background of the shape. But how can I set the image dynamically - captured by the camera - to the shape background?
Use scale type as fit center.
android:scaleType="fitCenter"
There are Two way
1# Use rounded image instead of rectangle to set src
2# use this link instead of ImageButton
https://github.com/hdodenhof/CircleImageView
Related
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"
I have ImageView with shape but image is also visible to the outside of shape bounds.
This is my shape code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/dashboard_bg" />
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
android:useLevel="false">
<solid android:color="#android:color/transparent" />
<padding
android:bottom="7dp"
android:left="7dp"
android:right="7dp"
android:top="7dp" />
<stroke
android:width="10dp"
android:color="#android:color/white" />
</shape>
</item>
</layer-list>
and this is my ImageView
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/layer_list_circl"/>
And this is the result i got..
what i want just squeeze image into shape. Thanks in advance.
First of all, the image you are loading seems large, so you need to scale it to fit the right size you want. since the image you are trying to wrap is rectangle it can't be wrapped in a ring shape.
Therefore, you can use an image loading lib like fresco or picasso and set the correct size you want, and the scaling/crop option.
After you did that, you can add the background as you created and it should fit the image.
You can try adding this to your ImageView:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/layer_list_circl"/>
an Imageview is a wrapped in a linearlayout. the linearlayout has a rounded background. I want the imageView not to overlap the rounded corners of the linearlayout. Cardview does something similar but I don't want all corners radius.
Here's my snippet.
<LinearLayout
android:background="#drawable/bottom_sheet_top_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.chrisbanes.photoview.PhotoView
android:id="#+id/preview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:minHeight="300dp"
android:scaleType="center" />
</LinearLayout>
bottom_sheet_top_stroke
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="0dp"
android:left="-6dp"
android:right="-6dp"
android:bottom="-32dp">
<shape android:shape="rectangle">
<solid android:color="#color/dark"/>
<stroke
android:width="5dp"
android:color="#color/white"/>
<corners android:radius="40dp"/>
</shape>
</item>
</layer-list>
how do I achieve making sure the image is within the defined radius of the parent and not overlap.
You have to make your Image with roundedCorners.
For Image loading libaries like Glide and picasso there is already lots of transformation available .
You have to use RoundedCornersTransformation
Glide.with(this).load(url)
.apply(RequestOptions.bitmapTransform(
new RoundedCornersTransformation(this, 10, 10))).into(mImageView);
For transformations you can take reference from glide-transformations .
I want to create buttons in my android application, just like shown in the image below
you can se all the four buttons below.
I want to give them a custom color background and then an icon on the top. There is no way I'm able to adjust the icon at the centre of button.
How can I achieve the desired effect?
You can do this with FrameLayout and for circle background you need to create a shape drawable file. Refer my answer here.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_shape"
android:clickable="true"
android:focusable="true"
android:visibility="visible"
android:foreground="?android:attr/selectableItemBackground"
android:padding="4dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_star_border_black_24dp"/>
</FrameLayout>
round_shape.xml add this file in drawable folder
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:angle="270"
android:startColor="#color/profile_grey"
android:endColor="#color/profile_grey"/>
</shape>
Create Drawable and use that drawable as Button Background.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:color="#e1e1e1"
android:width="0.5dp"/>
<solid android:color="#android:color/white"/>
</shape>
</item>
</selector>
This Code create a circle with white background.
This question already has answers here:
How to make an ImageView with rounded corners?
(58 answers)
Closed 5 years ago.
Im using Picasso and trying to put image in circle imageview, but the picture is still square. How can i fix it ?
Picasso.with(context).load(resId).resizeDimen(R.dimen.image_size, R.dimen.image_size).into(holder.mImageViewItem);
image_size = 60dp
drawable resource :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFF"/>
and imageview :
<ImageView
android:id="#+id/picture_imageview_item"
android:layout_marginTop="15dp"
android:layout_marginLeft="25dp"
android:layout_marginBottom="15dp"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#drawable/buttonshape" />
There are different libraries you can use for a circle shaped image. The best one which i came across is
Siyamed
You can make a simple circle with white border and transparent content with shape.
// res/drawable/circle.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
android:useLevel="false" >
<solid android:color="#android:color/transparent" />
<stroke
android:width="10dp"
android:color="#android:color/white" />
</shape>
Then make a layerlist drawable and put it as background to your imageview.
// res/drawable/img.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"/>
<item android:drawable="#drawable/circle"/>
</layer-list>
and put it as background to your imageview.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/img"/>
In place of ic_launcher, you can add your image.
Thanks !!