I am working on a simple project and in which I am using RecyclerView to show a grid of some ImageView and TextView using GridLayoutManager in my fragment. I followed some tutorials on Google and made it. But I don't know why there is automatically so much padding between vertical views as if only 2 adjacent views are only showing up. [Here is a GIF of it](https://i.stack.imgur.com/o42GG.gif)

This is my fragment\_home.xml file in which the recylerview is to be shown:

```
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/gridrecyle
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="220dp"
    android:gravity="center"
    android:numColumns="2"
    android:columnWidth="180dp"/> 

```

This is my single\_item.xml file for each view:

```
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
    android:id="@+id/imagessssss"
    android:layout_width="180dp"
    android:layout_margin="10dp"
    android:layout_marginBottom="30dp"
    android:background="@drawable/grid_image"
    android:layout_height="180dp" />
<TextView
    android:id="@+id/texttsss"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lines="1"
    android:maxLines="1"
    android:textSize="25sp" />
 </LinearLayout>

```

This is my fragment\_home java class :

```
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     final  View vii = inflater.inflate(R.layout.fragment_home, null);
     context = getActivity();
     RecyclerView recyclerView = vii.findViewById(R.id.gridrecyle);
     CustomRecycleAdaptor adaptor = new CustomRecycleAdaptor(context);
     recyclerView.setAdapter(adaptor);
     recyclerView.setLayoutManager(new GridLayoutManager(context , 2 , GridLayoutManager.VERTICAL , false ));

```

This is my CustomRecycleAdaptor java class for the adaptor :

```
public class CustomRecycleAdaptor extends RecyclerView.Adapter <CustomRecycleAdaptor.Viewholder>{

private final Context mcontext;

public CustomRecycleAdaptor(Context mcontext) {
    this.mcontext = mcontext;
}

//This is a method for giving padding to imageviews with converting dp to pixels
public static float convertDpToPixel(float dp, Context context){
    return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}

public Integer[] mimg = {
    R.drawable.cake, R.drawable.donut, R.drawable.ice_creame, R.drawable.smoothie
};

public String[] mstrng = {
        "Cakes" , "Donuts" , "Ice Creams" , "Smoothies"
};

@NonNull
@Override
public CustomRecycleAdaptor.Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
    View vii = layoutInflater.inflate(R.layout.single_item , parent , false);
    Viewholder viewHolder = new Viewholder(vii);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull CustomRecycleAdaptor.Viewholder holder, int position) {

    TextView textView = holder.texts;
    ImageView imageView = holder.images;

    textView.setText(mstrng[position]);
    imageView.setImageResource(mimg[position]);

    int imgpadding = Math.round(convertDpToPixel(10 , mcontext));
    int txtpadding = Math.round(convertDpToPixel(40 , mcontext));

    imageView.setPadding(imgpadding , imgpadding , imgpadding , imgpadding);
    textView.setPadding(txtpadding , 0 , 0 , 0);

}

@Override
public int getItemCount() {
    return mstrng.length;
}

public class Viewholder extends RecyclerView.ViewHolder {
    public TextView texts;
    public ImageView images;

    public Viewholder(@NonNull View itemView) {
        super(itemView);

        texts = itemView.findViewById(R.id.texttsss);
        images = itemView.findViewById(R.id.imagessssss);
    }
}}

```

Kindly Assist me , Thanks in advance!

Update: Uploaded full single\_actvity.xml