12

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!

enter image description here

What could be the problem??

Community
  • 1
  • 1
Joel Min
  • 3,387
  • 3
  • 19
  • 38
  • Possible duplicate of [LinearLayout flagged if used in CardView](http://stackoverflow.com/questions/27440887/linearlayout-flagged-if-used-in-cardview) – yennsarah Nov 12 '15 at 13:50
  • 1
    I've already mentioned in the question that I've looked into that link and it did not solve my problem. – Joel Min Nov 12 '15 at 13:51
  • You haven't mentioned the link when I marked it as duplicate. Seems that there is an open issue: https://code.google.com/p/android/issues/detail?id=81362, which Android Studio version do you use? – yennsarah Nov 12 '15 at 13:54
  • I did mention the link, it's just that I did not actually name it as " LinearLayout flagged if used in CardView". I am using 1.5 RC 1, but the same thing was happening before this version. – Joel Min Nov 12 '15 at 13:55
  • This [post](http://stackoverflow.com/questions/32050016/views-getting-flagged-inside-cardview) suggests to use a backup version of your project, which is not a acceptable workaround, but maybe it'll help you. – yennsarah Nov 12 '15 at 14:00
  • And I assume you already tried the obvious solutions - clean&rebuild, invalidate cache & restart, maybe computer reboot, trying to do this in a new layout file/deleting this layout file & creating a new one..? – yennsarah Nov 12 '15 at 14:08
  • 4
    Thanks for your suggestions and while I was doing what you suggested I found out that AS could not compile CardView widget because correct dependency was not added. I had to add `compile 'com.android.support:cardview-v7:23.1.0'` in my gradle file. – Joel Min Nov 12 '15 at 14:25
  • If you post your comments as answer I will upvote & accept it as answer, thanks! – Joel Min Nov 12 '15 at 14:25
  • Apart from this suggestions, refer to my answer here, https://stackoverflow.com/questions/27440887/linearlayout-flagged-if-used-in-cardview/57663140#57663140 – Infinite Loops Aug 26 '19 at 18:34

4 Answers4

18

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
yennsarah
  • 5,467
  • 2
  • 27
  • 48
3

Just replace android.support.v7.widget.CardView with androidx.cardview.widget.CardView

Vikram Kodag
  • 485
  • 5
  • 6
2

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>
Sukitha Udugamasooriya
  • 2,268
  • 1
  • 35
  • 56
0

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.