Does CardView's elevation depends upon the position on the screen? - android

I have more than 3 CardView displayed on a screen as an item of a RecyclerView.
I have realized recently that the elevation of the CardView is different for a different card or the position of the card view on the screen.
You can see it in the picture attached to this question:
Blue CardView - the top shadow of the card has a clear darkgrey line and the bottom line has less shadow compared to others.
Yellow CardView - the top shadow of the card has a less clear grey line and the bottom line has dark shadow compared to others.
Red CardView - the top shadow of the card has a negligible grey line as compared to others.
Is it normal or am I doing something wrong?
The CardView layout is same for all the three as they are in RecyclerView:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="#ffffff"
app:cardElevation="1dp"/>

Related

Android RecyclerView fading edge appearing abruptly and using padding incorrectly

I'm currently converting a ListView in my Android app to use RecyclerView instead, but am having difficulty recreating the fading edge effects used by the former.
Firstly, I've set the RecyclerView to have bottom padding and use clipToPadding="false" to make sure the padding is only visible when you scroll to the bottom of the list, which is working. However, the fading edge does not appear along the bottom edge of the RecyclerView's visible box, instead it acts as though clipToPadding="true" and appears a little way up the RecyclerView.
Secondly, the fading edge does not smoothly grow/fade in when you start scrolling as it does for ListView, instead appearing suddenly at its full length and intensity as soon as you begin scrolling.
Here is the code I'm using:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/lists_overview_recycler"
android:background="#android:color/transparent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fastScrollEnabled="true"
android:clipToPadding="false"
android:requiresFadingEdge="vertical"
android:fadingEdgeLength="12dp"
android:drawSelectorOnTop="true"
android:paddingBottom="48dp"/>

Android xml avoid overlap constraintLayout border radius by child constraintLayout

I have some issue with follow structure:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/white_rectangle_radius10">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/first"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
/// many views here
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/second"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/first"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
/// many views here
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/third"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/second"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
/// many views here
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Programatically it can be 3 inner constraints or 4 or 2.
Root Constraint has round border radius.
Programatically when one of inner items clicked I set to this item background color, BUT when its first or last it overlap root border radius.
Note:
No need to suggest cardview as root element (not my option)
"Smart play" with margins/paddings also doesn't suit me because it should be changed programatically.
Is there some option to do it via XML?
When I added background color to first element it's top start/end corners not rounded.
First it is not recommended to nest layouts if you are going to use constraintLayout.
Second is how to design your view smoothly?
one of constraintlayout benefits is using the drag and drop option, therefore, start by doing that and ensure to place every item in its right place (but do not apply any constraint yet).
once all items are on the layout, click any item and look at the tool bar above the mobile screen, there is a magic wound, click on it, the constraint will be generated automatically based on the way you orgnized the view on the screen.
once that is done, now you might need to adjust the margins a bit according to the requirements.
also please assign id to each view before clicking the magic wound.
I hope I was helpful.

Clarification about Android clipToPadding property

I was working with animations recently, and got into trouble, when FrameLayout did not show its shadow properly due to weird reasons.
I found a good answer, which helped me, but there was used a property called clipToPadding. Answer can be found here: Android “elevation” not showing a shadow
However, I really wanted to understand the purpose of this property. I went to documentation of Android, which states that:
Defines whether the ViewGroup will clip its children and resize (but
not clip) any EdgeEffect to its padding, if padding is not zero. This
property is set to true by default.
I have read it for many times and I looked for examples on the web, but I really failed to find some. The only visuals I found was similar to this ClipToPadding difference
I can see the effect on the list, but how can it affect, when there is only one View in the ViewGroup for example etc.
It would be nice if someone could provide me a few key points about this property and how it works with a few examples.
clipToPadding basically means "If the view draws beyond its bounds into the padding area, overwrite this drawing with the parent's padding".
The shadow of an elevated view is drawn outside of the view's bounds. In order for it to be visible, you need to make sure that there is space around the view. You can achieve this by setting padding on the parent, but then you need to set clipToPadding=false so that the shadow is not overwritten.
As noted in this answer, this is also useful when you have a scrollable view such as a ListView or RecyclerView. When scrolling the list will draw contents out of its bounds. If you set clipToPadding=false, you'll be able to see that content instead of the padding, and only see the padding when you've completely scrolled up or down.
As an example, let's set an oval shape as background of a view and let's elevate this view. We'll also set some padding on the container. The screenshot below was taken with "Show layout bounds" activated in the Developers options. The area between the two red rectangles is the parent's padding.
This is how it looks with clipToPadding=true, notice how the shadow is clipped at the bottom, where it would lie between the two red rectangles:
With clipToPadding=false, we see the whole shadow:
Here is the XML I used:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#8888ff">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:clipToPadding="false"
android:layout_centerInParent="true">
<View
android:layout_width="170dp"
android:layout_height="170dp"
android:background="#drawable/oval"
android:alpha="0.5"
android:elevation="5dp"
/>
</RelativeLayout>
</RelativeLayout>
And here is is the oval drawable:
<shape android:shape="oval">
<solid android:color="#f2f2f2"/>
</shape>

Modify cardview padding

For card elevation to be visible, I use the attribute card_view:cardUseCompatPadding="true" in cardview XML. It added extra padding on all four sides of the cardview, but I do not need the bottom padding since the card below every card will already have a top padding. So currently it is like double padding between two cards because of top+bottom padding. How to reduce it by half?
P.S android:layout_marginBottom="-5dp" doesn't seem to work
Only use a margin at the bottom for the cards and set a padding to the top of your RecyclerView/ListView holding the cards:
<android.support.v7.widget.RecyclerView
...
android:paddingTop="5dp"
android:clipToPadding="false" />

Overlay Image over view corner

I am attempting to draw the medal overlaying the corner of the score screen shown here
There is an transparent layout holding the buttons and a RelativeLayout (black edge box), which holds the interior box (grey box) that contains the rest of the data. I tried adding the medal to the top left corner of the interior RelativeLayout and giving it negative margins, but that just cuts it off at the edge of the view. Adding it to the transparent layout puts it behind the corner.
How can I force the medal to overlay that corner? I'd prefer to do it in xml if possible, but any suggestions are welcome.
You can also achieve this by setting the android:clipChildren="false" and android:clipToPadding="false" attributes on the parent view (and if that doesn't work, set it on all the ancestor view groups as well, eventually it will work).
In the xml layout, an element that define after will overlay the element that define before.
So, you could change layout to something like this:
<LinearLayout>
<LinearLayout> <!--grey box --> </LinearLayout>
<ImageView src="medal" android:layout_marginLeft="-250dip"/>
<!-- change the amount of marginLeft to your desire -->
</LinearLayout>
merge them ...
http://developer.android.com/resources/articles/layout-tricks-merge.html would help

Categories

Resources