PreferenceActivity Basics

SDK Version: 
The PreferenceActivity class is a very easy way to create a standard looking settings screen for any application.

Minimal coding is required. The values are stored in SharedPreferences automatically by the PreferenceActivity.

The list of settings, default values, keys to stored under are defined in a very simple xml file.

The xml file should be located in res/xml/ directory and look like something like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <PreferenceScreen xmlns:android=""
  3.         android:title="Settings">
  5.         <EditTextPreference android:title="Name"
  6.                 android:inputType="text" android:summary="Set your login name here"
  7.                 android:key="name" android:persistent="true" android:defaultValue=""></EditTextPreference>
  9.         <EditTextPreference android:title="Password"

How to have a default database

SDK Version: 

If you want to include a database with initial data in your apk, you have to insert the database file into the projects assets folder, then programmatically check if the database, and if it does not exists copy the one from the assets.

You will need a function to check if the database exists, fox example:

  1. private static final String DATABASE_NAME = "testdatabase";
  2. private static final String DB_PATH = "/data/data/"+mContext.getPackageName()+"/databases/";
  4. private boolean isDataBaseExist() {
  5.         File dbFile = new File(DB_PATH+DATABASE_NAME);
  6.         return dbFile.exists();
  7. }

If the check shows that the the database does not exist, a function like this will copy it:

  1. private void copyDataBase() throws IOException {
  2.         // Open your local db as the input stream
  3.         InputStream myInput = mContext.getAssets().open("databases/"+DATABASE_NAME);
  4.         // Path to the just created empty db
  5.         String outFileName = DB_PATH + DATABASE_NAME;

Syndicate content