Example usage for android.widget ListView getDividerHeight

List of usage examples for android.widget ListView getDividerHeight

Introduction

In this page you can find the example usage for android.widget ListView getDividerHeight.

Prototype

public int getDividerHeight() 

Source Link

Usage

From source file:com.android.argb.edhlc.Utils.java

public static void justifyListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter adapter = listView.getAdapter();
    if (adapter == null)
        return;/*from   www . j a v a 2  s . c o m*/

    int totalHeight = 0;
    for (int i = 0; i < adapter.getCount(); i++) {
        View listItem = adapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams par = listView.getLayoutParams();
    par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
    listView.setLayoutParams(par);
    listView.requestLayout();
}

From source file:com.nttec.everychan.ui.theme.CustomThemeHelper.java

@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
    int mappingCount = 0;
    if (attrs != null) {
        for (int i = 0, size = attrs.getAttributeCount(); i < size; ++i) {
            String value = attrs.getAttributeValue(i);
            if (!value.startsWith("?"))
                continue;
            int attrId = resources.getIdentifier(value.substring(1), null, null); //Integer.parseInt(value.substring(1));
            if (attrId == 0) {
                Logger.e(TAG, "couldn't get id for attribute: " + value);
                continue;
            }/*from  w w w . j  a v  a2  s  . c  o  m*/
            int index = customAttrs.indexOfKey(attrId);
            if (index >= 0) {
                mappingKeys[mappingCount] = attrs.getAttributeNameResource(i);
                mappingValues[mappingCount] = customAttrs.valueAt(index);
                ++mappingCount;
            }
        }
    }

    if (mappingCount == 0 && textColorPrimaryOverridden == textColorPrimaryOriginal)
        return null;

    View view = instantiate(name, context, attrs);
    if (view == null)
        return null;

    boolean shouldOverrideTextColor = textColorPrimaryOverridden != textColorPrimaryOriginal
            && view instanceof TextView;
    for (int i = 0; i < mappingCount; ++i) {
        switch (mappingKeys[i]) {
        case android.R.attr.background:
            view.setBackgroundColor(mappingValues[i]);
            break;
        case android.R.attr.textColor:
            if (view instanceof TextView) {
                ((TextView) view).setTextColor(mappingValues[i]);
                shouldOverrideTextColor = false;
            } else {
                Logger.e(TAG, "couldn't apply attribute 'textColor' on class " + name
                        + " (not instance of TextView)");
            }
            break;
        case android.R.attr.divider:
            if (view instanceof ListView) {
                ListView listView = (ListView) view;
                int dividerHeight = listView.getDividerHeight();
                listView.setDivider(new ColorDrawable(mappingValues[i]));
                listView.setDividerHeight(dividerHeight);
            } else {
                Logger.e(TAG,
                        "couldn't apply attribute 'divider' on class " + name + " (not instance of ListView)");
            }
            break;
        default:
            String attrResName = null;
            try {
                attrResName = resources.getResourceName(mappingKeys[i]);
            } catch (Exception e) {
                attrResName = Integer.toString(mappingKeys[i]);
            }
            Logger.e(TAG, "couldn't apply attribure '" + attrResName + "' on class " + name);
        }
    }

    if (shouldOverrideTextColor) {
        TextView tv = (TextView) view;
        if (tv.getCurrentTextColor() == textColorPrimaryOriginal) {
            tv.setTextColor(textColorPrimaryOverridden);
        }
    }

    return view;
}

From source file:com.cerema.cloud2.ui.fragment.ShareFileFragment.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;//  ww  w . j  av a2s  . c o  m
    }
    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:com.kiwiteam.nomiddleman.TourPageActivity.java

/**
 * Makes listview items fill the list view on page to allow all list items to appear on page
 * @param listView//from  w  ww .  j  a va 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()));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

From source file:com.mobicage.rogerthat.util.ui.UIUtils.java

public static boolean setListViewHeightBasedOnItems(ListView listView, int maxHeight) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return false;
    }/*from w ww.j  a  v  a 2s  .com*/
    int numberOfItems = listAdapter.getCount();

    // Get total height of all items.
    int totalItemsHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
        View item = listAdapter.getView(itemPos, null, listView);
        item.measure(desiredWidth, 0);
        totalItemsHeight += item.getMeasuredHeight();
    }

    // Get total height of all item dividers.
    int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1);

    // Set list height.
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalItemsHeight + totalDividersHeight;
    if (maxHeight > 0 && params.height > maxHeight) {
        params.height = maxHeight;
    }
    listView.setLayoutParams(params);
    listView.requestLayout();

    return true;
}

From source file:com.lolay.android.ui.LolayBaseFragmentActivity.java

/**
 * Thanks to http://nex-otaku-en.blogspot.com/2010/12/android-put-listview-in-scrollview.html
 * WARNING: Only works with Linear Layouts or you'll get a NullPointerException in the measure code
 *///from   ww w.ja v  a2s  .  c  o m
public void listViewSetHeightFromChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

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

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

From source file:com.bamobile.fdtks.util.Tools.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;//from  ww  w  . j  av  a  2  s . 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);
    listView.requestLayout();
}

From source file:nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapterv2.java

public void justifyListViewHeightBasedOnChildren(ListView listView) {
    ArrayAdapter adapter = (ArrayAdapter) listView.getAdapter();

    if (adapter == null) {
        return;/*from  ww  w .j  ava2s .c  o m*/
    }
    ViewGroup vg = listView;
    int totalHeight = 0;
    for (int i = 0; i < adapter.getCount(); i++) {
        View listItem = adapter.getView(i, null, vg);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams par = listView.getLayoutParams();
    par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
    listView.setLayoutParams(par);
    listView.requestLayout();
}

From source file:com.roiland.crm.sm.ui.view.SmOppoInfoFragment.java

/**
 * /*from ww w .j  a  va 2s.  co m*/
 * <pre>
 * ?
 * </pre>
 *
 * @param listView 
 * @param listAdapter ?
 */
public void setListViewHeightBasedOnChildren(ListView listView, BaseAdapter listAdapter) {

    if (listAdapter == null) {
        return;
    }

    int totalHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
        View listItem = listAdapter.getView(i, null, listView);
        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:com.bamalearn.bamalearnburmese.DrawerMainActivity.java

public void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;/*from  w w w .j a  v a2s  .  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 AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,
                    AbsListView.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);
}