domenica 3 novembre 2013

02 - Counter

This week I want to realize a simple counter app which lets the user to increase or decrease a counter.
I want to explore layout, ui interaction and view updating.
Obtaining a reference to a view element is as simple as invoking:
increaseButton = (Button)findViewById(R.id.increaseButton);
Some observations about activity lifecycle (from vogella.com):
If the user interacts with an activity and presses the Back button or if the finish() method of an activity is called, the activity is removed from the current activity stack and recycled. In this case there is no instance state to save and the onSaveInstanceState() method is not called.
If the user interacts with an activity and presses the Home button, the activity instance state must be saved. TheonSaveInstanceState() method is called. If the user restarts the application it will resume or restart the last running activity. If it restarts the activity it provides the bundle with the save data to theonRestoreInstanceState() and onCreate() methods.
So it is important to handle the back button pressed event in order not to lose the state of the counter.
To deal with app state, you have to implement the methods

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putInt("counterValue", counterValue);
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    if (savedInstanceState.containsKey("counterValue")) {
        counterValue = savedInstanceState.getInt("counterValue");
        counterField.setText("" + counterValue);
    }
    super.onRestoreInstanceState(savedInstanceState);
}
To deal with app state in the case of the user pressing the back button, you have to deal with SharedPreferences like this:

we have to provide a method to write the instance state:
public boolean writeInstanceState(Context c) {

    SharedPreferences p = c.getSharedPreferences(
            MainActivity.PREFERENCES_FILE, MODE_WORLD_READABLE);

    SharedPreferences.Editor e = p.edit();
    e.putInt(COUNTER_VALUE_KEY, this.counterValue)
    return (e.commit());
}
and a method to read it:

public boolean readInstanceState(Context c) {

    SharedPreferences p = c.getSharedPreferences(PREFERENCES_FILE,
            MODE_WORLD_READABLE);

    this.counterValue = p.getInt(COUNTER_VALUE_KEY, 0);
    return (p.contains(COUNTER_VALUE_KEY));
}

martedì 29 ottobre 2013

01 - Hello Android

I will create a simple hello-something app.
Some simple considerations:
I created a new property that will be used to define text size.
To define a color for the background, I added an entry of type color in string.xml. Something like that:
<color name="red">#900</color>
Some notes about menu item.
In order to create menu items, you have to add it in the activity xml. This xml is organized in <menu>, <item> and <group> elements (see doc). Eclipse has a visual editor that helps you in the task.
Adding an item to the menu is as simple as adding an <item> element.
The attributes of the tag are
android:idA resource ID that's unique to the item, which allows the application can recognize the item when the user selects it.
android:iconA reference to a drawable to use as the item's icon.
android:titleA reference to a string to use as the item's title.
android:showAsActionSpecifies when and how this item should appear as an action item in the action bar.
To attach the menu to the activity, this code is required:

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.example_menu, menu);
    return true;
where example_menu is the name of the xml under res/menu which represents the menu.

In order to open a new activity clicking on a menu item, two methods like these are needed:


@Override
public boolean onOptionsItemSelected(MenuItem item) {
  // Handle item selection
  switch (item.getItemId()) {
   case R.id.about:
     openAbout();
     return true;
   default:
     return super.onOptionsItemSelected(item);
  }
}

private void openAbout() {
  Intent intent = new Intent(this, AboutActivity.class);
  startActivity(intent);
}

where AboutActivity is a new Activity with some content.

lunedì 21 ottobre 2013

Some little issues with dependencies

I created a new project in eclipse and shared with egit in order to publish it on bitbucket.
This messed up the project build path a little. It complained it cannot resolve android-support-v4.jar dependency. So I removed the broken dependency from the build path and re-imported it as simple jar (which is by default included in the lib directory).

52 apps in 52 weeks

I want to learn to develop android applications.
This is just a little blog to track my attempt to build an android app every week, hopely learning something in the process :)
If I can stick to the plan, an year from now I would have built 52 apps...