Am using the cardview in my android app. However the shadow is not showing. Here is the xml layout
The default optionsmenu shadow also not showing.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ECEDF0"
android:orientation="vertical" >
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clipChildren="false"
card_view:cardBackgroundColor="#color/white"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="14dp"
card_view:cardUseCompatPadding="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Google Play" />
</android.support.v7.widget.CardView>
</LinearLayout>
After going through the docs again, I finally found the solution.
Just add card_view:cardUseCompatPadding="true" to your CardView and shadows will appear on Lollipop devices.
What happens is, the content area in a CardView take different sizes on pre-lollipop and lollipop devices. So in lollipop devices the shadow is actually covered by the card so its not visible. By adding this attribute the content area remains the same across all devices and the shadow becomes visible.
My xml code is like :
<android.support.v7.widget.CardView
android:id="#+id/media_card_view"
android:layout_width="match_parent"
android:layout_height="130dp"
card_view:cardBackgroundColor="#android:color/white"
card_view:cardElevation="2sp"
card_view:cardUseCompatPadding="true"
>
...
</android.support.v7.widget.CardView>
As mentioned there CardView not showing Shadow in Android L
make sure that you are drawing your view using hardwareAccelerated = true
hardwareAccelerated = true
hardwareAccelerated = false
See Android Hardware Acceleration for details
For Lollipop and higher you should add some margins to the card:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
/>
Since the shadow lies outside of the actual view
for someone ,
one other thing that you should be aware of, shadows will not show if you have this line in the manifest:
android:hardwareAccelerated="false"
I tried all of the suggested stuff but it only worked for me when i removed the line, the reason i had the line was because my app works with a number of bitmap images and they were causing the app to crash.
Please try to put android:hardwareAccelerated="false" androidManifest file may solve your issue because i also faced same issue and solved by adding 1 line only in manifest.
Hey friends I got the solution of above problem.Just add this code in your xml.
<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:layout_width="match_parent"
android:layout_margin="8dp"
android:id="#+id/griditem"
android:layout_height="match_parent"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="3dp">`
I hope it will helpful for you...
Related
I'm using below code to create a CardView with corners but it stay rectangular
<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/cardview_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="6dp">
UPDATE : there was a mistake in my Manifest i just remove this line:
android:hardwareAccelerated="false"
I also had same issue. The issue I found is...
android:hardwareAccelerated="false"
removed this line from manifests and the issue resolved for me.
I saw nothing wrong with your code, but if it couldn't worked. try to add this:
app:cardCornerRadius="#dimen/margin_small"
app:cardElevation="#dimen/margin_small"
app:cardUseCompatPadding="true"
It worked well for me. And make sure you're runing above API 21.
Try this :
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="4dp">
</android.support.v7.widget.CardView>
reference link : check here
try it into style.xml
<item name="cardCornerRadius">9dp</item>
or simply put this line in your card view XML
app:cardCornerRadius="9dp"
Try this
<android.support.v7.widget.CardView
...
card_view:cardCornerRadius="20dp">
</android.support.v7.widget.CardView>
Perhaps 6dp radius is not noticable. :)
Suggestion
You need not to make many namespaces, you can use app for every attribute.
Just use this
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="20dp"
Using Android studio 3.1, and I have a strange problem, I can't edit my UI,
I enter the content_main.xml, using the design tab, blueprint, anything i drag to the blueprint is not appearing, I use the infer constraints, still not helping
this is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_main">
<EditText
android:id="#+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
tools:layout_editor_absoluteX="53dp"
tools:layout_editor_absoluteY="168dp" />
</android.support.constraint.ConstraintLayout>
I would't recommand using the UI designer, you don't get the feeling of what you are really doing with it. You should learn how to directly edit the XML file, this isn't really harder and at least you know what you are doing.
You must drag the widget to the component tree
ver imagen
Hi I am a newbie to Android Programming using AS. This should be a common/easy problem but I have not found a solution so far.
I was creating left_ and right_fragments tags in activity_main.xml for tablets, in which I wanted to have a background color in right_fragment(temporarily set to #ffffff and keep left_fragment as default). Then the Android Lint warned that I got an Overdraw: Painting regions more than once on the right_fragment:
I tried to create a new theme:
<style name="NoBgTheme" parent="android:Theme.Material.Light.DarkActionBar">
<item name="android:windowBackground">#null</item>
</style>
and added android:theme="#style/NoBgTheme" to right_fragment.xml, but it did not work.
Screenshot of this app w/ Debug GPU Overdraw enabled.
If I change android:theme= from "#style/AppTheme" to "#style/NoBgTheme" in AndroidManifest.xml, it worked, but all other layouts lost their backgrounds.
Any idea how to fix this OVERDRAW? Thx in advance.
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal">
<fragment
android:id="#+id/left_fragment"
android:name="jerryc05.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/right_fragment"
android:name="jerryc05.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
left_fragment.xml is so simple that we can just ignore it.
right_fragment.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:theme="#style/NoBgTheme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<TextView
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="This is right fragment"
android:textSize="20sp"
tools:ignore="HardcodedText" />
</LinearLayout>
AndroidManifest.xml remains unchanged.
MainActivity.java remains unchanged.
A little bit of overdraw will happen. You need to set the nobgtheme to your main activity if you want to fix the overdraw on your left fragment and right fragment display. If this only happens during tablet layouts you can check to see if this is a tablet layout being used and set the no background theme at runtime using methods described here: How to change current Theme at runtime in Android
Alright, I got an alternative solution.
I added android:theme="#style/NoBgTheme" after <activity android:name=".MainActivity" before >.
Then I manually specified android:background="#f0f0f0" and android:background="#ffffff" to left_ and right_fragment.xml respectively.
It worked so well!!!
But the Android Lint Warning still exists...(is this a bug or still my fault?)
Since upgrading to SDK 21 I've been trying to use the elevation property but it never seems to work.
I want to elevate a frame above others so I'm setting the following
android:elevation="4dp"
but there's no sign of shadow. I've tried buttons and framelayouts but not got any elevation
So here's the full tag
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/panel_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:elevation="4dp"
>
Am I missing something else I need to add
Make sure that the background for the container holding the FrameLayout is not transparent.
According to this Z = Elevation + TranslationZ
so try this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/panel_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:elevation="4dp"
android:translationZ="4dp">
You need to add a background to it something like:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/panel_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
android:background="#drawable/dialog_background">
Instead of using white color. Otherwise the shadows won't work. Also, is it not necessary to use android:translationZ attribute since that just adds to the elevation, but is not required if you don't want animation.
Image as seen in ICS
Where as when run in Gingerbread
XML code for the fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background"
android:id="#+id/homelayout" >
<ImageView
android:id="#+id/homepageLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/background"
android:alpha="255"
android:scaleType="fitXY" />
</LinearLayout>
Any reason for such strange behavior?
try changing
android:layout_width="match_parent"
android:layout_height="match_parent"
to
android:layout_width="wrap_content"
android:layout_height="wrap_content"
in your ImageView. I don't know how you use your XML, but by the looks of it you might also want to change te same parameters in the LinearLayout to "wrap_content". Now you are telling the ImageView to (probably) fill the whole area.
// Alex
The problem is observed only when you dynamically request for the actionbar from the code.
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
If you do it using styles, this problem is not seen.