0

I am using RecyclerView with Horizontal LinearLayoutManager. The child of RecyclerView is a CardView although the width parameters of the child are set still, recycler view inflates it with squeezed width

Here is my RecyclerView

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_popular_courses"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/card_popular_quizzes" />

Here is card_popular_quizzes.xml

<androidx.cardview.widget.CardView
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:minHeight="150dp"
    app:cardElevation="@dimen/card_itemcourse_elevation"
    app:cardBackgroundColor="#309C98"
    app:cardCornerRadius="16dp"
    >

<!--    app:cardElevation="10dp"-->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center">

        <ImageView
            android:id="@+id/quiz_thumbnail_imgvw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:cropToPadding="true"
            android:maxWidth="100dp"
            android:src="@drawable/android_developer" />

        <TextView
            android:id="@+id/quiz_textvw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Hall of Fame"
            android:textColor="@color/whiteTextColor"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginBottom="10dp"/>

    </LinearLayout>


</androidx.cardview.widget.CardView>

Additionally recyclerview is inside a fragment here is code where I add LinearLayoutManager

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //removed some view initialisations

        RecyclerView rvPopularCourses=view.findViewById(R.id.rv_popular_courses);
        rvPopularCourses.setLayoutManager(
                new LinearLayoutManager(view.getContext(),LinearLayoutManager.HORIZONTAL,false));
        rvPopularCourses.setAdapter(quizAdapter);
    }

Here is a visual screenshot for reference enter image description here

Daniel.Wang
  • 2,242
  • 2
  • 9
  • 27
AmanSharma
  • 821
  • 9
  • 15

1 Answers1

0

LinearLayout width is wrap_content which is causing this issue. You have to set your LinearLayout layout_width to match_parent in card_popular_quizzes.xml

arvind
  • 104
  • 4