Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layout_margin in CardView is not working properly

I am playing with the new CardView, but the margins don't seem to be applying.

<?xml version="1.0" encoding="utf-8"?>  <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_view" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="18dp" android:layout_marginLeft="18dp" android:layout_marginRight="18dp" card_view:cardCornerRadius="4dp" card_view:cardBackgroundColor="#FFA" card_view:layout_margind="4dp">  <LinearLayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="vertical">  <TextView     android:id="@+id/info_text"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="Hello World!"/>  <TextView     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="Hello World!"/>  <TextView     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="Hello World!"/>  <TextView     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="Hello World!"/>  <TextView     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="Hello World!"/>  </LinearLayout>  </android.support.v7.widget.CardView> 

By the way, I'm using it in a Fragment that is in a ViewPager. The card extends the entire width of the screen (match_parent) even though I am using android:layout_marginLeft="18dp" and android:layout_marginRight="18dp" on the CardView.

Any ideas what I might be doing wrong?

like image 443
Eric Cochran Avatar asked Jul 03 '14 22:07

Eric Cochran


People also ask

How do I customize my CardView?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.

Can we use CardView without RecyclerView?

Is it possible to implement a click listener on each button without using a RecyclerView? Yes, You can use cardview as menu item but you should use layout instead of menu.

What is card elevation in CardView?

Note that the card_view:cardElevation is used to determine the size and softness of the shadow so as to realistically depict the depth. These properties can be set programmatically as well: CardView card = ... card.

When should I use CardView?

CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI design.


2 Answers

If you use RecyclerView to add CardView, android:layout_margin should be sufficient.

But using ListView to add CardView, you might do this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" >  <android.support.v7.widget.CardView     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_marginLeft="11dp"     android:layout_marginRight="11dp"     android:layout_marginTop="11dp">        (other layout ...) </android.support.v7.widget.CardView>  </FrameLayout> 

But it is usually not the optimal one.

like image 118
zonda Avatar answered Sep 28 '22 08:09

zonda


I had the same issues before, I found answer in here... CardView layout_width="match_parent" does not match parent RecyclerView width

At the time of inflating the CardView xml inside your ViewPager, pass the ViewGroup in it. this will allow inflater to know which layout parameters to refer.

To inflate the layout file use something like below:

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_list_item, viewGroup,false); 

Hope that helps

like image 24
Nixit Patel Avatar answered Sep 28 '22 08:09

Nixit Patel