Last Update - 12 Oct 2018

Tips for regular uses during Android development

Tips for regular uses during Android development

Tips for regular uses during Android development, these tricks and tips are mostly used by developers.

How to get Device Name?

In my word Device Name of any device is manufacturer and model:

public static String getDeviceName() {
   String manufacturer = Build.MANUFACTURER;
   String model = Build.MODEL;
   if (model.startsWith(manufacturer)) {
      return capitalize(model);
   } else {
      return capitalize(manufacturer) + " " + model;

private static String capitalize(String s) {
   if (s == null || s.length() == 0) {
      return "";
   char first = s.charAt(0);
   if (Character.isUpperCase(first)) {
      return s;
   } else {
      return Character.toUpperCase(first) + s.substring(1);
Log.i(TAG,"DeviceName: "+AppController.getDeviceName());

The output of this code:

I/MainActivity: Device Name: Google Android SDK built for x86


How to get the current Version Code?

Best practice for getting the current app version code by using get package manager in context. Simply create a method for that and pass context to get the current version code.

  public static int versionCode(Context context){
		int versionName = 0;
		if(context != null) {
			try {
				PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
				versionName = pInfo.versionCode;
			} catch (Exception e) {
		return versionName;
Log.i(TAG,"Version Code: "+AppController.versionCode(context));

The output of this code:

I/MainActivity: Version Code: 1


How to get Screen Layout size?

Screen Layout size is also an important part of the app developer. Now I am going to show how you can get the screen layout size. We can get this by using given below method.


Let's take the example:

  public static String getScreenLayout(Context context) {
		String size = "";
		if(context != null) {
			int screenLayout = context.getResources().getConfiguration().screenLayout;
			screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;
			switch (screenLayout) {
				case Configuration.SCREENLAYOUT_SIZE_SMALL:
					return "small";
				case Configuration.SCREENLAYOUT_SIZE_NORMAL:
					return "normal";
				case Configuration.SCREENLAYOUT_SIZE_LARGE:
					return "large";
				case 4: // Configuration.SCREENLAYOUT_SIZE_XLARGE is API >= 9
					return "xlarge";
					return "undefined";
		return size;
Log.i(TAG,"Screen Layout: "+AppController.getSizeName(context));

Output of this code:

I/MainActivity: Screen Layout: normal

In my case, my screen size is normal.


How to get screen size?

Let's take an example to get screen size in width*height:

DisplayMetrics displayMetrics = new DisplayMetrics();
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

Log.i(TAG, "Screen resolution: "+width +" x "+ height);



Screen resolution: 1440 x 2392


How to get how much DPI in the screen?

DPI, DPI stands for (dots per inch) is a number that measures how many pixels are contained either across or down in a single inch of screen space.

Let's take an example to get dots per inch on the screen by using context.

   public static int getDpi(Context context){
		int densityDpi = 0;
		if(context != null) {
			DisplayMetrics metrics = getMatrics(context);
			densityDpi = (int) (metrics.density * 160f);
		return densityDpi;
   public static DisplayMetrics getMatrics(Context context){
		DisplayMetrics metrics = null;
		if(context != null) {
			metrics = context.getResources().getDisplayMetrics();
		return metrics;
Log.i(TAG,"Device Dpi: "+AppController.getDpi(context));

The output of this code:

I/MainActivity: Device Dpi: 420


How to hide the virtual keyboard?

Sometimes an app require to hide the keyboard forcefully, then we can use this code to hide your app virtual keyboard. Now create this method and simply call it, bypassing your context and view where you want to hide the virtual keyboard.

   public static void hideKeyboard(Context context, View view) {
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0);


More For You

Did you find this page helpful? X