Android : Cardview Background is turning to black on 4.1.2 - android

I am using google cardView support library for my card functionality. It works well for kitkat and version up but however the background of card is set to black and padding/margins are not applied on device 4.1.2.
<android.support.v7.widget.CardView
android:id="#+id/all_goals_card_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:padding="10dp"
app:cardCornerRadius="4dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardBackgroundColor="#android:color/white"
>
</android.support.v7.widget.CardView>

Okay, I just stumbled across the same issue and I found some devices to have some "special" very-light light-theming defaults cough samsung cough I will answer this slightly old queston.
The thing here is that you are most likely using the wrong context to inflate you layout. I think you are using the application-context to do so. Application-Context does not apply the theme you defined.
This (inflating with the application-context) is legal, but inflation
will be done with the default theme for the system on which you are
running, not what’s defined in your application.*
For example if you do:
LayoutInflater.from(context).inflate(R.layout.menu_rental_list_item, parent, false);
The context here should be an Activity- or Fragment Context - NOT the application-context.
Please double check that.
*) Ah, you want to read more about contexts? Please continue reading here.

don't use "#android:color/white"
card_view:cardBackgroundColor="#fff"

This will solve the issue:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fff"
card_view:cardBackgroundColor="#fff"
android:layout_margin="2dp">
Notice these lines:
xmlns:card_view="http://schemas.android.com/apk/res-auto"
and
card_view:cardBackgroundColor="#fff"

I had the same issue on android 4.1.2 device. I was using an ImageView with shape drawable inside CardView which was the actual culprit.
Please check the answer in this link which helped me fix the issue.

In my case, I put android:theme="#style/Theme.AppCompat.Light.NoActionBar" in the manifest file for related activity

Don't use
android:Theme.Dialog
or
android.R.style.Theme_Dialog
, if your CardView is a part of the DialogFragment or Dialog layout.

Related

cardBackgroundColor and cardCornerRadius not working in AndroidX

I'm struggling with CardView corner radius and background color with AndroidX libraries.
I've defined my layout as below:
<?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"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="#dimen/retail_card_width"
card_view:cardCornerRadius="#dimen/card_radius"
card_view:cardBackgroundColor="#color/baseYellow"
android:layout_height="#dimen/retail_card_height">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_width="match_parent" android:layout_height="match_parent"
tools:src="#drawable/ic_fruit_1"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:scaleType="fitEnd"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="#+id/ivRetailBrand"
android:layout_width="#dimen/brand_icon_size"
android:layout_height="#dimen/brand_icon_size"
tools:src="#drawable/esselunga"
android:layout_marginTop="#dimen/retail_brand_margin"
android:background="#drawable/round_outline"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="#dimen/retail_brand_margin"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Unfortunately, neither cardCornerRadius nor cardBackgroundColor seem working on my layout. I can't understand if my issue depends on AndroidX libraries or not.
Here there's my layout preview:
I removed this lines in my Manifest file and after that my cardviews are worked prefectly fine
android:hardwareAccelerated="false"
Try modifying the CardView as:
<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:layout_width="#dimen/retail_card_width"
app:cardCornerRadius="#dimen/card_radius"
app:cardBackgroundColor="#color/baseYellow"
android:layout_height="#dimen/retail_card_height">
It turned out that the problem was I was mixing cardview androidx library with support recyclerview library. Once I rebuilt the project with:
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
everything turned out fine.
I might be late for this but let me save someone's time in the future. I had this problem on my Android Studio preview and came to realise that the problem is that the content inside the CardView is not clipped to the bounds of the CardView, so the corners you see are corners of the ImageView, which is actually clipped when you run the app on emulator/device.
I hope this helps!
I had a similar problem. I solved it by wrapping the CardView in FrameLayout.
Addition to an answer:
Or you can try using "app" namespace instead of "card_view":
app:cardBackgroundColor="#color/background_color"
app:cardCornerRadius="#dimen/corner_radius"
I don't think it's related to the CardView setup.
Check Your ImageView they can be the reason for corner's not being rounded.
Comment Out Image View Code(whole ConstraintLayout) and check the UI.As i had the Same Issue in Past.
try to clean and rebuild the project. This worked for me..
You can create a new drawable file round_corner_colored.xml and paste the following code in that file :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#color/light_sky_blue"/>
<corners
android:radius="#dimen/_12sdp"/>
</shape>
Now set this drawable the background of cardview as :
android:background="#drawable/round_corner_colored"
Hope it will help !!
There are brands that force Dark mode when app installed for the first time this will affect the coloration of views and applying color in CardView will get ignored. You may apply this to your main theme to prevent such event.
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
For me, It wasn't showing properly in Preview. But it was showing properly on the device. There is some rendering issue. I keep encountering such problems in android studio.
Faced issue in Android Studio 4.2.1 (Mac)
For me this worked
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/white"
app:contentPadding="#dimen/_10sdp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true"
android:layout_marginStart="#dimen/_20sdp"
android:layout_marginTop="#dimen/_10sdp">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/low"
android:textColor="#color/text_color_blue"
android:textSize="#dimen/_11ssp"
android:textStyle="normal" />
</androidx.cardview.widget.CardView>
Ok, So I ran into the same issue, and none of these suggestions resolved my issue. I was setting the background color of the card via cardview.setBackgroundColor(color):
, instead of cardview.setCardBackgroundColor()
. The cardview contains an embedded object, of card, the card is where the corner radius is appended to. So setting the color outside of the CardView.Card will not render the corner-radius as intended. Hope this helps.
I upgraded compileSdkVersion from 28 to 29 in app level gradle and the problem solved.
compileSdkVersion 29

