1

I am using a recycler view to add items to my view dynamically.

Here is the frame_layout where i am defining the dimesions of the recycler view

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:background="@color/white"
    android:clickable="true"
    tools:context=".CategoriesFragment">
<RelativeLayout
        android:id="@+id/categories_fruits_clicked"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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


    </RelativeLayout>

</FrameLayout>

Here is the recycler_view_list_item

<?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="vertical">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:background="@drawable/product_background"  <!--a blue colored background, with rounded corners-->
            android:layout_margin="16dp">

            <ImageView
                android:id="@+id/product_imageView"
                android:layout_marginTop="15dp"
                android:layout_width="120dp"
                android:layout_height="120dp"/>

            <TextView
                android:id="@+id/textViewTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginTop="16dp"
                android:layout_marginStart="16dp"
                android:layout_toEndOf="@id/product_imageView"
                android:text="Banana"
                android:fontFamily="@font/baloo"
                android:textSize="20sp"
                android:textColor="@color/colorAccent" />

            <TextView
                android:id="@+id/textViewPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textViewTitle"
                android:layout_marginStart="16dp"
                android:layout_toEndOf="@id/product_imageView"
                android:text="Rs 120"
                android:textStyle="bold"
                android:textColor="@color/green"/>
            <Button
                android:id="@+id/qty_minus"
                android:background="@null"
                android:text="-"
                android:textColor="@color/red"
                android:textSize="20sp"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_below="@+id/textViewPrice"
                android:layout_toEndOf="@id/product_imageView"
                android:layout_margin="16dp"/>

            <TextView
                android:id="@+id/qty_counter"
                android:text="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewPrice"
                android:layout_toEndOf="@id/qty_minus"
                android:layout_marginTop="26dp"
                android:layout_marginEnd="10dp"/>

            <Button
                android:id="@+id/qty_plus"
                android:background="@null"
                android:text="+"
                android:textColor="@color/red"
                android:textSize="20sp"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_below="@+id/textViewPrice"
                android:layout_toEndOf="@id/qty_counter"
                android:layout_marginTop="16dp"/>

        </RelativeLayout>


</LinearLayout>

For some weird reason, the recycler view doesn't cover the entire screen width, inspite of it being defined as "match_parent". Surprisingly, the preview is appearing correctly, with the blue color background spread across the layout width, however, in my device, it is showing only as big as wrap content.

When i change the layout_width inside the Relative Layout of recycler_view_list_item, to 400dp, it covers the entire screen's width. Where am i going wrong with match_parent?

Edit 1: My ProductRecyclerViewAdapter

public class ProductRecyclerViewAdapter extends RecyclerView.Adapter<ProductRecyclerViewAdapter.ProductViewHolder> {

    private Context context;
    private List<Product> productList;

    public ProductRecyclerViewAdapter(Context context, List<Product> productList) {
        this.context = context;
        this.productList = productList;
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.product_recyclerview_list_item,null);
        ProductViewHolder holder = new ProductViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder prouctViewHolder, int i) {
        Product product = productList.get(i);
        prouctViewHolder.pdtTitle.setText(product.getTitle());
        prouctViewHolder.pdtPrice.setText("MRP Rs " + String.valueOf(product.getPrice()));
        prouctViewHolder.pdtImageView.setImageDrawable(context.getResources().getDrawable(product.getImageId()));

    }

    @Override
    public int getItemCount() {
        return productList.size();
    }

    class ProductViewHolder extends RecyclerView.ViewHolder{

        ImageView pdtImageView;
        TextView pdtTitle, pdtPrice;

        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);
            pdtImageView = itemView.findViewById(R.id.product_imageView);
            pdtTitle = itemView.findViewById(R.id.textViewTitle);
            pdtPrice = itemView.findViewById(R.id.textViewPrice);
        }
    }
}

Edit 2: Added the images

The Preview

THE PREVIEW(ABOVE)

How it is visible in my device

HOW IT IS VISIBLE IN MY DEVICE(ABOVE)

CS Learner
  • 231
  • 2
  • 11
  • Possible duplicate of [match\_parent width does not work in RecyclerView](https://stackoverflow.com/questions/30691150/match-parent-width-does-not-work-in-recyclerview) – ADM Jun 25 '19 at 10:13
  • Can you post the screenshot where the list item is not covering full screen?? Also you have mentioned layout_margin to the RelativeLayout may be it was the issue.. – Rahul Khurana Jun 25 '19 at 10:13
  • remove android:layout_margin="16dp" this line from relative layout and then try. – khushbu vadi Jun 25 '19 at 10:16
  • Share your layout inflate code – Tariqul Islam Jun 25 '19 at 10:18
  • @khushbuvadi No, i tried doing that, it didn't help – CS Learner Jun 25 '19 at 10:25
  • @TariqulIslam please see my edit – CS Learner Jun 25 '19 at 10:25
  • 1
    For inflating layout use your viewGroup instead of **null** Like: inflater .inflate(R.layout.product_recyclerview_list_item,viewGroup,false); And for inflater LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext()); – Tariqul Islam Jun 25 '19 at 10:29
  • Thank you so much, @TariqulIslam, it worked. Can you tell me why using null gave the wrong answer? – CS Learner Jun 25 '19 at 10:39
  • RecyclerView inflating in RelativeLayout or other Layout Like FrameLaout etc. These are main container. And all these layout under **ViewGroup** . So when you set null it will avoid it parent. Sow how match_parent work without parent? Thats why it was working on static size like 400 dp . – Tariqul Islam Jun 25 '19 at 10:43

2 Answers2

0

try this, Change your Relative layout to linear layout and put layout weight to 1

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:background="@color/white"
    android:clickable="true"
    tools:context=".CategoriesFragment">
<LinearLayout
        android:id="@+id/categories_fruits_clicked"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/categories_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_weight="1"/>
    </RelativeLayout>

</FrameLayout>
Rajneesh Shukla
  • 1,048
  • 13
  • 21
0
<?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="match_parent" 
android:orientation="vertical">

use match parent height of item layout and

LayoutInflater.from(getContext()).inflate(R.layout.recycler_view_list_item, parent, false)
Brijesh Lukhi
  • 15
  • 1
  • 1
  • 9