CardView background alpha colour not working correctly - android

i am trying to make CardView with elevation but the issue is when i use no-alpha color like "#ffffff" it works fine but when i set some alpha color like #b0ffffffit shows another inner view with elevation
like this
but when i set non-alpha color like"#ffffff" it works fine
and this is my layout
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#b0ffffff"
android:layout_centerInParent="true"
card_view:cardBackgroundColor="#b0ffffff"
card_view:cardElevation="5dp">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="#drawable/people" />
</android.support.v7.widget.CardView>
btw i am trying to make a view like this

just remove the line:
card_view:cardBackgroundColor="#b0ffffff"
and it should work

Try this
<android.support.v7.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardBackgroundColor="#B0FFFFFF"
app:cardCornerRadius="50dp"
app:cardElevation="5dp">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="#drawable/people" />
</android.support.v7.widget.CardView>

If you are seeing annoying border around your CardView, you can try the code below, as mentioned in 배준모's answer. This also should work with androidx.cardview.widget.CardView.
<android.support.v7.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="#dimen/margin"
android:background="#android:color/transparent"
android:elevation="0dp"
app:cardBackgroundColor="#color/form_card_bg_color"
app:cardElevation="0dp"
app:contentPadding="#dimen/margin_large"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true" >

Related

CardView corners show white pixels?

I'm trying to create a custom "Warning" dialog, which I want to have rounded corners in it. So I've put my whole layout into a CardView and I'm setting my dialog's layout as this layout.
However, on dark backgrounds, such as the default Android dialog background, I seem to be getting white pixels at the corners of my dialog.
I've already tried what is proposed here, setting app:cardBackgroundColor="#color/transparent", but it didn't make a difference.
The layout of my dialog is the following (its a different one form the screenshots but the buttons, bakcground and the CardView are the same):
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/error_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/error_dialog_margin"
android:layout_marginEnd="#dimen/error_dialog_margin"
app:cardCornerRadius="#dimen/error_dialog_corner_radius"
app:cardElevation="#dimen/error_dialog_elevation"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/error_icon"
style="#style/ErrorViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/default_outer_margin"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/error_icon"
android:tint="#color/redColor" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/error_title"
style="#style/ErrorViewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/default_outer_margin"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginEnd="#dimen/default_outer_margin"
android:ellipsize="end"
android:gravity="center_horizontal"
android:maxLines="2"
android:text="#string/something_went_wrong" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/error_message"
style="#style/ErrorViewMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/default_outer_margin"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginEnd="#dimen/default_outer_margin"
android:gravity="center"
android:minHeight="55dp"
tools:text="#string/try_again_later" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/default_margin"
android:background="#color/blueColor">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/error_ok_button"
style="#style/PrimaryButton"
android:layout_width="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:text="#string/ok_button" />
</FrameLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
Please check the screenshots of the issue:
Any idea why this happens and how to fix it? It seems to be related to the cardview's background, since setting all backgrounds to transparent fixes the issue, But I need the dialog background to be white.
For me setting
app:cardBackgroundColor="#color/transparent"
in the CardView fixed it
Instead of using a CardView for rounding the dialog corners declare a new style in your styles.xml.
This style should have ThemeOverlay.MaterialComponents.Dialog.Alert as a parent.
The property you need to set is shapeAppearanceMediumComponent:
<style name="style_dialog" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="shapeAppearanceMediumComponent">#style/style_dialog_background</item>
</style>
<style name="style_dialog_background" parent="ShapeAppearance.MaterialComponents.MediumComponent">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">#dimen/error_dialog_corner_radius</item>
</style>
Edit your dialog layout and remove the CardView parent:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/error_icon"
style="#style/ErrorViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/default_outer_margin"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/error_icon"
android:tint="#color/redColor" />
...
</LinearLayout>
Finally, create your dialog like this:
MaterialAlertDialogBuilder(activity, R.style.style_dialog)
.setView(<your-dialog-layout>)
...
.create().apply {
show()
}
Try using material card layout
com.google.android.material:material:1.2.1
<com.google.android.material.card.MaterialCardView
android:layout_width="180dp"
android:layout_margin="4dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:foreground="?attr/selectableItemBackground"
android:padding="8dp"
app:cardCornerRadius="6dp"
app:cardElevation="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#drawable/border"
>

