Tuesday, November 10, 2015

104 Android Studio Events and Actions


.
104 Android Studio Events and Actions
This tutorial explains the concept of Events and Actions which are fundamental to logic processing (CONTROLLERS).
The tutorial was done with Android Studio 1.4.
1) START UP
User normally types the name in the Edit Text object and then tap the SEND button.
Once the button is tapped, our program will respond with some processing.
In this example, the most important EVENT would be the tapped event.
Our program must be made aware of the event (LISTENING) and it should respond with proper actions.
2) ADD A CALL TO PROGRAM CODES
Edit the layout file by adding a call to program codes.
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>
The statement android:onClick="sendMessage" means whenever the button is clicked or tapped, find “sendMessage” to respond. In Java Programming, sendMessage technically is called a METHOD.
Let’s say we run the apps now. What would happen?
Explanation:
When the user tap the button, a call to “sendMessage” Method was triggered.
However, Android could not find “sendMessage” Method. Therefore a critical error is displayed on screen and Android System shuts down..
3) EDIT SENDMESSAGE METHOD
We will edit MainActivity.java to insert sendMessage method.
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.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) {
       Toast.makeText(getApplicationContext(), "Responding...", Toast.LENGTH_LONG).show();
   }
}
A method is declared with few reserved words.
  • public = this method can be called from outside of its class (e.g button can call this method)
  • void = this method does not return any value after it has been executed
  • (View view) = this method receives a View object (e.g the visual object on the screen) which allows the method to hook to this object and perform logic processing.
This method contains a statement
  • Toast command (toast=a kind of pop up message)
  • Toast command displays a string  “Responding…
  • It was a long duration type (LENGTH_LONG)
OUTCOME
4) INTERACTION BETWEEN METHODS AND VIEWS OBJECTS
We have seen how a VIEW object (in which a button is part of it) CALLED the sendMessageMETHOD to perform ACTIONS when the onClick EVENT is triggered.
Besides receiving a call, sendMessage METHOD is able to get more information from the VIEW eg accessing the value of EditText object.
This is done by hooking a CODE OBJECT to the VIEW OBJECT. The concept is very much similar to the Voodoo black magic; once a doll is hooked to a person, the witch can hurt the person by pinching the doll.
We want to get the value in the EditText object and display it in the Toast object.
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();
   }
}
The first statement hooks a CODE object to a VIEW object.
The second statement try to access the Text property of the editText object and convert the property to string.
Finally the value is displayed in Toast message.
OUTCOME.
DOWNLOAD

.

No comments:

Post a Comment