Ground-Up Android, Part 4: Writing the Code

by Yoni Samlan on Oct 7, 2009

This is part 4 of a 5-part series on introductory Android development. If you’re just arriving, you should head on over to Part 1.

In Part 3, we defined our layout and prettied up our app’s main screen. Now let’s get to the business of real coding – the stuff that makes the app tick behind the scenes.

The Activity

So far, there’s only a few pre-created lines in our Activity, TwitterPoster.java:

1
2
3
4
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

Let’s break that down a bit. What this is saying is:
When our Activity is created,
Do generic Activity creation stuff that all Activities do
Then load up the view from res/layout/main.xml for display.

(The “savedInstanceState” isn’t applicable right now; if you’re curious, you can use it to save things like the content of text fields when your view is destroyed – for example, if the user takes a phone call or rotates the screen – and restore them later when the user returns to your activity).

In particular, when
setContentView(R.layout.main);
runs, it gives us access in code to the views inside that layout. In order to access those views, we need to reference them with the findViewById method:

1
2
final Button postTweet = (Button) findViewById(R.id.TwitterPosterPostButton);
final EditText tweetEditText = (EditText) findViewById(R.id.TwitterPosterMessageText);

As you type, Eclipse may give you errors for classses like Button or EditText that haven’t been imported yet. Just use the “quick fix” (Command-1 or Ctrl-1, or hover over the underlined error) to automatically add the missing import statements.

Now that we have our EditText and Button, let’s set an action when the user clicks on the button. For now, let’s just have it Toast us the status message (a Toast is an Android popup that displays a message for a few seconds, then disappears – similar to Growl on a Mac).

1
2
3
4
5
6
7
8
postTweet.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        String twitterStatus = tweetEditText.getText().toString();
        Toast.makeText(TwitterPoster.this,
            "You tweeted " + twitterStatus, 
            Toast.LENGTH_LONG).show();
    }
});

Go ahead and try that out.

OnClickListener & Toast in action

The OnClickListener and Toast in action

The message, of course, is a lie right now; we haven’t written the code that actually posts to Twitter, but this is a good start. In Part 5, we’ll write the Twitter code and learn how to debug Android code.

On to Part 5!

Code Recap

In case you’ve drifted a bit, here’s what you should have for TwitterPoster.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.activefrequency.android.demos.twitternator;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class TwitterPoster extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button postTweet = (Button) findViewById(R.id.TwitterPosterPostButton);
        final EditText tweetEditText = (EditText) findViewById(R.id.TwitterPosterMessageText);
        postTweet.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
                            String twitterStatus = tweetEditText.getText().toString();
			    Toast.makeText(TwitterPoster.this, 
                                "You tweeted "+twitterStatus, 
                                Toast.LENGTH_LONG).show();
			}
		});
    }
}

Tags: ,

About Yoni Samlan

Yoni Samlan is a partner and developer at Active Frequency. He's been programming since he was yea tall, and has been a professional Python and Django developer since 2005.

  • Appu
    Nice and easiest code..
    Thank you...
blog comments powered by Disqus