Example usage for android.view View setLayoutParams

List of usage examples for android.view View setLayoutParams

Introduction

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

Prototype

public void setLayoutParams(ViewGroup.LayoutParams params) 

Source Link

Document

Set the layout parameters associated with this view.

Usage

From source file:Main.java

public static void setScale(Object target, int width, int height) {
    if (target instanceof View) {
        View targetView = (View) target;
        ViewGroup.LayoutParams layoutParams = targetView.getLayoutParams();
        layoutParams.width = width;//from   w w w  . java2  s. c  o m
        layoutParams.height = height;
        targetView.setLayoutParams(layoutParams);
    }

}

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  ***
 *///from www .ja v a  2 s  .  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 setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;/*from w w w .  ja va 2  s .  com*/

    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();

    listView.setOnTouchListener(new View.OnTouchListener() {
        // Setting on Touch Listener for handling the touch inside ScrollView
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                return true; // Indicates that this has been handled by you and will not be forwarded further.
            }
            return false;
        }
    });
}

From source file:Main.java

public static void setViewsTopMargin(View view, int topMargin) {
    if (null == view) {
        return;/*  w  w w.j a  va  2s. c  o  m*/
    }

    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
    params.topMargin = topMargin;
    view.setLayoutParams(params);
}

From source file:Main.java

/**
 *
 * @param v/*from w w  w . j  a  v a2  s.  c o  m*/
 * @param rect
 */
public static void positionAndResizeView(View v, Rect rect) {
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(v.getLayoutParams().width,
            v.getLayoutParams().height);
    params.setMargins(rect.left, rect.top, 0, 0);
    v.setLayoutParams(params);
}

From source file:Main.java

private static View addViewToAnimLayout(final View view, int[] location) {
    int x = location[0];
    int y = location[1];
    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) view.getLayoutParams();
    lp.leftMargin = x;/*  w w w . j  a  v  a  2  s  .  c om*/
    lp.topMargin = y;
    view.setLayoutParams(lp);
    return view;
}

From source file:Main.java

public static void setHeight(View view, int height) {
    RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); // width,height
    param.height = height;/*  w  ww  .  j  ava  2  s.c o m*/
    view.setLayoutParams(param);
}

From source file:Main.java

public static void setSize(View view, int rule, int width, int height) {
    LayoutParams param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // width,height
    param.addRule(rule);/*from  w  w  w  .j ava  2 s  . co  m*/
    param.width = width;
    param.height = height;
    view.setLayoutParams(param);
}

From source file:Main.java

public static Bitmap createDrawableFromView(Context context, View view) {

    Bitmap bit = null;/* ww  w . j av a2 s . c o  m*/
    try {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));
        view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.buildDrawingCache();
        bit = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bit);
        view.draw(canvas);
    } catch (NullPointerException e) {
        Log.e("NullPointerException", e.toString());
        e.printStackTrace();
    }
    return bit;
}

From source file:Main.java

public static void scale(View v, int pixel) {
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
    params.leftMargin -= pixel;/*from   ww  w  . j  av  a2  s  .  c  o m*/
    params.rightMargin -= pixel;
    params.topMargin -= pixel;
    params.bottomMargin -= pixel;
    v.setLayoutParams(params);
}