get GridView Width - Android android.widget

Android examples for android.widget:GridView

Description

get GridView Width

Demo Code

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.GridView;
import android.widget.ListAdapter;

public class Main {

  @SuppressLint("NewApi")
  public static int getGridViewWidth(GridView listView, int n) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
      return 0;/* w  w w . j  a  v a  2s.  c  o  m*/
    }
    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
      View listItem = listAdapter.getView(i, null, listView);
      listItem.measure(0, 0);
      totalHeight += listItem.getMeasuredWidth();
    }
    return totalHeight + 5;
  }

}

Related Tutorials