set View Height In DP - Android User Interface

Android examples for User Interface:View Size

Description

set View Height In DP

Demo Code


//package com.java2s;

import android.app.Activity;

import android.util.DisplayMetrics;

import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;

public class Main {
    public static void setViewHeightInDP(Activity context, View view,
            float dimensionInDP) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = convertDPtoPixel(context, dimensionInDP);
        view.setLayoutParams(layoutParams);
    }//from www.ja va2 s. c o m

    public static int convertDPtoPixel(Activity context, float dimensionInDP) {
        int dimensionInPixel = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dimensionInDP, context
                        .getResources().getDisplayMetrics());
        DisplayMetrics displaymetrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay()
                .getMetrics(displaymetrics);
        return dimensionInPixel;
    }
}

Related Tutorials