Change margins of view. - Android User Interface

Android examples for User Interface:View Margin

Description

Change margins of view.

Demo Code


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

public class Main {
    /**//from w w w  .  j a  v  a  2s . c  om
     * Change margins of view.
     * @param view View to change margins.
     * @param margins Margins around view.
     */
    public static void setMargins(View view, int margins) {
        setMargins(view, margins, margins, margins, margins);
    }

    /**
     * Change margins of view.
     * @param view View to change margins.
     * @param left Left margin.
     * @param top Top margin.
     * @param right Right margin.
     * @param bottom Bottom margin.
     */
    public static void setMargins(View view, int left, int top, int right,
            int bottom) {
        if (view == null)
            return;
        LayoutParams params = view.getLayoutParams();
        if (params instanceof MarginLayoutParams) {
            MarginLayoutParams marginParams = (MarginLayoutParams) params;
            marginParams.leftMargin = left;
            marginParams.topMargin = top;
            marginParams.rightMargin = right;
            marginParams.bottomMargin = bottom;
            view.requestLayout();
        }
    }
}

Related Tutorials