layout_margin in CardView is not working properly

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


ViewPager.LayoutParams does not extend ViewGroup.MarginLayoutParams

It's that simple.

http://developer.android.com/reference/android/support/v4/view/ViewPager.LayoutParams.html


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.