Last Update - 25 Nov 2020

Tips for regular use during Android development

Tips for regular use during Android development

Here we go with new things to learn in android development tips and tricks, these tricks and tips are mostly used by developers.

If you are a beginner android developer then you must need to learn How to start Android app development for beginners. You will get your basic requirement for android developer required skills on that page.

Use this App Development Cost calculator to quickly estimate rates and development time for a variety of factors, including developers' location, number of features, and platform choice.

A good developer always tries to choose android best practices to implement things.

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

Set log information to print your message.

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

Set log information to print your message.

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.

Vibrate Android Phone Custom

We can get this by using the 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;
	}

Set log information to print your message.

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

Set log information to print your message.

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's 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

These are some basic new things to learn in android development for every Android developer. These are the most common things, which you will definitely face in the time of development.

Keep focus on yourself to add skill and think how to be a good android developer.

More For You

Did you find this page helpful? X