Search code examples
androidandroid-layoutandroid-linearlayoutandroid-relativelayout

Element LinearLayout is not allowed here - inside RelativeLayout


I keep getting an error that my LinearLayout isn't allowed in my RelativeLayout object. I am trying to create a multi-filter search bar. This LinearLayout is meant to hold different elements in the search bar. Here is my code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#212121"
        android:minHeight="?attr/actionBarSize">


        <TextView
            android:id="@+id/toolbar_title"
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/app_name"
            android:textColor="#FFF" />


    </androidx.appcompat.widget.Toolbar>


    <RelativeLayout
        android:id="@+id/view_search"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#50000000"
        android:clickable="true"
        android:visibility="invisible">

        <ProgressBar
            android:id="@+id/marker_progress"
            style="?android:attr/progressBarStyle"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerInParent="true"
            android:indeterminate="true"
            android:visibility="gone" />
    </RelativeLayout>

    <ListView
        android:id="@+id/listContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:clipToPadding="false"
        android:divider="#fff"
        android:paddingTop="56dp"
        android:visibility="gone" />

    <androidx.appcompat.widget.CardView
        android:id="@+id/card_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:visibility="invisible"
        card_view:cardCornerRadius="2dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/linearLayout_search"
                android:layout_width="match_parent"
                android:layout_height="48dp">

                <ImageView
                    android:id="@+id/image_search_back"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:background="?android:attr/selectableItemBackground"
                    android:clickable="true"
                    android:padding="12dp"
                    android:src="@mipmap/ic_arrow_back" />

                <EditText
                    android:id="@+id/edit_text_search"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#fff"
                    android:focusable="true"
                    android:gravity="center_vertical"
                    android:hint="@string/search_restaurants_and_cuisines"
                    android:imeOptions="actionSearch"
                    android:inputType="textCapWords"
                    android:maxLines="1"
                    android:paddingLeft="12dp"
                    android:paddingRight="8dp" />

            </LinearLayout>

            <View
                android:id="@+id/line_divider"
                android:layout_width="match_parent"
                android:layout_height=".5dp"
                android:layout_below="@+id/linearLayout_search"
                android:background="#eee" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_below="@+id/line_divider"
                android:divider="#FFFFFF" />
        </RelativeLayout>
    </androidx.appcompat.widget.CardView>


    <TextView
        android:id="@+id/txtNoResultsFound"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:padding="@dimen/activity_horizontal_margin"
        android:text="@string/no_results_found" />


    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:layout_marginBottom="@dimen/corners_small_value"
        android:layout_marginLeft="@dimen/corners_small_value"
        android:layout_marginRight="@dimen/corners_small_value">

    </ListView>

    <View
        android:id="@+id/toolbar_shadow"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_below="@+id/toolbar"
        android:background="@drawable/toolbar_shadow" />
</RelativeLayout>

Why is this happening? I made sure it is in the updated androidx CardView, so not sure why LinearLayout is still not allowed to be in the RelativeLayout inside CardView.


Solution

  • It looks like you have a typo in your CardView tag:

    <androidx.appcompat.widget.CardView
    

    That should look like this instead:

    <androidx.cardview.widget.CardView ...
    

    (Replace .appcompat. with .cardview.).