Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.app.Activity;
import android.content.Context;

import android.util.DisplayMetrics;

public class Main {
    /**
     * This Function convert the DP to Pixel.
     * 
     * See below for @params.
     */

    public static int convertDPtoPX(Context ctx, int dp) {

        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity) ctx).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        float logicalDensity = metrics.density;
        int px = (int) (dp * logicalDensity + 0.5);
        System.out.println("DP to Pixel-->" + dp + " = " + px);
        return px;
    }
}