Android UI component - android

Hello can you tell me how can I add the rectangular box in the android layout which adds up like a 3d effect as in the below screenshot. Thanks.

You can add this effect to almost every view or widget, using android:elevation attribute, or programmatically using setElevation(x) method. X is number of Dp.
Note: This only works on api level 21 or newer.

you are referring to material design you can add this effect using card view

Add elevation to your view
read this documentation for assigning elevation to your view
https://developer.android.com/training/material/shadows-clipping.html

Add dependency compile 'com.android.support:cardview-v7:23.0.1'
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#color/grey_300"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">

Related

Android Widget Shadow which supports from API 16

I want to have shadow in Android widget like button, edittext, searchview or anything. I have no idea about android elevation. I think it only supports from API 23. If there is any proper solution then please provide.
here is my demo widget. I need the shadow in border of my searchview.
<android.support.v7.widget.SearchView
android:id="#+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroud="#android:color/white" />
You can get the shadow effect by adding this to the widget:
android:background="#android:drawable/dialog_holo_light_frame"
Got the answer from here: https://stackoverflow.com/a/30931750/8466860.
Place your views inside CardView. Using CardView you can add elevation, rounded corners etc. It supports back till API 7.
to use CardView, add it via your app level gradle..
dependencies {
...
compile 'com.android.support:cardview-v7:25.3.1'
}

Card view shadow is not showing

This is my card_view. I have mentioned card_view:cardElevation.
But still shadow is not showing.
I have searched many links. Everywhere they have mentioned to use card_view:cardElevation.
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#color/white"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false">
</android.support.v7.widget.CardView>
Can you tell what I am missing here?
Read https://developer.android.com/training/material/lists-cards.html
To create a card with a shadow, use the card_view:cardElevation
attribute. CardView uses real elevation and dynamic shadows on Android
5.0 (API level 21) and above and falls back to a programmatic shadow implementation on earlier versions. For more information, see
Maintaining
Compatibility.
UPDATE
try adding margin to card if you want to see shadow
check https://developer.android.com/reference/android/support/v7/widget/CardView.html
Set the following attribute in your cardview
card_view:cardUseCompatPadding = "true"
card_view:cardElevation = "5dp"

Shadow of CardView not visible on Android Lollipop

Following code works perfectly on Kitkat, but shadows are not visible in Lollipop.
Actually I can see shadow in Android Studio Preview, but not while running on a device/emulator. I'm using CardView for adapter of ViewPager (android.support.v4.view.ViewPager)
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="6dp">
Actually it also shows shadow in L version, but it is based on elevation so you can't see the shadow if card height is match parent
try adding margin to card if you want to see shadow
Found the solution. I'm able to get the shadow effect using following code.
android:background="#android:drawable/dialog_holo_dark_frame"

Android 5.0 does not respect view's margins

I have an app that runs well in any version of android, but in Lollipop seems that all views don't respect any margin or padding.
Does anyone know why??
Thanks in advance.
I'm declaring the CardView like this:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view1"
android:background="#color/backgroundItem"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
card_view:cardCornerRadius="0dp"
android:layout_weight="1">
Per the CardView documentation:
Note that, if you specify exact dimensions for the CardView, because of the shadows, its content area will be different between platforms before L and after L. By using api version specific resource values, you can avoid these changes. Alternatively, If you want CardView to add inner padding on platforms L and after as well, you can set setUseCompatPadding(boolean) to true.

Strange CardView behaviour

I'm trying to implement simple cardview with semi-transparent background.
<android.support.v7.widget.CardView
android:id="#+id/card_view"
app:cardBackgroundColor="#33FFFFFF"
app:cardCornerRadius="4dp"
android:foreground="#33FFFFFF"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="100dp">
......
</android.support.v7.widget.CardView>
And this is the result in L and preL
android L: (Nexus 5)
Pre L:
Any idea how to fix the pre L to look like L ?
As you can see there are too many borders/strokes surrounding the card.
I'm can guess it relates to shadows and stuff...
Thanks.
Look like it's a known defect in google forums
Thanks to #benhylau for providing implementation for cardview with alpha here
There no possible to color cardview background with #__FFFFF as it ignores the transparency values. need to work with alpha attribute

Categories

Resources