add View Margins In Dp - Android User Interface

Android examples for User Interface:View Margin

Description

add View Margins In Dp

Demo Code


//package com.java2s;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class Main {
    /**/*from  w  ww .  java  2 s. c  o  m*/
     * @param view view to be adjusted
     * @param left left margin in DP
     * @param top top margin in DP
     * @param right right margin in DP
     * @param bottom bottom margin in DP
     * @return adjusted view
     */
    public static View addMarginsInDp(View view, int left, int top,
            int right, int bottom) {
        DisplayMetrics dm = view.getResources().getDisplayMetrics();
        ViewGroup.LayoutParams viewLp = view.getLayoutParams();
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                viewLp.width, viewLp.height);
        lp.setMargins(left, top, right, bottom);
        view.setLayoutParams(lp);
        return view;
    }

    public static View addMarginsInDp(View view, int vertical,
            int horizontal) {
        return addMarginsInDp(view, horizontal, vertical, horizontal,
                vertical);
    }

    public static View addMarginsInDp(View view, int margin) {
        return addMarginsInDp(view, margin, margin);
    }
}

Related Tutorials