Android examples for User Interface:View Size
set View Width In DP
//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 setViewWidthInDP(Activity context, View view, float dimensionInDP) { ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); layoutParams.width = convertDPtoPixel(context, dimensionInDP); view.setLayoutParams(layoutParams); }//from ww w .j a va 2 s . com 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; } }