.
104-Programmr: Events and Actions in Android
Events and Actions are two important concepts in programming.
Events refer to changes that occur during the running of a program that may trigger a response from the computer.
Actions refer to computer activities that respond to the Events.
A simple example is button clicks event eg when the user clicks a button, computer will respond by displaying a new window.
The task of displaying a new window may require a series of steps.
These steps are grouped together in a block, which is called “method” in Java Language.
|
0) Starting Up
Continue from the previous tutorial, http://android-steps.blogspot.my/2015/01/103-programmr-using-image-resources-in.html .
1) Events
We will register a button click event to the About Button.
Add a new attribute android:onClick with a value "showAbout".
This means whenever the user clicks the button, the program will find showAbout codes (or to be more precise, in Java Language, we call it showAbout method)
|
File: res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:background="@drawable/bgcloud"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/smiley"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MyAndroid"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About"
android:onClick="showAbout"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
/>
</LinearLayout>
</LinearLayout>
|
OUTCOME.
If you run the Apps and click the About button, you will get an error message “Unfortunately, MyAndroid has stopped”.
This actually means that the onClick event has been responded by the app however it could not find showAbout method and thus it encountered an error that caused itself to stop running.
We will add showAbout method in the following step.
|
2) Actions
Action codes are written in Java files.
In this example, it is MyAndroid.java.
Add showAbout method.
Import View Class to support the method.
|
File: MyAndroid.java
package com.example.MyAndroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MyAndroid extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showAbout(View v){
//to do
}
}
|
If you run and click the About button, you should not get the error anymore.
However, the program remains idle as the showAbout method does not contain anything yet.
Add a Toast Object (a kind of pop up message).
Import a Toast class in order to use the Toast Object.
|
File: MyAndroid.java
package com.example.MyAndroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MyAndroid extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showAbout(View v){
Toast.makeText(getApplicationContext(), "About", Toast.LENGTH_LONG).show();
}
}
|
OUTCOME.
No comments:
Post a Comment