Gilberto Ibarra
Gilberto Ibarra

Reputation: 2927

How to get width and height of the item in a GridView

Here is my code.

@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
    View v = view;
    final ViewHolder viewHolder;
     final String typeName;

if (v == null) {
    v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
    viewHolder = new ViewHolder();

    viewHolder.mView =  v.findViewById(R.id.parentLayout);
    viewHolder.picture = (ImageView) v.findViewById(R.id.picture);
    viewHolder.name = (TextView) v.findViewById(R.id.text);

    v.setTag(viewHolder);

} else {
    viewHolder = (ViewHolder) v.getTag();
}

item = (ResourcesDrawables) getItem(position);

    int  width = viewHolder.mView.getWidth();
    int  height = viewHolder.mView.getHeight();

i try to get width /height but it returns 0 , how can i get the width and height of the item?.

Upvotes: 0

Views: 2214

Answers (1)

Android2390
Android2390

Reputation: 1150

You will not get the height and width of the cell of a gridview correctly if you put it inside the getView() method . You are trying to measure a view before its even drawn. Put it inside the onMeasure() or onSizeChanged() or try implementing the onGlobalLayoutListener() . Your best bet is one of these 3 . This answer here at : Making GridView items square should help you

Upvotes: 3

Related Questions