Android Idioms

This is a list of Android API idioms, idiosyncrasies, and things that didn't work how I expected them to.
  1. Setting colors programmaticaly from a resource
  2. Programmatic bold text
  3. Default preferences behavior
  4. Integer-valued ListPreference

Setting colors programmaticaly from a resource

Since resources and colors both use the type "int", your color might end up black/weird if you try to set it like this:
(TextView) view).setTextColor(R.color.white);
Instead, use:
(TextView) view).setTextColor(getResources().getColor(R.color.white));

Programmatic bold text

There is to TextView.setTextStyle() method.
To programmatically set bold text, call:
(TextView) view).setTypeface(Typeface.DEFAULT_BOLD);

Default preferences behavior

The "default" value in SharedPreferences.getInt(prefkey, default) is not used if a default value is specified in the XML preferences file.
Furthermore, if the default in the XML file is changed any time after the preference value is accessed at least once, it will have no effect;
it will keep the default that existed when the pref was first accessed.

Integer-valued ListPreference

Since android doesn't support integer types for this yet, instead of:
settings.getInt("my_prefkey_name", default_integer_value);
use:
Integer.parseInt( settings.getString("my_prefkey_name", default_integer_value + "") );
Subpages (1): Progress Notifications
Comments