<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/image"
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_gravity="center"
android:layout_marginEnd="#dimen/_16dp"
android:background="#drawable/ic_group_profile_image"
android:scaleType="centerCrop"
android:src="#drawable/ic_group_profile_image"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/save"
app:layout_constraintTop_toTopOf="parent" />
binding.appbar.groupImage.apply {
shapeAppearanceModel = shapeAppearanceModel
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED, 8f)
.build()
load(path)
}
**this is my xml and kotlin code for round image, **
this is my output what I'm getting
I need to show like this
help me to solve this problem.
You can use this style in the style.xml file:
<style name="circleImageView" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
For this, you can use CircleImageView library which is quite easy to use and provide you profile picture in round shape.
The initial step is to add gradel dependencies to your android project.
dependencies {
...
implementation 'de.hdodenhof:circleimageview:3.1.0'
}
After this, use the given below snippet inside your layout file or where you want to use a round profile picture.
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="#drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
For more info, you can check the GitHub link
Related
I am using ShapeableImageView thus:
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/myImage"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="#drawable/larry"
app:strokeColor="#color/red"
app:strokeWidth="9dp"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.CornerCircle" />
and it's drawing quite alright. However, I want a little spacing between the view and the stroke. I have tried android:padding="10dp" but that doesn't seem to be working.
I have also tried removing the stroke properties and using this drawable resource to set the background of the view:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="9dp"
android:color="#00FF00" />
</shape>
but the border didn't appear at all.
Please, do you know of any way I can achieve it?
I was looking for this myself, and just stumbled on contentPadding in the Android documentation.
In XML:
app:contentPadding="10dp"
In code:
.setContentPadding(left, top, right, bottom)
It is not elegant but you can try something like this:
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="0dp"
app:cardBackgroundColor="#color/red600"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.CornerCircle"
>
<com.google.android.material.imageview.ShapeableImageView
app:srcCompat="#drawable/..."
android:padding="9dp"
app:strokeWidth="1dp"
app:strokeColor="#color/white"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.CornerCircle" />
</com.google.android.material.card.MaterialCardView>
with
<style name="ShapeAppearanceOverlay.App.CornerCircle" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
I want to make the same toolbar. Everything is perfect but can not get the shadow as the picture shows. I design the whole things but can not get the shadow. Can anyone plz guide me. I m posting my code below and attaching the image below:
layout design image
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="#+id/cvToolbar"
style="#style/materialCardView"
android:layout_width="match_parent"
android:layout_height="#dimen/dp_56"
android:layout_marginStart="#dimen/dp_8"
android:layout_marginTop="#dimen/dp_8"
android:layout_marginEnd="#dimen/dp_8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/ivToolbarBack"
android:layout_width="#dimen/dp_0"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/dp_23"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_arrow_back" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/tvToolbarTitle"
style="#style/pinVerificationToolbarTextStyle"
android:layout_width="#dimen/dp_0"
android:layout_height="#dimen/dp_40"
android:layout_marginStart="#dimen/dp_24"
android:layout_marginTop="#dimen/dp_11"
android:layout_marginEnd="#dimen/dp_8"
android:layout_marginBottom="#dimen/dp_5"
android:gravity="top"
android:text="#string/pos_system"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/ivToolbarBack"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
<style name="pinVerificationToolbarTextStyle">
<item name="android:textAppearance">#style/TextAppearance.Text.Regular</item>
<item name="android:textColor">#color/colorBlack</item>
<item name="android:textSize">22sp</item>
<item name="android:letterSpacing">-0.01</item>
<item name="android:textStyle">bold</item>
<item name="fontFamily">#font/martel_sans</item>
</style>
<style name="materialCardView">
<item name="cardBackgroundColor">#color/colorWhite</item>
<item name="cardCornerRadius">26dp</item>
<item name="cardElevation">1dp</item>
<item name="elevation">1dp</item>
<item name="android:translationZ">3dp</item>
</style>
can somebody please help me to resolve.
you can use android:background="#android:drawable/dialog_holo_light_frame" to get shadow also you can make a background drawable using any drawing software like photoshop ,xd etc
you can see this as a reference Android View shadow
Just add android:elevation=“6dp” in your toolbar
In your onCreate method of your activity code add the following code:
kotlin code:
supportActionBar?.elevation = 8F
java code:
getSupportActionBar().setElevation(8);
------------------------OR-----------------
kotlin code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
actionBar?.elevation = 8F
}
java code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getActionBar().setElevation(8);
}
to achieve 8dp of elevation in your app bar. (adding elevation is going to add shadow)
You can change the numeric value to achieve the desired elevation(shadow)
how to create this type of layout in android look like image
:
use MaterialCardView and use cardElevation
add this in gradle file to use material components
implementation 'com.google.android.material:material:1.2.0-alpha03'
style.xml
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
in your xml
<com.google.android.material.card.MaterialCardView
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:strokeWidth="1dp"
app:strokeColor="#color/stroke_color"
app:cardElevation="10dp"
android:layout_margin="8dp">
//add some view
</com.google.android.material.card.MaterialCardView>
I think you can try to use cardView and add onclick func to act as a button
Inside the cardView, you need to customize the layout of TextView, ImageView and other UI you want.
https://guides.codepath.com/android/using-the-cardview
https://material.io/develop/android/components/cards/
For button shadow, I guess cardView can add shadow too
How to add shadow on CardView aligned to bottom of parent
Did u try: android:elevation="3dp" in view xml? let try.
Hope this help!
<androidx.cardview.widget.CardView
android:id="#+id/maincard"
android:layout_width="#dimen/_250sdp"
android:layout_height="#dimen/_150sdp"
android:layout_marginStart="#dimen/_5sdp"
android:layout_marginEnd="#dimen/_15sdp"
app:cardCornerRadius="#dimen/_12sdp"
app:cardElevation="#dimen/_10sdp"
app:cardPreventCornerOverlap="false">
I'm using the AndroidX support libraries. In my manifest I declare:
<application
...
android:theme="#style/AppTheme">
In values.styles.xml I declare:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="android:imageButtonStyle">#style/MyImageButton</item>
</style>
<style name="MyImageButton" parent="Widget.AppCompat.ImageButton">
<item name="android:background">#android:color/transparent</item>
</style>
Then I use the ImageButton via:
<ImageButton
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp" />
Unfortunately, my ImageButton shows a grey background instead of the transparent one. What do I have to do to fix it?
You need to replace this line
<item name="android:imageButtonStyle">#style/MyImageButton</item>
with
<item name="imageButtonStyle">#style/MyImageButton</item>
And This is also another way given by #prince
Add this line in your code to
style="#style/MyImageButton"
So,
<ImageButton
style="#style/MyImageButton"
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp" />
or you don't want to add style in your all button tag you can follow this answer to change the style of the default button. Hope it will help you. Try and let me know it works or not.
Thanks.
Happy coding :)
You can simply set background of ImageButton to null as below :
<ImageButton
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#null" />
This will make your ImageButton transparent.
I have the following CardView and I want to set different radius for each corner in the card. Is it possible to change them by XML or programmaticaly? Thanks in advance.
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:cardCornerRadius="0dp"
app:cardElevation="0dp">
</android.support.v7.widget.CardView>
EDIT
As Avinash suggest, I am looking for the behaviour of this lib github.com/captain-miao/OptionRoundCardview but using the default CardView item. If it is not possible to change it individually, this lib is a good approach.
It requires the official MaterialCardView (which extends the androidx.cardview.widget.CardView) and at least the version 1.1.0 of the Material components library.
Add to your layout the MaterialCardView:
<com.google.android.material.card.MaterialCardView
style="#style/CustomCardViewStyle"
...>
</com.google.android.material.card.MaterialCardView>
Define a custom style inheriting a material card style (for example Widget.MaterialComponents.CardView) and use the shapeAppearanceOverlay attribute:
<style name="CustomCardViewStyle" parent="#style/Widget.MaterialComponents.CardView">
<item name="shapeAppearanceOverlay">#style/ShapeAppearanceOverlay_card_custom_corners</item>
</style>
<style name="ShapeAppearanceOverlay_card_custom_corners" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">4dp</item>
<item name="cornerSizeTopLeft">8dp</item>
<item name="cornerSizeBottomRight">16dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
You can also achieve it programmatically.
Just apply a custom ShapeAppearanceModel to the corners of the card.
Something like:
float radius = getResources().getDimension(R.dimen.my_corner_radius);
cardView.setShapeAppearanceModel(
cardView.getShapeAppearanceModel()
.toBuilder()
.setTopLeftCorner(CornerFamily.ROUNDED,..)
.setTopRightCorner(CornerFamily.ROUNDED,..)
.setBottomRightCorner(CornerFamily.ROUNDED,radius)
.setBottomLeftCornerSize(0)
.build());
Note: it requires the version 1.1.0 of the library.
With Jetpack compose you can use the shape parameter in the Card.
Something like:
Card(
shape = RoundedCornerShape(
topStart = 4.dp,
topEnd = 8.dp,
bottomEnd = 16.dp,
bottomStart = 2.dp,
)
){
Text("Content Card")
}
You can create a custom xml and name it rounded_corners.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp"
android:topLeftRadius="20dp"
android:topRightRadius="30dp"
android:bottomLeftRadius="40dp"
android:bottomRightRadius="50dp"/>
<solid android:color="your_background_color" />
</shape>
And then use this as the background for your CardView:
android:background="#drawable/rounded_corners"
EDIT: I just noticed that this may work for all other views other than CardView, so refer to this question for seeing how to do a workaround.
NOTE: This here is a workaround if you want to achieve rounded corners at the bottom only and regular corners at the top. This will not work if you want to have different radius for all four corners of the cardview. You will have to use material cardview for it or use some third party library.
Here's what seemed to work for me:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#F9F9F9">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/profile_bg"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cvProfileHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="32dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="280dp"
android:orientation="vertical"
android:background="#drawable/profile_bg"
android:id="#+id/llProfileHeader"
android:gravity="center_horizontal">
<!--Enter your code here-->
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
There's two cardview's in all. The second cardview is the one that will have rounded corners (on all sides as usual) and will hold all other subviews under it. The first cardview above it is also at the same level (of elevation), and has the same background but is only about half the height of the second cardview and has no rounded corners (just the usual sharp corners). This way I was able to achieve partially rounded corners on the bottom and normal corners on the top. But for all four sides, you may have to use the material cardview.
In case, if you use ImageView inside CardView, you should change ImageView into ShapeableImageView. Normal ImageView only works with all corners not each corner.
Here is more detail example on material.io docs
themes.xml
<style name="MyCardView" parent="#style/Widget.MaterialComponents.CardView">
<item name="shapeAppearanceOverlay">#style/MyCardViewOverlay</item>
</style>
<style name="MyCardViewOverlay">
<item name="cornerFamily">rounded</item>
<!-- below items does not work with normal ImageView -->
<item name="cornerSizeTopRight">0dp</item>
<item name="cornerSizeTopLeft">60dp</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
layout.xml
<com.google.android.material.card.MaterialCardView
style="#style/MyCardView"
android:layout_width="200dp"
android:layout_height="200dp"
app:cardPreventCornerOverlap="false">
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#color/orange_600"
app:shapeAppearanceOverlay="#style/MyCardViewOverlay" />
</com.google.android.material.card.MaterialCardView>
Hi you can add it programmatically or by xml with following code.
app:cardCornerRadius="0dp"// xml
cardView.setRadius(0);
this one is extra who is looking for elevation
app:cardElevation="0.7dp"//xml
app:cardMaxElevation="1dp"//xml
cardView.setCardElevation(2.1f);//code
cardView.setMaxCardElevation(3f);//code
The complete Java representation of the CardView’s XML.
CardView cardView = (CardView) findViewById(R.id.cardView);
cardView.setUseCompatPadding(true);
cardView.setContentPadding(30, 30, 30, 0);
cardView.setPreventCornerOverlap(true);
cardView.setCardBackgroundColor(Color.WHITE);
cardView.setCardElevation(2.1f);
cardView.setRadius(0);
cardView.setMaxCardElevation(3f);