How to use Event listener for your android app tutorial


In this lesson you'll learn to set event listeners in Java code. Run this app which shows a button and a text. First assign a new ID to the TextView by setting attribute android:id to value @+id/mainTextView1. Next assign a new ID mainButton1 to the Button. Inside the onCreate method get the TextView object with findViewById and assign it to variable TextView textView. Now also get the Button object with findViewById and assign it to variable Button button. We have set a click event listener to the button with setOnClickListener. The onClick method is called when the button is clicked. Inside the onClick method set the text of the TextView object to Clicked with setText(). Set an OnLongClickListener on the Button object with setOnLongClickListener().
Now inside the onLongClick method set the text of the TextView object to Long Clicked with setText(). Finally make the onLongClick method return true, to tell Android that the event was handled. Congratulations! You've learned how to set event listeners in Java code. Play with the code on your own a bit. Also try other controls which define additional events.

REQUIREMENTS:
Download:
Eclipse

Instruction:
After you download all the files needed in this tutorial, we need to install and set-up a java JDK path to our environment variables, the step below will show you how.

--Go to Java JDK installation directory in "C:\Program Files\Java\jdk1.7.0\bin" Copy this path
--Go to the environment path by Clicking: Window taskbar--->right click Computer--->Click Advance System setting ---> then go to Advanced tab Then select "Environment Variable" then create new Variable name and variable value by clicking the New radio button. Within the "Variable name" input these "JAVA_HOME" then in the "Variable value" point it to your C:\Program Files\Java\jdk1.7.0\bin 
that's it we are on a way of creating our first game and deploy it to a different platform.

After that we will also set-up Android SDK, to its path by extracting first it to the directory you want for example in "drive D:" or "drive C:"  once we did this, we can now copy the root folder of this SDK which is likely to this format "D:\Android SDK" copy it then add it also to the "Environment path" like what you did before in "Java JDK".

COMPLETE CODES
MAINACTIVITY CODE

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        final TextView textView = (TextView) findViewById(R.id.mainTextView1);
        Button button = (Button) findViewById(R.id.mainButton1);
        
        button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View p1)
{
// TODO: Implement this method
textView.setText("Clicked");
}
});

button.setOnLongClickListener(new OnLongClickListener() {

@Override
public boolean onLongClick(View p1)
{
// TODO: Implement this method
textView.setText("Long Clicked");
return true;
}
});
    }
}


MAIN XML CODE



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/mainButton1"
android:text="Button"
android:textSize="20dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />

<TextView
android:id="@+id/mainTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press the button"
android:textSize="20dp"
android:layout_marginTop="20dp"/>
</LinearLayout>

Post a Comment

0 Comments