CardView rounded corner gets unexpected white colors

When using rounded corner in CardView, shows a white border in rounded area which is mostly visible in dark environment. For easier understanding, check the attached image blow.
here is xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorTransparent"
android:orientation="vertical"
android:padding="4sp">
<androidx.cardview.widget.CardView
android:id="#+id/image_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="20dp"
android:background="#color/colorTransparent"
app:cardCornerRadius="20dp"
app:cardElevation="4dp">
<ImageView
android:id="#+id/iv_themes"
android:layout_width="match_parent"
android:layout_height="110dp"
android:background="#color/colorTransparent"
android:gravity="center"
android:scaleType="centerCrop"
android:src="#mipmap/tm_2_mip_background" />
</androidx.cardview.widget.CardView>
</LinearLayout>
I had the same problem recently.
Adding app:cardBackgroundColor="#color/transparent" to the CardView did the trick.

Android CardView transparent background

I want to create a cardview where the cards have simple one photo and an upper right bottom to remove them. So in order to the bottom be completelly visible i need to make the card bigger than the photo, but i dont want to show it. So i try to set the background to transparent and at design time everything seams fine
This is a screenshot of android studio at design time:
A) shows how the card looks like
B) is the same card when i click on it, showing that the card is a bit bigger than the photo itself (the blue borders are the normal android studio border when you select an element)
but during the runtime the cards are getting some weird elevation:
Here goes the code:
influencer_card.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/smallMargin"
android:elevation="0dp"
card_view:cardBackgroundColor="#android:color/transparent"
card_view:cardCornerRadius="0dp">
<ImageView
android:layout_width="#dimen/influencerCardSize"
android:layout_height="#dimen/influencerCardSize"
android:layout_gravity="bottom"
android:scaleType="centerCrop"
/>
<TextView
android:layout_width="#dimen/influencerCardSize"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:alpha="0.7"
android:background="#color/colorPrimaryDark"
android:gravity="center"
/>
<ImageButton
android:layout_width="#dimen/removeButtonSize"
android:layout_height="#dimen/removeButtonSize"
android:layout_gravity="right|top"
android:background="?android:attr/selectableItemBackground"
android:src="#drawable/ic_remove" />
</android.support.v7.widget.CardView>
new_influencers.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="#dimen/influencersViewHeight"
android:layout_gravity="center"
android:layout_margin="#dimen/smallMargin"
android:background="#android:color/transparent"
android:elevation="0dp"
android:scrollbars="vertical"
android:visibility="visible" />
</LinearLayout>
I don't change any style related thing on the java code...
how can i remove this annoying elevation
If you don't want the elevation, you should consider not using CardView since CardView brings the elevation by default.
Instead, replace the CardView with a container like ConstraintLayout or RelativeLayout. These options don't carry any elevation by default, are transparent, and you can still add foreground ripple effects to them if you wish to do so.
want to card make invisible ,make "alpha" attribute 1
If you want to remove elevation in CardView,
Use app:cardElevation = "0dp" instead of android:cardElevation="0dp".
Use card_view:cardElevation="0dp" instead of android:elevation="0dp". Hope this helps you
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_below="#+id/linearLayout"
android:layout_centerHorizontal="true"
card_view:cardElevation="0dp"
card_view:cardBackgroundColor="#android:color/transparent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/img"
android:scaleType="centerCrop"
android:layout_marginRight="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:alpha="0.7"
android:text="text"
android:layout_marginEnd="10dp"
android:background="#color/colorPrimaryDark"
android:gravity="center"
android:layout_marginRight="10dp" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|top"
android:background="?android:attr/selectableItemBackground"
android:src="#drawable/img" />
</android.support.v7.widget.CardView>

