Java tutorial
//package com.java2s; import android.app.Activity; import android.content.Context; import android.preference.PreferenceManager; import android.util.DisplayMetrics; public class Main { private static final String EXTRA_DEF_KEYBOARDHEIGHT = "DEF_KEYBOARDHEIGHT"; private static final int DEF_KEYBOARD_HEAGH_WITH_DP = 300; private static int sDefKeyboardHeight = -1; public static int getDefKeyboardHeight(Context context) { if (sDefKeyboardHeight < 0) { sDefKeyboardHeight = dip2px(context, DEF_KEYBOARD_HEAGH_WITH_DP); } int height = PreferenceManager.getDefaultSharedPreferences(context).getInt(EXTRA_DEF_KEYBOARDHEIGHT, 0); return sDefKeyboardHeight = height > 0 && sDefKeyboardHeight != height ? height : sDefKeyboardHeight; } public static int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } private static DisplayMetrics getDisplayMetrics(Context context) { DisplayMetrics dm = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm); return dm; } }