Example usage for android.view View measure

List of usage examples for android.view View measure

Introduction

In this page you can find the example usage for android.view View measure.

Prototype

public final void measure(int widthMeasureSpec, int heightMeasureSpec) 

Source Link

Document

This is called to find out how big a view should be.

Usage

From source file:com.jefftharris.passwdsafe.lib.view.GuiUtils.java

/**
 * Set the height of a ListView based on all of its children
 *///from w  w w .  j av  a  2s  .c o  m
public static void setListViewHeightBasedOnChildren(final ListView listView) {
    final ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    // Defer measurement so listview is rendered to get its width
    listView.post(new Runnable() {
        @Override
        public void run() {
            int width = View.MeasureSpec.makeMeasureSpec(listView.getMeasuredWidth(), View.MeasureSpec.AT_MOST);
            int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int totalHeight = 0;
            int numItems = listAdapter.getCount();
            for (int i = 0; i < numItems; i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(width, height);
                totalHeight += listItem.getMeasuredHeight();
            }

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight;
            if (numItems > 0) {
                params.height += listView.getDividerHeight() * (numItems - 1);
            }
            listView.setLayoutParams(params);
        }
    });
}

From source file:Main.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;/*w  w  w  .  j  av a2s .  c o  m*/

    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

From source file:com.google.cast.samples.games.gamedebugger.DebuggerFragment.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;//  w  w w .  j av a  2 s.  co m
    }

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

From source file:Main.java

public static int getViewWidth(View view) {
    if (view != null) {
        int width = view.getMeasuredWidth();
        if (width == 0) {
            LayoutParams params = view.getLayoutParams();
            if (params != null && params.width > 0) {
                return params.width;
            }//from www.j av  a  2  s. c om
            view.measure(0, 0);
            width = view.getMeasuredWidth();
        }
        return width;
    }
    return 0;
}

From source file:Main.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;/*from  w w w  .  j av a 2  s  .  c o  m*/
    }

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        if (listItem instanceof ViewGroup) {
            listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

From source file:Main.java

public static int getViewHeight(View view) {
    if (view != null) {
        int height = view.getMeasuredHeight();
        if (height == 0) {
            LayoutParams params = view.getLayoutParams();
            if (params != null && params.height > 0) {
                return params.height;
            }//from  w w  w.  j a  v  a 2s  .com
            view.measure(0, 0);
            height = view.getMeasuredHeight();
        }
        return height;
    }
    return 0;
}

From source file:Main.java

public static void setListViewHeightBasedOnChildren1(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;/*from   w ww.  j a v a  2 s. c om*/
    }
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0) {
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

From source file:Main.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;//from   ww  w . jav a  2  s.  c om
    }

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        if (listItem instanceof ViewGroup) {
            listItem.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
        }
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

From source file:Main.java

/**
 * * Method for Setting the Height of the ListView dynamically.
 * *** Hack to fix the issue of not showing all the items of the ListView
 * *** when placed inside a ScrollView  ***
 */// w w  w  .  j a v  a  2s  .c  o  m
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

From source file:Main.java

public static void setListViewSize(ListView list) {
    ListAdapter myListAdapter = list.getAdapter();
    if (myListAdapter == null) {
        //do nothing return null
        return;//from   ww  w  .j  a va2  s  . c om
    }

    //set listAdapter in loop for getting final size
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(list.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < myListAdapter.getCount(); i++) {
        view = myListAdapter.getView(i, view, list);
        if (i == 0) {
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }

    //setting listview item in adapter
    ViewGroup.LayoutParams params = list.getLayoutParams();
    params.height = totalHeight + (list.getDividerHeight() * (myListAdapter.getCount() - 1));
    list.setLayoutParams(params);
}