Last active
July 7, 2021 14:37
-
-
Save piyush-malaviya/eb1a4410c50484a1480deeb64d5e56be to your computer and use it in GitHub Desktop.
DisplayUtils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.Context; | |
import android.os.Build; | |
import android.util.DisplayMetrics; | |
public class DisplayUtils { | |
private static DisplayUtils instance; | |
public static DisplayUtils getInstance() { | |
if (instance == null) { | |
instance = new DisplayUtils(); | |
} | |
return instance; | |
} | |
public float dpToPx(int dp, Context context) { | |
return dp * (context.getResources().getDisplayMetrics().density); | |
} | |
public int getStatusBarHeight(Context context) { | |
int result = 0; | |
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); | |
if (resourceId > 0) { | |
result = context.getResources().getDimensionPixelSize(resourceId); | |
} | |
return result; | |
} | |
public int getNavigationBarHeight(Context context) { | |
int result = 0; | |
int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android"); | |
if (resourceId > 0) { | |
result = context.getResources().getDimensionPixelSize(resourceId); | |
} | |
return result; | |
} | |
public int getSoftButtonsBarHeight(Activity activity) { | |
// getRealMetrics is only available with API 17 and + | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
DisplayMetrics metrics = new DisplayMetrics(); | |
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); | |
int usableHeight = metrics.heightPixels; | |
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics); | |
int realHeight = metrics.heightPixels; | |
if (realHeight > usableHeight) | |
return realHeight - usableHeight; | |
else | |
return 0; | |
} | |
return 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// status bar height | |
int statusBarHeight = 0; | |
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); | |
if (resourceId > 0) { | |
statusBarHeight = getResources().getDimensionPixelSize(resourceId); | |
} | |
// action bar height | |
int actionBarHeight = 0; | |
final TypedArray styledAttributes = getActivity().getTheme().obtainStyledAttributes( | |
new int[] { android.R.attr.actionBarSize } | |
); | |
actionBarHeight = (int) styledAttributes.getDimension(0, 0); | |
styledAttributes.recycle(); | |
// navigation bar height | |
int navigationBarHeight = 0; | |
int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android"); | |
if (resourceId > 0) { | |
navigationBarHeight = resources.getDimensionPixelSize(resourceId); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment