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.
Related
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
I am beginner in android studio and having headache with this if someone could help me am i missing something?.... I have problem with Constraint layout, the problem is i put some element(ImageView, Button it does not mater) in layout and constraint it to parent but when I run it in emulator it just is not showing. also it is not showing me those buttons under elements connect BaseLine and remove Constrains for that element.
"screenShot":
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="343dp"
android:layout_height="118dp"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_c`enter code here`onstraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#tools:sample/backgrounds/scenic[7]" />
</android.support.constraint.ConstraintLayout>
when i run it in emulator it just is not showing
The problem is here :
tools:srcCompat="#tools:sample/backgrounds/scenic[7]"
You need to understand what tools is, so I recommend to you to read a little bit the documentation.
Just a note of it :
It's a namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
Instead you have to use to show it on the emulator.
app:srcCompat="#drawable/your_drawable"
Note: remember to add : xmlns:app="http://schemas.android.com/apk/res-auto"
I found the solution.it's difrent in new Android Studio 3.4, You have to Right click on element and option Show Base Line is there now.
I am a beginner in android development. I have created a android project. But in the preview section, I am not able to see the components. But components are visible when i run the same app on emulator i am able to see the components. I am missing some setting here? XML is given below.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I've had have also this problem and solved it by only selecting another "Theme" cause
the default theme appTheme doesn't show added items.
If you i.e. select Transulcent, any item becomes visible ;-)
cheers
Oliwan
Maybe sound a weird answer, but self-tested this.
Change the XML file's (maybe activity_x.xml) layout's root tag android.support.constraint.ConstraintLayout to LinearLayout, then preview will start working again (as happened in my case). [If not, then perform force refresh (R)]
Then switch back to old one, I mean: Change the XML file's (maybe activity_x.xml) layout's root tag LinearLayout to android.support.constraint.ConstraintLayout. And force a refresh.
This does seem to be a bug in Android Studio. When it showed up for me, I fixed it by making a trivial change in the app build.gradle (change version code), do a gradle sync, then change back and sync again.
After that, the design view worked again.
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.
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.