Tips for regular uses during Android development

How to become a better Android developer

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 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());

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 getPackageManager 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));

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 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";
				default:
					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();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

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

 

Output:

Screen resolution: 1440 x 2392

 

How to get how much DPI in 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));

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, by passing 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

Top