Two views with same elevation side-by-side - android

I have two views with the same elevation beside each other. My wanted behaviour is that they won't cast a shadow over each other as they have the same elevation, however, what is happening is that the view on the left, casts a shadow on the right. They are not the same size so I can't put them both in another view and apply an elevation to that view.
Is this the expected behaviour? Is there a way round it?
Edit:
I just recreated with simpler views, here is the code.
I also noticed it has the expected behaviour if I have the view directly in the layout and don't include it as I did it in this example and as I need it to work.
<LinearLayout
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"
android:orientation="horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#android:color/holo_green_dark">
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#android:color/holo_red_dark"
android:elevation="24dp"/>
<include layout="#layout/test"/>
</LinearLayout>
And here is the include:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#android:color/holo_red_dark"
android:elevation="24dp"/>
</LinearLayout>
And the screenshot:

See the hierarchy you have:
So you have applied elevation to 1 and 3, which are not siblings. Apparently, if one view is higher in the hierarchy, than it should cast a shadow, regardless those views have same elevation or no.
Had you applied elevation to 2 instead of 3 you would not see shadow effect.
So if you just change your test.xml to this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="24dp">
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#android:color/holo_red_dark"/>
</LinearLayout>
You'd get this output:

Related

How to remove default shadow in ListItemPresenter in android TV

how to remove the default black shadow (black shadow or each corners) in ListItemPresenter,
attaching image of presenter view
here is xml of the item presenter:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#android:color/transparent"
app:cardCornerRadius="16dp"
android:layout_width="200dp"
android:layout_height="200dp">
<androidx.cardview.widget.CardView
android:background="#android:color/transparent"
app:cardCornerRadius="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/block_bg_state"
android:gravity="center"
android:orientation="vertical">
...
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
If you don't want a shadow, then you don't need to use a CardView. It's meant as a "wrapper" to add elevation (a shadow) and rounded corners to the view it holds.
If this is your complete layout, you can also remove the LinearLayout and add the layout width/height directly to your ConstraintLayout.

RecyclerView's scrollbar not appearing on edge of screen

I'm trying to add a margin around my RecyclerView whilst enusring it can scroll vertically but for some reason the scrollbar does not position itself at the end/right of the screen. Is this an issue specific to Android X?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingEnd="#dimen/activity_horizontal_margin">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"/>
</LinearLayout>
Is this an issue specific to Android X?
Not really.
The issue here is you are specifying padding on the container of the RecyclerView.
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingEnd="#dimen/activity_horizontal_margin"
This is because the rect used to draw the scrolls bars are limited to the rect where is drawn the RecyclerView. This is also the reason because the scroll bars restraint on the top.
Remove these and you are going to obtain the scroll bars positioned on the border of the screen.
Check also this related question
Explanation:
The bar is in the recyclerview, the recycler is constrained by the padding.
Quick fix:
Add padding to the recylerview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:paddingEnd.../>
</LinearLayout>
Good practice:
List should be full width so user interactions are not hindered by unknown spacings. Add the padding to the item you are inflating in the adapter.

The item position in the RecyclerView is not saved

I use the mode adjustResize, RecyclerView and EditText, when the keyboard appears, the window shrinks but the visible elements in the RecyclerView go down.
The structure of the layout looks like this:
<LinearLayout
android:orientation="vertical">
<android.support.v7.widget.RecyclerView />
<LinearLayout
android:orientation="vertical"
android:layout_gravity="bottom"/>
</LinearLayout>
I set setStackFromEnd to true, in LinearManager but it does not help.
Versions of libraries: 25.3.0
How can this unpleasant problem be avoided?
instead of using linear layout on top use Relative layout like following...
<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/bottom_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottom_view"/>
</RelativeLayout>

How to force SurfaceView to occupy specific section othe screen

I am trying to use SurfaceView. So I created a SurfaceView widget in the layout file as shown below in the code. The problem I have is how to set a weight to the SurfaceView?
As shown below in the code I set :
android:layout_weight=".4"
but still the SurfaceView occupies the eintire screen, what i want is to make the SurfaceView occupies only 40% of the screen. I also refered to a post in this link SurfaceView in Layout
but it does not show how to set the weight to the SurfaceView from the xml file.
Please let me know how to set weight to the SurfaceView from the xml file.
code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.surfaceview_00.MainActivity">
<SurfaceView
android:id="#+id/mainActivity_surfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"/>
</RelativeLayout>
update:
I also tried the below code but still i got one SurfaceView that occupies the whole screen. I expected after running the below code is to have two SurfaceViews each occupies half of the screen, but i got only one
code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.surfaceview_00.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<SurfaceView
android:id="#+id/mainActivity_SurfaceView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<SurfaceView
android:id="#+id/mainActivity_SurfaceView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
RelativeLayout does not support layout_weight. You need to use the LinearLayoutfor that.
Read more at https://developer.android.com/guide/topics/ui/layout/linear.html
But basically change it to something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:weightSum="1">
<SurfaceView
android:id="#+id/mainActivity_surfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"/>
</LinearLayout>
android:layout_weight is only used by LinearLayout. Since you use RelativeLayout it is ignored.
SurfaceView doesn't belong to App's view hierarchy. It creates separate layer on the background, and position it depending on its size. All other views are displayed over SurfaceView. To do what you want you need to use TextureView.

Making CardView width match parent even when elevation > 0

I wanted a cardview similar to this:
I wanted my CardView separated like that. The problem is, I don't know how to make CardView go edge to edge or match the width of the CardView to its parent. If I put shadows or setting the elevation at 4dp/8dp, the entire CardView adjusts to show the shadows on both end. Which is not what I want. This is worse when viewed on pre-lollipop.
I am believing this is just thick line separating it which can be done creating custom view with a custom drawable. But can this be done with just CardViews? I need my CardViews to go to the most edge and show separation at the bottom.
Here is my current layout. I created a separate layout file for each CardViews and included them in my fragment like this:
<LinearLayout
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"
android:fillViewport="false"
android:fitsSystemWindows="true"
tools:context="com.neonwarge.android.notifire.activity.TaskActivity"
android:orientation="vertical">
<include
layout="#layout/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<include
layout="#layout/cardview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
....
</LinearLayout>
And here is what generally a CardView looks like:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:layout_marginTop="#dimen/activity_vertical_margin">
...
</RelativeLayout>
</android.support.v7.widget.CardView>
</merge>

Categories

Resources