set View Margins - Android User Interface

Android examples for User Interface:View Margin

Description

set View Margins

Demo Code


//package com.java2s;
import android.view.View;
import android.view.ViewGroup;

public class Main {
    public static void setMargins(View view, int left, int top, int right,
            int bottom) {
        ViewGroup.LayoutParams params = view.getLayoutParams();

        if (params instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) view
                    .getLayoutParams();/* w ww . jav  a2  s  . co  m*/
            marginLayoutParams.setMargins(left, top, right, bottom);
            view.requestLayout();
        }
    }
}

Related Tutorials