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(ABOVE)
HOW IT IS VISIBLE IN MY DEVICE(ABOVE)