David Janes' Code Weblog

November 15, 2011

Python, Sqlite3, FTS3 & MacOSX

db,python,tips · admin · 8:49 am ·

If you can’t load FTS3 on a Mac, this post on StackOverflow tells you how to solve the problem.

The final bit of magic is in:

from pysqlite2 import dbapi2 as sqlite3

November 11, 2011

Beautiful Code

android,code fragments,java · admin · 8:40 am ·

This is what I’m aiming for — powerful declarations, minimal logic. UIButtonGroup, UIWebView and UIBundleHelper carry a lot of the weight here and I hope to explain (and share) all of these soon.

public class TTActivityInfo
  extends TTAbstractActivity
{
  public Button uiLeftButton;
  public Button uiRightButton;
  public UIButtonGroup buttonGroup;

  public WebView uiWebView;

  static String lActivityTitle = "Welcome!";
  static String lLeftButtonTitle = "Welcome";
  static String lRightButtonTitle = "About the app";

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    this.setContentView(R.layout.tt_activity_info);

    UIHelper.bindViews(this);
    UIHelper.hookupButtons(this);

    _configureWebView();
    _configureNavigationBar();
    _configureButtons();

    UIBundleHelper.restoreActivityStateFromBundle(this, savedInstanceState);

    if (savedInstanceState == null) {
      _forceWebViewToCurrentState();
    }
  }

  @Override
  protected void onSaveInstanceState(Bundle _bundle)
  {
    UIBundleHelper.saveActivityStateToBundle(this, _bundle);
  }

  protected void _configureNavigationBar()
  {
    TTUI.configureNavigationBarTitle(this, lActivityTitle);
  }

  protected void _configureButtons()
  {
    TTUI.configureBlueButton(this, uiLeftButton, lLeftButtonTitle);
    TTUI.configureBlueButton(this, uiRightButton, lRightButtonTitle);

    buttonGroup = new UIButtonGroup(uiLeftButton, uiRightButton);
    buttonGroup.select(uiLeftButton);
  }

  protected void _configureWebView()
  {
    if (uiWebView == null) {
      return;
    }

    uiWebView.getSettings().setJavaScriptEnabled(true);
    uiWebView.setWebViewClient(new UIWebViewClient(this, "uiWebView"));
  }

  protected void _forceWebViewToCurrentState()
  {
    if (uiWebView == null) {
      return;
    }

    if (buttonGroup.getSelectedButton() == uiRightButton) {
      uiWebView.loadUrl("http://www.google.com");
    } else {
      uiWebView.loadUrl("http://www.davidjanes.com");
    }
  }

  public void _handleButtonSelected(Button _button)
  {
    buttonGroup.select(_button);

    _forceWebViewToCurrentState();
  }

  public void uiLeftButton_onClick(Button _button)
  {
    _handleButtonSelected(_button);
  }

  public void uiRightButton_onClick(Button _button)
  {
    _handleButtonSelected(_button);
  }
}

November 10, 2011

Activity (I): The First Rule of Android Activities

android,java · admin · 5:43 pm ·

Android Applications are structured around Activities. Understanding how Activities work and structuring your code within that framework is key to creating successful Android applications. Do not try to fight this: I know from hard personal experience that this does not work.

Here is the first thing you need to know about Android Activities, and understanding this will shape everything you do while coding them in the future:

Activities can be fucking destroyed and recreated at any time – even while the user is interacting with them.

Do you understand this and the implications of this? Probably not, but I’ll outline the implications of this over a few blog posts and how to work with it.

When your activity is destroyed, you lose everything – all your variables, everything you’ve setup, popup alerts, the Activity object itself. All gone.

Here are some times that an Activity will be destroyed and recreated:

  • You rotate your Android
  • ¬†You open or close a Keyboard
  • ¬†Another Activity is showing and Android needs the memory

In Android parlance, these are called “Configuration Changes“. There are ways to block this from happening, but I suggest it’s better to roll with the punches and “do the right thing”.

Fortunately Android destroys and recreates Activities in a well-defined structured manner and provides the proper places (albeit Java-clumsy) to save and restore the state so that the user won’t see that the whole interface they were looking at was destroyed and remade. More about this soon.

Naming Standard: _configure and _update

android,ideas · admin · 12:13 pm ·

In preparation for a series of posts I’m about to do on Android Activities, I want to do a brief post on a recent standardization I’ve decided on. When doing user interfaces, there’s two things that I’m always doing: some initial configuration of elements (such as setting the color or font of a label) and then maybe multiple times changing the state of an element (such as setting the actual label text).

So from now on, I’m naming these:

  • _configureXYZ – a function / method for the one-time setup of an XYZ.
  • _updateXYZ – a function / method for changing the state of an XYZ, with the expectation this is going to be called multiple times.

Note that I don’t write one-function per UI element. It’s more like _updateButtons, _updateMaps, etc.

Note the underscore. Whenever I write a function that’s intended only for the use of the current object, I prefix with “_“.

Powered by WordPress