Android ConstraintLayout #dimens replaced with hardcoded values

Heyho mates,
currently I am learning the new layout editor of Android Studio with the new ConstraintLayout.
By the way, I hate it.
But I got the issue, that if I want to specify a layout_height with #dimen, it gets replaced with a dp value instead.
Someone else got this issue?
Android Studio version 2.2.2 & 2.2.3 same issue.
Newest gradle version.
Thanks in advance guys!
Edit :
Code example :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="#dimen/imageViewHeight"
app:srcCompat="#drawable/accept"
android:id="#+id/imageView"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
Also happens outside scrollview.
Turns out this is a bug and will be fixed in Android Studio 2.3.
I think the reason behind this is, that ConstraintLayout as a WYSIWYG layout is inevitably meant to be edited/manipulated in the visual editor only.
Therefore keeping references to dimens in your dimens.xml would mean that as soon as you change something, move some elements around, these would not be relevant anymore and are hence replaced with "actual current values".
Also as mentioned in the docs:
All margins offered by the tool are factors of 8dp to help your views align to Material Design's 8dp square grid recommendations.

Element LinearLayout is not allowed in CardView

Note: PLEASE READ THE QUESTION before mark it as duplicate! I've mentioned that the similar question has not solved my problem.
I am using android.support.v7.widget.CardView for my layout xml. However when I place a LinearLayout within the CardView, Android Studio gives me the following error "Element LinearLayout is not allowed here".
In fact ANY types of widgets (like TextView) would cause this error, I have tried solutions from LinearLayout flagged if used in CardView link, which was to restart Android Studio. But it did not work. Below is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
And I will also post a screenshot!
What could be the problem??
So, a collection of suggestions, just to be sure:
restart AS
invalidating Caches & restart
Check if the right dependencies are added
clean & rebuild of project
try same code in another file/project
trying to find a backup of your project
maybe computer reboot
Just replace android.support.v7.widget.CardView with androidx.cardview.widget.CardView
Check if the Gradle dependency is added properly for the CardView.
In build.gradle
implementation 'com.android.support:cardview-v7:28.0.0'
And then in the XML
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#fff"
app:cardCornerRadius="2dp"
android:elevation="2dp">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/textView" />
</androidx.cardview.widget.CardView>
make sure that you did not inter constrains(magic stick) to card view because it will add constrains to card view and prevent any linear layout.

Android 5.0 does not respect view's margins

I have an app that runs well in any version of android, but in Lollipop seems that all views don't respect any margin or padding.
Does anyone know why??
Thanks in advance.
I'm declaring the CardView like this:
<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/card_view1"
android:background="#color/backgroundItem"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
card_view:cardCornerRadius="0dp"
android:layout_weight="1">
Per the CardView documentation:
Note that, if you specify exact dimensions for the CardView, because of the shadows, its content area will be different between platforms before L and after L. By using api version specific resource values, you can avoid these changes. Alternatively, If you want CardView to add inner padding on platforms L and after as well, you can set setUseCompatPadding(boolean) to true.

Android alert dialog not styled properly on Lollipop

I have tried everything to get this working and cannot figure it out.
I am trying to use an alert dialog im my app. It works fine on KitKat but not on Lollipop.
I have even tried using many material dialogs on GitHub and again they work on Kitkat but not on Lollipop.
I am testing on my Nexus 5 with stock nexus factory image.
KITKAT WITH GITHUB MATERIAL DIALOG
KITKAT WITH STOCK ALERT DIALOG
LOLLIPOP WITH GITHUB MATERIAL DIALOG
LOLLIPOP WITH STOCK ALERT DIALOG
Also this is the library on github installed on the same device its not working on. So its something about my app that is causing this. what could it be
android:fitsSystemWindows="true" was the culprit.
I had that declared in my styles.xml.
Removed it from styles.xml and placed in my layout and it working now.
I had the same problem and didn't find any fitsSystemWindows on any of my styles.xml.
To solve it i had to wrap the Layout in a FrameLayout and add the margins to the Layout like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/dialog_margin_title"
android:layout_marginBottom="#dimen/dialog_margin"
android:layout_marginLeft="#dimen/dialog_margin"
android:layout_marginStart="#dimen/dialog_margin"
android:layout_marginRight="#dimen/dialog_margin"
android:layout_marginEnd="#dimen/dialog_margin"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please enter the email address of the person you would like to follow, this person will be notified." />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
</LinearLayout>
</FrameLayout>

Categories

Resources