Published using Google Docs
105 Android Studio Listening To Events
Updated automatically every 5 minutes

105 Android Studio Listening To Events

We have learned that programming codes can be separated by their roles; 1) VIEWS and 2) CONTROLS.

Whenever important EVENTS occur, our program will respond with ACTIONS. Thus our program must be made aware of ie LISTEN TO any changes in program state that give rise to EVENTS.

EVENT LISTENING can be coded in two ways; 1)Set onClick Property in VIEW File or 2)Write onClick Listener in CONTROL File.

1) Set onClick Property in VIEW File

We have done this in the previous tutorial, http://android-steps.blogspot.my/2015/11/104-android-studio-events-and-actions.html .

Or download startup file here.

Recall…

File Name: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

   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:paddingLeft="@dimen/activity_horizontal_margin"

   android:paddingRight="@dimen/activity_horizontal_margin"

   android:paddingTop="@dimen/activity_vertical_margin"

   android:paddingBottom="@dimen/activity_vertical_margin"

   tools:context=".MainActivity"

   android:orientation="horizontal">

   <TextView android:text="Name:"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content" />

   <EditText

       android:id="@+id/edit_message"

       android:layout_width="0dp"

       android:layout_weight="1"

       android:layout_height="wrap_content"

       android:hint="Type name here..." />

   <Button

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="Send"

       android:onClick="sendMessage"/>

</LinearLayout>

File Name: MainActivity.java

package com.notarazi.myemptyproject1;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

   }

   public void sendMessage(View view) {

       EditText editText = (EditText) findViewById(R.id.edit_message);

       Toast.makeText(getApplicationContext(), "You have entered, \n" + editText.getText().toString(), Toast.LENGTH_LONG).show();

   }

}

In this example, LISTENING is placed in the VIEW file while ACTIONS is of course placed in the CONTROL file.

This doesn’t involve much codes. Hence is easy for beginners.

However, suppose ACTIONS do not exist, the program will encounter a runtime error.

2) Write onClick Listener in CONTROL File

Edit the two files above as follows.

Add Id “send_message” to the button

Delete the strikethrough text.

File Name: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

   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:paddingLeft="@dimen/activity_horizontal_margin"

   android:paddingRight="@dimen/activity_horizontal_margin"

   android:paddingTop="@dimen/activity_vertical_margin"

   android:paddingBottom="@dimen/activity_vertical_margin"

   tools:context=".MainActivity"

   android:orientation="horizontal">

   <TextView android:text="Name:"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content" />

   <EditText

       android:id="@+id/edit_message"

       android:layout_width="0dp"

       android:layout_weight="1"

       android:layout_height="wrap_content"

       android:hint="Type name here..." />

   <Button

       android:id="@+id/send_message"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="Send"

       android:onClick="sendMessage"/>

</LinearLayout>

Add Listening Code in onCreate code section

File Name: MainActivity.java

package com.notarazi.myemptyproject1;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       Button button = (Button) findViewById(R.id.send_message);

       button.setOnClickListener(new View.OnClickListener() {

           public void onClick(View v) {

               sendMessage(v);

           }

       });

   }

   public void sendMessage(View view) {

       EditText editText = (EditText) findViewById(R.id.edit_message);

       Toast.makeText(getApplicationContext(), "You have entered, \n" + editText.getText().toString(), Toast.LENGTH_LONG).show();

   }

}

In this example, both LISTENING and ACTION are written in the same file ie CONTROL File.

It is easier to maintain codes this way because they stay in the same place and hence programmer would not miss any of them.

OUTCOME

There is no difference in terms of behavior and look

DOWNLOAD

MyEmptyProject1_listen.zip