Delayed task execution in Java and Android

If you need to do some delayed task in for example your Android App, you can easily achieve this with a Java TimerTask. This is a short example, where some method ( removeDialog() ) is run with a delay of 2 seconds. //import statements import java.util.Timer; import java.util.TimerTask; //in your method, use the Timer Schedule function: new Timer().schedule( new TimerTask() { @Override public void run() { removeDialog(); } }, 2000 );

» Read more

Android SQLite Sort order does not work with upper and lowercase letters

If you are using SQLite database, with a Cursor or Perhaps Content provider, you may have come across alphabetical sorting problems regarding upper and lowercase letters. Perhaps you are requesting a cursor from a Content Provider: String[] projection = { Table.COLUMN_ID, Table.COLUMN_TITLE}; String sortOrder = Table.COLUMN_TITLE + " COLLATE NOCASE ASC"; CursorLoader cursorLoader = new CursorLoader(this, YourProvider.CONTENT_URI, projection, null, null, sortOrder); The solution to the alphabetical sorting problem is the COLLATE NOCASE in the part of the

» Read more

Android Studio does not start

Have you just downloaded and installed the new Android Studio? And it will not start after the installation? Here is the solution: You are most likely missing the JDK_HOME environment variable. It should be pointing to your java JDK installation, for example  C:\Program Files\Java\jdk1.7.0_21\ If you haven’t installed Java JDK 7, you can download it from Oracle here.

» Read more

How to notify your Fragment of different user navigation events

If you are using Android Fragments in your app, it can sometimes be useful to know when a user is navigation away from your fragment, so that you can act upon the users action, either by saving away data, asking the user for a confirmation, or what else. Here I will describe three methods of registering navigation events. Telling the Fragment that the back button has been pressed, through a reference to your fragment If you would like to know

» Read more

Convert milliseconds to a readable date in Java

This is a simple code snippet that converts milliseconds to a dateformat that is more understandable by humans. import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { long ms = 1359256734; Date date = new Date(ms); SimpleDateFormat dateformat = new SimpleDateFormat("MMM dd, yyyy HH:mm"); System.out.println(dateformat.format(date)); } } The millisecond date is parsed into a Date object, and then printed at a specific format given by the SimpleDateFormat object. For

» Read more