Skip to content

Different Ways to Implement Buttons in Android: Listeners

September 27, 2011

So you want to use a Button in your Android App to do something. One would think that this would be an easy process and yes, it is, but there are different ways of writing and implementing the Java for this. For the beginner Android developer, it can be a rather confusing on which one to use.

First I’ll start with a “popular” one, which is very straight forward but not very flexible.
This example is going to give the user two buttons on a page and each button is going to launch a new window/screen (called an Intent) once a button is clicked.

Here is the main.java file:

package com.hockey.fenimore;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class HockeyActivity extends Activity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
        
	Button newGoalie = (Button)findViewById(R.id.newGoalie);
	newGoalie.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View arg0) {
		Intent myIntent = new Intent(arg0.getContext(), Newgoalie.class);
		startActivityForResult(myIntent, 0);
	}
     });


     Button editGoalie = (Button)findViewById(R.id.editGoalie);
     editGoalie.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View arg1) {
		Intent myIntent = new Intent(arg1.getContext(), Editgoalie.class);
		startActivityForResult(myIntent, 0);
	}
     });
  }
}

The breakdown:
Button newGoalie = (Button)findViewById(R.id.newGoalie);
If you tried to read the above line in plain words, you’re telling the application that you are creating a “Button” call newGoalie and this (Button) is being associated or will be triggered by an button object on the page which you will find (findViewById) and has an ID called newGoalie. This newGoalie needs to been defined and already created in your main.xml file.

newGoalie.setOnClickListener(new OnClickListener()
The button called newGoalie is going to be listening for a click and when it is clicked, we are going to create a new OnClickListener().

Inside the onClick:
Intent myIntent = new Intent(arg1.getContext(), Editgoalie.class);
You are creating an “Intent” call myIntent (You “intend” to do something when the button is clicked). I am going to get the views context and I wish to launch a new screen/page in my android application called EditGoalie.class

startActivityForResult(myIntent, 0);
Now that you are going to be leaving the screen and moving to the next one, I want to start the activity and wait for the results because I need to return to this page.

Notes:
This example is assuming that you have already created two button in your main.xml file. Here is a sample of what they can look like.

<Button android:id="@+id/newGoalie"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/button_stick"
     android:textColor="#FFFFFF"
     android:text="@string/newGoalie"
     android:paddingTop="46dp"
     android:layout_marginTop="75dp"
     android:textSize="18px"
     android:textStyle="bold">
</Button>
    
<Button android:id="@+id/editGoalie"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/button_stick"
     android:textColor="#FFFFFF"
     android:text="@string/editGoalie"
     android:paddingTop="46dp"
     android:textSize="18px"
     android:textStyle="bold">
</Button>

When you start your application there is only one .java file under the src folder. If you are going to have a button launch a new screen you will need to create a new .java file (class) for each new screen. So in this example there are three .java files:
HockeyActivity.java (Main Starting Screen)
Newgoalie.java (Seen in the code as: Intent myIntent = new Intent(arg1.getContext(), Newgoalie.class);)
Editgoalie.java

When you launch either of the new screens, remember that you will need to return to your main screen by sending a valid Intent back. There are two kinds of results you will want to send back although there are actually more in the android documentation under Summary. Android Result Values.
1) setResult(RESULT_OK, okIntent);
2) setResult(RESULT_CANCELED, cancelIntent);

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: