The Activity

Your project's src/ directory contains the standard Java-style tree of directories based upon the Java package you used when you created the project (i.e., com resulted


in src/com/commonsware/android/). Inside the innermost directory you should find generated source file named, which is where your first activity will go. This will contain a single button that displays the time the button was last pushed (or the application was started if the button hasn't been pushed).

Open in your editor and paste in the following code:


import; import android.os.Bundle; import android.view.View; import android.widget.Button; import java.util.Date;

public class Now extends Activity implements View.OnClickListener { Button btn;

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

btn = new Button(this); btn.setOnClickListener(this); updateTime(); setContentView(btn);

public void onClick(View view) { updateTime();

private void updateTime() { btn.setText(new Date().toString());

Or, if you download the source files off the CommonsWare Web site, you can just use the Skeleton/Now project directly.

Let's examine this piece-by-piece.

0 0

Post a comment