How to Give a Java Object More Responsibility in Your Android App

By Barry A. Burd

Sometimes, you want to give your Java object more to do in your Android app. Consider this: You have a printer and you try to install it on your computer. It’s a capable printer, but it didn’t come with your computer, so your computer needs a program to drive the printer: a printer driver. Without a driver, your new printer is nothing but a giant paperweight.

But, sometimes, finding a device driver can be a pain in the neck. Maybe you can’t find the disk that came with the printer.

Imagine you have one off-brand printer whose driver is built into its permanent memory. When you plug the printer into a USB port, the computer displays a new storage location. (The location looks, to ordinary users, like another of the computer’s disks.) The drivers for the printer are stored directly on the printer’s internal memory. It’s as though the printer knows how to drive itself!

Let’s say you want to display the properties of a particular bag, and you don’t like dealing with a bag’s nitty-gritty details. In particular, you don’t like worrying about commas, blank spaces, and field names when you display a bag:

bag.kind + ", " + bag.weight + ", " +

bag.daysAged + ", " + bag.isDomestic + "\n"

You’d rather have the BagOfCheese class figure out how to display one of its own objects.

Here’s the plan: Move the big string with the bag’s fields, the commas and the spaces from the MainActivity class to the BagOfCheese class. That is, make each BagOfCheese object be responsible for describing itself in String form. You could make give each bag’s form its own Display button.

java-programming-for-android-developers-2e-display-buttons

The interesting characteristic of a Display button is that when you press it, the text you see depends on the bag of cheese you’re examining. More precisely, the text you see depends on the values in that particular form’s fields.

The same thing happens in the second set of code below when you call bag1.toString(). Java runs the toString method shown the first set of code. The values used in that method call — kind, weight, daysAged, and isDomestic — are the values in the bag1 object’s fields. Similarly, the values used when you call bag2.toString() are the values in the bag2 object’s fields.

package com.allmycode.a09_10;

public class BagOfCheese {

public String kind;

public double weight;

public int daysAged;

public boolean isDomestic;

public BagOfCheese() {

}

public BagOfCheese(String kind, double weight,

int daysAged, boolean isDomestic) {

this.kind = kind;

this.weight = weight;

this.daysAged = daysAged;

this.isDomestic = isDomestic;

}

public String toString() {

return kind + ", " + weight + ", " + daysAged + ", " + isDomestic + "\n";

}

}

Now, let’s take a look at having a bag display itself.

package com.allmycode.a09_11;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;

import com.allmycode.a09_10.BagOfCheese;

public class MainActivity extends AppCompatActivity {

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView = (TextView) findViewById(R.id.textView);

BagOfCheese bag1 = new BagOfCheese("Cheddar", 2.43, 30, true);

BagOfCheese bag2 = new BagOfCheese("Blue", 5.987, 90, false);

textView.setText("");

textView.append(bag1.toString());

textView.append(bag2.toString());

}

}

In the first set of code, the BagOfCheese object has its own, parameterless toString method. And in the second set, the following two lines make two calls to the toString method — one call for bag1 and another call for bag2:

textView.append(bag1.toString());

textView.append(bag2.toString());

A call to toString behaves differently depending on the particular bag that’s being displayed. When you call bag1.toString(), you see the field values for bag1, and when you call bag2.toString(), you see the field values for bag2.

To call one of an object’s methods, follow a reference to the object with a dot and then the method’s name.