Incorrect overlay behavior in FrameLayout - android

According to the docs, child views in FrameLayout are drawn on top of each other with the most recently added view on top. I have notice though, that this seems to not be accurate in Lollipop or later. For example, in the following xml, the button is still visible even though it should be covered by my custom view.
It is worth mentioning that although my custom view extends FrameLayout, I do inflate a child view, so my FrameLayout isn't empty.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/some_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="button"/>
<mycustomframelayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"/>
</FrameLayout>
Any idea what I am missing here?

Buttons in Lollipop and higher have a default elevation to them which causes them to always draw on top. You can change this by overriding the default StateListAnimator.
Try putting this into your button XML:
android:stateListAnimator="#null"
The FrameLayout should now cover the button.

Try putting an elevation to the FrameCustom:
<mycustomframelayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:elevation="100dp"/>

Related

How to have a scrollable floating Cardview like this?

Does anyone know how to have a floating Cardview like this?
http://chairnerd.seatgeek.com/images/autocomplete_checkout.gif
The background image should be able to change programmatically and the cardviews should be scrollable. And the position of the first Cardview should be somewhere below the image. Thanks in advance!
I figured it out myself and I will post my solution here in case anyone run into the same situation.
Here how the layout file should look like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:background="#color/bgGrey">
<ImageView
android:layout_width="match_parent"
android:layout_height="125dp"
app:srcCompat="#drawable/soccer"
android:id="#+id/imageView"
android:scaleType="centerCrop"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="120dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp">
EDIT: Within the LinearLayout, something like a place holder should be added. Otherwise a part of the content at the end would not be shown. So I used a textview to do so.
<TextView
android:layout_width="match_parent"
android:layout_height="120dp" />
Note: The height here should match the marginTop in the LinearLayout
Yes it is a cardView directly on a ScrollView, or a ListView simply with the item's layout with background transparency.
The scrollview/listview is placed on a FrameLayout or RelativeLayout. Either there is a padding/margin on top, or a "stub" first element which is transparent".
Bellow (declared first in the parent layout) the scrollview/listview you can place an image or any other static component whatsoever.
And above you can place other floating components (like the Check-out button on your example)

Filling a view that has `wrap_content` as height

So I'm using a RecyclerView, with some cards. I want to get an overlay on the card when it's clicked, which should cover all contents, but not change the size of the view. Hardcoding the size of the card is not possible, since the contents have different dimensions.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--contents and stuff-->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" />
</RelativeLayout>
The example above is pretty much what I'm trying to achieve, but this obviously won't work, since the View's and RelativeLayout's height refer to each other.
How can I make the View cover everything inside the RelativeLayout?
Edit
Clarification: The issue I'm having has nothing to do with clicking, covering or anything like that. I'm having issues with just covering the RelativeLayout.
Credits go to thunder413 for suggesting the FrameLayout in the comments.
Basically, all I had to do was encapsulating the RelativeLayout (as shown in my question) in a FrameLayout, and move the View out of the RelativeLayout into the FrameLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--contents and stuff-->
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" />
</FrameLayout>

Attach view to custom RelativeLayout in Android

I have a RelativeLayout with a match_parent main LinearLayout view and a secondary wrap_content LinearLayout which is initially not visible (gone).
The secondary one has to be shown at the top of the screen but declaring it at the beginning of the xml file, it is not shown even when set to visible because it's behind the main one.
That's why it's declared after the main LinearLayout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- FULL SCREEN CONTENT -->
</LinearLayout>
<LinearLayout
android:id="#+id/secondary_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/f_white_transparent"
android:gravity="center"
android:visibility="gone">
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
</LinearLayout>
</RelativeLayout>
I would like to create a custom RelativeLayout to be used in all the xml files where I can obtain the same behavior and I can be able to put whatever content under the main layout.
Is there any way to do it still using xml?
You are using a RelativeLayout but do not use the benefits of it. You can declare the wrap_content-Layout in first place and add the parameter android:layout_alignParentBottom="true" to it.
In your match_parent-LinearLayout you can add the parameter android:layout_above="#+id/secondary_layout" to make sure it will give space to the secondary Layout. This way gone will work, too.

App Widget Layout warning

In develepoers guide it is advised to use the following layout for App Widgets.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/widget_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/my_widget_background">
</LinearLayout>
</FrameLayout>
Naturally Eclipse have the warning This LinearLayout layout or its FrameLayout parent is useless.
is it correct? Can I remove the LinearLayout?
It really depends on what the content of your widget is. Do you need a LinearLayout or a FrameLayout to position your content correctly? From you question one can't answer that question. But if it's a LinearLayout then the following is identical to the layout above:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="#dimen/widget_margin"
android:background="#drawable/my_widget_background">
</LinearLayout>
Please note that I added the layout_margin attribute instead of using the padding because the background will be drawn regardless of what padding you define.

Android ViewGroup clipping doesn't work

Why is android:clipChildren="false" not working? I want to achieve the same effect as the CSS overflow: visible so that the child views are visible even if they're positioned outside their parent layout. I tried something like this and although the Graphical Layout in Eclipse shows the correct behaviour, on the phone it doesn't work.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0000FF"
android:clipChildren="false"
android:clipToPadding="false" >
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:background="#FF0000"
android:clipChildren="false"
android:clipToPadding="false" >
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="120dp"
android:background="#00FF00" />
</RelativeLayout>
</RelativeLayout>
I'm, trying something similar for now and it works only when I use booth android:clipChildren="false" on all parents a next problem is if under overlapped child is something that repaint itself like ListView, Android then forgot repaint our child and it disappear :-(
Only way is call .invalidate() manually on overlapped child after every repaint of view under it :-(
I also found that at least small peace of overlapped child must be in the its parent.
And another problem is with receive touch/click events in view which is outside of the parent. I not found any solution for it.

Categories

Resources