Screen Shot 2014-04-10 at 19.52.46

Linode – The best VPS host

A couple of years ago, I was looking for a good place to host a VPS, with this blog, and other stuff. After testing out a couple of known and little known services, I stumbled across Linode.

The VPS you get for your money, is absoslutely worth it!
They have a pretty powerful starter plan, with 1 GB of ram and 48 GB of storage.
Theres great guides and support for setting things up.

Great performance, uptime and customer support.

Now they have also come up with hourly billed plans like Amazon, letting you test anything in a VM for almost nothing.

If you want to try them out, you can use this referral link, that would give me a free month of hosting :)

Update 21 apr: They have been doing lots of upgrades before, and now they have done it again! With another doubling of ram, SSDs and more!

featured-extract

How to extract strings into strings.xml resource files in Android Studio

I have previously posted a guide on how to extract string resources in Eclipse, so I thought I would do it for Android Studio too, as it is not directly obvious how to acheive this.

All your applications strings should be placed in the various /values/strings.xml files in your app. This is the best practice, gathers all texts, and easily enables you to translate your app into other languages.

While developing apps, it might be easier to just hardcode the string values, but when you are done testing, you can hit  ALT + Enter to bring up the string resource extraction dialog.

Choose Extract string resource, and type a name for the resource in the dialog.

Your string should now have been moved to the strings.xml file you selected (default is in: values/strings.xml).

The process goes like this:

Continue reading

How to detect changes to your Android Preferences

This will demonstrate how to listen for changes to the user preferences in your app, so  that you can perform tasks depending on what the user selected/changed on the preferences screen.

I have an app, with a listfragment displaying rows of text.  There is also a preferences screen (SettingsActivity.java) where users can set the sorting order of the list.

When users change the sort order, the list should be refreshed, to disaply the items in the new order.

This code is set up inside my listfragments onCreate() method.
(except the changedPrefsListener which is a field variable)


private SharedPreferences.OnSharedPreferenceChangeListener prefListener;

//listener on changed sort order preference:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {

        log("Settings key changed: " + key);
        if(key.equals(SettingsActivity.PREF_SORT_ORDER))
            getLoaderManager().restartLoader(LOADER_ID, null, tCallbacks);

    }
};
prefs.registerOnSharedPreferenceChangeListener(prefListener);

The preferences.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <ListPreference
        android:key="pref_sort_order"
        android:title="Sort notes by"
        android:summary=""
        android:dialogTitle="Sort notes by"
        android:entries="@array/orderby_array_entries"
        android:entryValues="@array/orderby_array_values"
        android:defaultValue="edited" />

</PreferenceScreen>

The preference listener allows me to detect that the PREF_SORT_ORDER preference has been changed, so I can issue a restartLoader() call to refresh the list via the loadermanager.

In this case, the sort order of the list is changed, so the loader will have to do a new query against the Content Provider, with the right sort settings.
If you are sorting text, you have probably come across a problem where the sorting is case sensitive, to fix that, have a look at my previous post about case insensitive sorting.

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.

//do import statements
import java.util.Timer;
import java.util.TimerTask;

//then later in your code, use the Timer Schedule function:
new Timer().schedule(
    new TimerTask() {
        @Override
        public void run() {
            removeDialog();
        }
    },
    2000
);

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 query where you specify the sorting.

Note that the ASC is optional to specify. You could also use DESC to reverse the sorting order.

You can find more information on CursorLoader at the Android Developer site, here and here.

Android Emulator: How to access the sqlite database of an app

This is an example of how to access the sqlite database of an Android app running on an emulator.

Make sure you have the android adb tool on your PATH.

Run this command in the terminal: adb version
If you have adb on your path, it should show something similar to this:
Android Debug Bridge version 1.0.31

If not, follow this guide: Adding the adb tool to the terminal for easy access.

Accessing the SQLITE database

  1. Open a new Terminal window.
  2. Enter adb shell
  3. Navigate to the folder where your apps databases are:
    cd /data/data/<your app package name>/databases/
  4. Type ls to see which database files are present.
  5. Open a database file with the sqlite3 tool: sqlite3 database-name.db
  6. You can now browse around the database, using the different supported commands.
    To see all commands, type .help
    To list all tables in this database, type .tables
    To list all rows in a table, type  SELECT * FROM tablename;
  7. When you are done browsing the database, type .exit to return from the sqlite3 tool.

There are a lot of options with the sqlite tool, so I suggest you visit the sqlite documentation for more sqlite commands and examples.