set Margin For View - Android User Interface

Android examples for User Interface:View Margin

Description

set Margin For View

Demo Code


//package com.java2s;

import android.view.View;
import android.view.ViewGroup;

public class Main {
    public static void setMarginForView(View view, int leftMarginInPixels,
            int topMarginInPixels, int rightMarginInPixels,
            int bottomMarginInPixels) {

        if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view
                    .getLayoutParams();/*from   www  . ja v  a2s.  c o m*/
            layoutParams.setMargins(leftMarginInPixels, topMarginInPixels,
                    rightMarginInPixels, bottomMarginInPixels);
        }
    }
}

Related Tutorials