Category Archives: CODING

(Android) Delay method call

This is how you could delay a method call for a given amount of time. I’ve used this to have an alert dialog pop up a few second after starting an activity.

This will fire myMethod() after 3000 miliseconds:

				Handler handler = new Handler();
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						myMethod();			
					}}, 3000);

(Android) Check if Google Play Store is installed on device

Here’s a quick way to check if Google Play Store is installed. I use this to keep activities that use in-app billing from crashing on devices that doesn’t have Google Play Store. Works fine in the emulator as well. If the method returns false, I’ll just give the user an alert telling them they need Google Play for in-app billing. If it returns true the in-app billing system gets set up.

Here, implemented in a static method. If you call this from an activity you go:

boolean myBoolean = googlePlayStoreInstalled(this);

and if you’re in a fragment or so just go

boolean myBoolean = googlePlayStoreInstalled(getActivity());

I keep this in a utility class.

 
	public static boolean googlePlayStoreInstalled(Activity activity) {

		PackageManager packageManager = activity.getApplication().getPackageManager();
		List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);

		for (PackageInfo packageInfo : packages)
			if (packageInfo.packageName.equals(GooglePlayStorePackageNameOld) || packageInfo.packageName.equals(GooglePlayStorePackageNameNew))
				return true;

		return false;
	}

(Android) Simple confirm dialog

	
private void confirmDialog() {		
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
				
		builder
		.setMessage("Are you sure?")
		.setPositiveButton("Yes",  new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int id) {
				// Yes-code
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog,int id) {
				dialog.cancel();
			}
		})
		.show();
	}

YouTube Tutorials

My all time favorite tutorial maker Derek Banas has an extensive catalogue of programming tutorials on his Youtube channel. I’ve used those a lot, especially in my studies. His series on design patterns are golden!

I really like the tempo in them. Very hands-on and very organised.

He has a bunch of videos that covers entire techniques in a single video. Perfect if you’re a somewhat experienced developer that need to get up to speed in something.

Here’s and example:

(Android) Find physical screen size programatically

This method will find out if screen is a tablet size.

You could of course make this even more corner case-compliant by getting the units dpi (dot per inch) and use the Pythagorean theorem to find the actual inch size of the diagonal. But this will probably suffice for most case.

	private boolean isLargeScreen() {
		
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);	
		
		// Get the density of the screen (dp)
		float density = metrics.density;		
		
		// Find out if the smallest of x/y is largeer than 600 (7" tablet. For 10" use 720)
		return Math.min(metrics.widthPixels / density,  metrics.heightPixels / density) > 600;
	}

(Android) Custom view orientation change – restore state.

Here’s one pretty simple way to get your custom view to restore its current state after an orientation change:
You can of course put any values in there, for example if you need to restore fields.

(quickly plotted code. Haven’t run it, but it should work)

@Override
protected Parcelable onSaveInstanceState() {
	Bundle bundle = new Bundle();
	bundle.putParcelable("instanceState", super.onSaveInstanceState());
	bundle.putString("myString", myEditText.getText().toString());
	return bundle;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
	if (state instanceof Bundle) {
		Bundle bundle = (Bundle) state;
		myEditText.setText(bundle.getString("myString"));
		state = bundle.getParcelable("instanceState");
	}
	super.onRestoreInstanceState(state);
}