Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CardView goes on top of FrameLayout, but declared first

I have simple FrameLayout with support CardView as first item, and TextView as second, so TextView must be on top of inflated view. This works on pre-Lolipop but on 21+ card takes toppest place in layout, why that's so and how to fix this? Same thing with RelativeLayout. Layout:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="wrap_content">      <android.support.v7.widget.CardView         android:layout_width="match_parent"         android:layout_height="200dp"         android:layout_margin="20dp"         app:cardBackgroundColor="#ff0000"         app:cardUseCompatPadding="false"         />      <TextView         android:layout_width="match_parent"         android:layout_height="100dp"         android:text="i am top view!"         android:gravity="center"         android:layout_gravity="center"         android:textSize="30sp"         android:textAllCaps="true"         android:textColor="#00ff00"         android:background="#0000ff"         />  </FrameLayout> 
like image 583
Konstantin Berkov Avatar asked Jul 25 '15 17:07

Konstantin Berkov


People also ask

Why does FrameLayout hold one view?

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

How do I customize my card view?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.

What is the difference between relative layout and FrameLayout?

RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets. TableLayout : is a view that groups its child views into rows and columns. FrameLayout : is a placeholder on screen that is used to display a single ...

Is FrameLayout a ViewGroup?

Android Framelayout is a ViewGroup subclass which is used to specify the position of multiple views placed on the top of each other to represent a single view screen.


2 Answers

In case someone gets here and the solution for setting elevation doesn't work for them (like in my case, where I needed to draw an image above the CardView and having a shadow on it was not acceptable), you can solve the issue by wrapping the CardView inside another FrameLayout. In the example provided, it would look something like this:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="wrap_content">      <!-- This is the added FrameLayout -->     <FrameLayout         android:layout_width="match_parent"         android:layout_height="wrap_content">          <android.support.v7.widget.CardView             android:layout_width="match_parent"             android:layout_height="200dp"             android:layout_margin="20dp"             app:cardBackgroundColor="#ff0000"             app:cardUseCompatPadding="false"             />      </FrameLayout>      <TextView         android:layout_width="match_parent"         android:layout_height="100dp"         android:text="i am top view!"         android:gravity="center"         android:layout_gravity="center"         android:textSize="30sp"         android:textAllCaps="true"         android:textColor="#00ff00"         android:background="#0000ff"         />  </FrameLayout> 
like image 148
Ereza Avatar answered Oct 17 '22 07:10

Ereza


I might be joining the discussion a bit late, but if you can afford giving up the CardView's elevation, you can just set the cardElevation property of the CardView in your XML layout to 0dp.

Like so:

app:cardElevation="0dp" 
like image 34
Itamar Avatar answered Oct 17 '22 07:10

Itamar