card view boundaries showing on pre lollipop devices

The following code is rendering with undesired view boundaries on devices with android versions pre lollipop:
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardBackgroundColor="#color/transparent"
app:cardCornerRadius="#dimen/card_corner_radius"
app:cardElevation="0dp"
app:cardUseCompatPadding="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
app:srcCompat="#{radio.radioLogo}"
app:riv_corner_radius="#dimen/card_corner_radius"
android:alpha="#{radio.playing}"/>
<LinearLayout
android:id="#+id/overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/transparent"
android:gravity="center"
android:orientation="vertical"
android:visibility="#{radio.recording?View.VISIBLE:View.GONE}">
<TextView
android:id="#+id/recording_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:text="#string/recording"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#color/white" />
<TextView
android:id="#+id/recording_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:text="#{radio.recordingElapsedTime}"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#color/white" />
</LinearLayout>
</FrameLayout>
</android.support.v7.widget.CardView>
ps: I tried card_view:cardPreventCornerOverlap="false"
and it doesn't solve my problem.
lollipop render
Pre lollipop render
Thanks in advance for any interest in helping.
I found a solution that worked for me and I wanted to post it in case any one else came across this situation.
I set the maximum elevation to 0dp and now rendering is perfect on API level 16 too.
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardBackgroundColor="#color/transparent"
app:cardCornerRadius="#dimen/card_corner_radius"
app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:cardUseCompatPadding="true">
If you need to keep elevation on v21 devices and remove cardview weird boundaries on pre21 devices - create values-v21/dimens.xml with:
<dimen name="card_elevation">2dp</dimen>
And values/dimens.xml with:
<dimen name="card_elevation">0dp</dimen>
Then add property to CardView:
app:cardElevation="#dimen/card_elevation"

Put a button over an ImageView

I'm newbie in Android.
I would like to know if it's possible to put a button or another component over an ImageView. I've tried setting the image as a background image of a LinearLayout, but when I change between landscape and portrait mode, the image porportions are changed.
Thanks a lot.
Don't put the image as a background, you don't have any control on how the image is scaled. Instead, create a RelativeLayout, and put an ImageView as one of the child, and you can place anything (buttons etc) as other RelativeLayout children.
<RelativeLayout ...>
<ImageView (your image) ...>
<Button (the button you want) ... />
</RelativeLayout>
Try this code... it's Help you....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageviewMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="Path "
/>
<Button android:id="#+id/but2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Try this Code .....
In that give paramater in button to set your Button Position....
android:layout_margin or android:layout_alignParent
And also give Path of Image....
There is another way to do it,http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/
Thank you.
The simpliest way to do it is to use a FrameLayout.
This is easy using the ConstraintLayout.
Set the constraints of the Button to the ImageView.
Drag the Button anywhere you want over the ImageView to position it.
XML code will be autogenerated.
For those using Constraint Layout: Just use an ImageView and add its constraints from all the four side to Parent and than add the other views as well.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
app:cardElevation="10dp"
app:cardPreventCornerOverlap="true"
app:contentPadding="5dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/food"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/imagebtn_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_share_24"
app:layout_constraintTop_toTopOf="parent"
android:padding="15dp"
app:layout_constraintStart_toStartOf="parent" />
<ImageButton
android:id="#+id/imagebtn_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_call_24"
android:padding="15dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/expdate_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/test_date"
android:background="#E2E3E4"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#+id/imagebtn_address"
android:layout_marginEnd="30dp"
android:padding="5dp"/>
<TextView
android:id="#+id/title_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginBottom="5dp"
android:background="#E2E3E4"
android:padding="5dp"
android:textSize="18sp"
android:text="#string/test_foodname"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/imagebtn_address"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<ImageButton
android:id="#+id/imagebtn_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_location_on_24"
android:padding="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Note: Replace the drawables added with your own.

Categories

Resources