Android - Event Handling in Activity


So you have linked the xml layout file and the activity file and now you want to handle the click event. There are multiple ways of doing this. The best way is that your activity implements an interface called OnClickListener.

Suppose our application user interacts with our layout i.e they fill the required fields and click on the submit button. TThe moment they click the submit button we want to process the entered information. So for that we need some event handling mechanism.

Create a subclass of Activty and make it implement an interface called OnClickListener.

public class FirstActivity extends Activity implements OnClickListener {
}

This interface has a method named onClick() which is called when event such as click of button occurs. We need to implement this method inside our Activity.

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

Screenshot of the tutorial

Before click event

 

After click event

 

So, create a new Activity

Note : If you want to make button clickable then you need to add following line of code tp your button 

 submit.setOnClickListener(this); 

Also the Toast().show() helps to create a pop up on the screen or you can say an alert on the user interface. This method takes 3 arguments :

  1. The reference of the activity.
  2. The text to be displayed as a second argument.
  3. The duration of alert. It can be "LENGTH_LONG" or "LENGTH_SHORT".

The Activity code is already explained in our previous tutorials, so if you have just started learning android and are uncomfortable with the code below then i request you to go back and study previous notes related to Activity in Android.

package com.example.activityproject;

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

public class FirstActivity extends Activity implements OnClickListener {
	EditText username;
	Button submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        username = (EditText) findViewById(R.id.editText1);
        submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(this);        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.first, menu);
        return true;
    }


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String msg = username.getText().toString();
	    Toast.makeText(this, "the text entered by user is "+msg, Toast.LENGTH_LONG).show();
		
	}
    
}

The layout file is very simple, just drag and drop from eclipse. No rocket science in it.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FirstActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="56dp"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_centerVertical="true"
        android:text="Submit" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/submit"
        android:layout_marginBottom="24dp"
        android:text="Please enter your name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

So in this note we learned about adding event handling on buttons. In the next tutorial we will add more buttons and make them clickable to start new Activity with the help of Intent.