0

I am trying to match the CardView width to the screen width but CardView width is not matching to screen width. I have checked my code again and again and I've even made all the attributes width equal to match parent.

Recyclerview xml --

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DisplayTodayExercises">

<android.support.v7.widget.RecyclerView
    android:id="@+id/exercises_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

</LinearLayout>

Card view xml --

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_general_dimen"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/exer_name"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:layout_height="40dp"
        android:textStyle="bold"
        android:gravity="center_vertical"
        tools:text="Exercise Name"/>


    <TextView
        android:id="@+id/se_eps"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:textStyle="bold"
        android:layout_marginLeft="@dimen/sets_reps_dimen"
        android:gravity="center_vertical"
        android:layout_height="40dp"
        tools:text="5 x 6"/>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_marginLeft="@dimen/sets_reps_dimen"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"/>

</LinearLayout>
  </android.support.v7.widget.CardView>

Inflating code

LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.exercises_card_view_layout, null);
    return new ExerciseContentViewHolder(view);

Here is the screenshot

enter image description here

Sid
  • 29
  • 7

2 Answers2

3

The problem lies with your inflater. Check out the documentation for LayoutInflater, specifically for the inflate method.

Try this:

LayoutInflater.from(parent.getContext())
        .inflate(R.layout.exercises_card_view_layout, parent, false);

Instead of supplying null, supply the parent view. This way the inflater knows what LayoutParameters to use. Supplying false parameter tells the LayoutInflater not to attach it to the parent just yet. The RecyclerView will handle this for you.

deluxe1
  • 737
  • 4
  • 15
  • Always attach source of your code if it's not yours: https://stackoverflow.com/questions/24503760/cardview-layout-width-match-parent-does-not-match-parent-recyclerview-width?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – muminers Mar 23 '18 at 13:43
  • You are welcome. @muminers - I have encountered such a problem and explained how I solved it. I copied the part of the code from my own application, just changed the name of the view. After all, it's a generic line of code that is always used in cases like this and it's highly likely and this was posted as answer several times. I cannot link to all of them. – deluxe1 Mar 23 '18 at 13:49
0

This is kind of known issue. You can wrap your CardView with eg RelativeLayout or try the solution from here: CardView layout_width="match_parent" does not match parent RecyclerView width

muminers
  • 1,190
  • 10
  • 19