Android


About device name, current version code, and much more

device name current version code

Hey. There are few tips for regular uses during Android app development. Mostly app required device name, current version code, screen layout size, and much more things. These tricks and tips are mostly used by developers.

So, Without wasting any time let’s get knowledge about these amazing things for every developer.

How to get Device Name?

In my word device name of the, 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) {
				e.printStackTrace();
			}
		}
		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.

getConfiguration().screenLayout

Let’s take an 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";
				default:
					return "undefined";
			}
		}
		return size;
	}

Log.i(TAG,"Screen Layout: "+AppController.getSizeName(context));

The output of this code:

I/MainActivity: Screen Layout: normal

In my case, my screen size is normal.

How to get the screen size?

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

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

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

The output of this code:

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 requires 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);
	}

Conclusion

In this tutorial, I have to discuss some basics but important point line device name, current version code, screen layout size, and many more things. I hope you like it.

Published by Tirthraj Pandey

Professional Android app Developer, Full Stack Web Developer, Blogger, Entrepreneur And Founder of Coding Issue. I am always trying to learn new things or new possible ways to do things.

Did you find this page helpful?

X

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

*