25

I'm creating RecycleView with some items. So I need to get the width and height of the one row of RecycleView

Here I'm creating RecycleView:

    RecyclerView rvSmetki = (RecyclerView) findViewById(R.id.rvArtikli);
    rvSmetki.setLayoutManager(new GridLayoutManager(this, 3););
    rvSmetki.setAdapter(new ArtikliAdapter(this));

    // Here I want to get width and height....

And this is my ArtikliAdapter:

public class ArtikliAdapter extends RecyclerView.Adapter<ArtikliAdapter.ViewHolder> {

    private static Context context;

    private LayoutInflater inflater;
    private ArrayList<Artikl> artikliList;

    public ArtikliAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        artikliList = LogInActivity.getArtiklList();
    }

    @Override
    public ArtikliAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View row = inflater.inflate(R.layout.artikli_row, parent, false);
        return new ArtikliAdapter.ViewHolder(row);
    }

    @Override
    public void onBindViewHolder(ArtikliAdapter.ViewHolder holder, int position) {
        Artikl currentArtikl = artikliList.get(position);

        holder.tvNaziv.setText(currentArtikl.getNaziv());
        holder.tvCena.setText("Цена: " + currentArtikl.getProdaznaCena());
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout rlArtikl;
        private TextView tvNaziv;
        private TextView tvCena;

        public ViewHolder(View itemView) {
            super(itemView);

            rlArtikl = (RelativeLayout) itemView.findViewById(R.id.rlArtikl);
            tvNaziv = (TextView) itemView.findViewById(R.id.tvNaziv);
            tvCena = (TextView) itemView.findViewById(R.id.tvCena);
        }
    }
}

Image

How can I get width and height of one row?

KiKo
  • 1,668
  • 7
  • 27
  • 56

1 Answers1

39
@Override
public void onBindViewHolder(final MyAdapter.MyViewHolder holder, int position) 
{
 .....
    holder.itemView.post(new Runnable()
    {
        @Override
        public void run()
        {

            int cellWidth = holder.itemView.getWidth();// this will give you cell width dynamically
            int cellHeight = holder.itemView.getHeight();// this will give you cell height dynamically

        }
    });
  .....
}

I hope this will help you!!

Amrut Bidri
  • 6,276
  • 6
  • 38
  • 80
  • 7
    It returns 0. But actually i want to get width and height in Activity, not in Adapter... – KiKo Jul 02 '15 at 10:03
  • 1
    Use interface to send this data back into activity – Amrut Bidri Jul 02 '15 at 10:54
  • Yes, It return 0. Not work @AmrutBidri. Can you explain more detail/ – K.Sopheak Nov 21 '16 at 09:52
  • 4
    This will return 0. The correct way is to manually measure the View first. Then fetch the width and height. `view.measure( View.MeasureSpec.makeMeasureSpec(recyclerViewWidth, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));` Original and full answer here: http://stackoverflow.com/a/38765295/4344964 – Suleiman19 Jan 02 '17 at 11:34
  • Works perfectly for me! – RoaflinSabos Jul 06 '17 at 14:06
  • It depends on the device, so better not to use it. I've seen it working in a pixel but not in a Sony Experia. – juanmeanwhile Aug 09 '17 at 16:17
  • Use a ViewTreeObserver inside viewholder and get the height, if you get height 0. – Reejesh PK Mar 05 '19 at 12:10