how to fix CardView Corner radius issue? - android

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"

Related

Elevated base android

I'm trying to add an elevated base to an layout and I can't manage to do that. If someone know how to help me a little it would be appreciated.
This is the base I want to get:
You can simple achieve this using MaterialCardView by following code snippet :
<com.google.android.material.card.MaterialCardView
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="100dp"
app:cardCornerRadius="0dp"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Rest of your layout code goes here-->
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
Happy Coding :)

Android Constraint Layout showing attribute 'android:layout_constraintWidth_percent' not found

I searched a lot for this error but ended up seeing that everyone is suggesting of uninstalling android studio and reinstalling it with all sdk work which is not feasible to me. How can we remove this error in a decent way, please refer me to the answer if anyone has already given the right one.
Please use app instead of android.
app:layout_constraintWidth_percent="0.5"
Also, Add one constraint property like below:
app:layout_constraintStart_toStartOf="parent"
or
app:layout_constraintTop_toTopOf="parent"
your_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<Button
android:id="#+id/btn_temp"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintWidth_percent="0.5"
android:text="Hello World!"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Cardview shadow not appearing in lollipop devices?

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...

Updated Android SDK and Android Studio messed up all my XMLs: "Element XXX not allowed here"

I just updated my Android Studio and Android SDK, and now I can't do anything in my XMLs. It shows the same thing as previous, but I can't add anything and something got messed up.
Example:
<?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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/bocterAppLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/bocterapp"
android:contentDescription=""/>
</LinearLayout>
It error says: "Element ImageView is not allowed here".
Do you have any idea why this has happened?
Cheers!
I had the same problem. Try to exchange the ImageView with the android.support.v7.widget.AppCompatImageView! That solved it for me.
I also had the same problem. In my case, I was using a container from the old support library. So changing
<android.support.constraint.ConstraintLayout ... />
to
<androidx.constraintlayout.widget.ConstraintLayout ... />
solved it for me - with no change to the ImageView.

Android elevation property not working

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.

Categories

Resources