Maven and apklib: Duplicated file error

The maven build error occurred when I had two Android projects: the one is apklib and the second one that is the actual application (the apk file) that uses apklib. The error is saying that apk project contains duplicated R.java and BuildConfig.java files under target directory. The first file comes from apk project, and the second … Continue reading Maven and apklib: Duplicated file error

Android SQLite schema migration with patches

When upgrading your Android application you often need to change its data model. When the model is stored in SQLite database, then its schema must be updated as well. I recommend the concept of patching and versioning the database This is very well described in this article: http://www.greenmoonsoftware.com/2012/02/sqlite-schema-migration-in-android/ The idea behind it Android lets you … Continue reading Android SQLite schema migration with patches

Android status bar notifications [complete tutorial with source code]

Displaying status bar notification is a common way to unobtrusively inform user that something has happened (like new GMail message notification). In your app you can display it whenever you want. I will guide you how to do it from basics. This is how notifications are presented to the user: Google's documentation regarding the notifications … Continue reading Android status bar notifications [complete tutorial with source code]

Android: check device screen density programatically

When designing app for multiple screen sizes it is useful to check screen density (ldpi, mdpi, hdpi, xhdpi) and to know exact value in dpi. This is how to do it programatically: public void determineScreenDensity() { DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int density = metrics.densityDpi; if (density==DisplayMetrics.DENSITY_HIGH) { Toast.makeText(this, "DENSITY_HIGH: " + String.valueOf(density), Toast.LENGTH_LONG).show(); … Continue reading Android: check device screen density programatically

Android: check device screen size programatically

When designing app for multiple screen sizes it is useful to check screen size (small, normal, large, xlarge). This is how to do it programatically: public void determineScreenSize() { if((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) { Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show(); }else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) { Toast.makeText(this, "Normal sized screen" , Toast.LENGTH_LONG).show(); }else if ((getResources().getConfiguration().screenLayout … Continue reading Android: check device screen size programatically

Android: handle uncaught exceptions

To avoid the 'unfortunately app has stopped' dialog box, you can handle all uncaught exceptions in your app by setting custom handler. Uncaught exception Handler Paste this code in the beginning of your app. This will setup the handler. Do it only once, for example in onCreate of your fisrt activity that is displayed to … Continue reading Android: handle uncaught exceptions

Android rotate animation

Here is how to make view infinetely rotate around its center: 1. Define animation in xml Put this file in /res/anim: <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2500" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" android:repeatMode="restart" android:toDegrees="360" > </rotate> 2. Load animation in java file I load it in onCreate and keep it as a class field. Thanks to … Continue reading Android rotate animation