Saturday, June 16, 2012

Android Button Event

In my previous post, I have mentioned that how create an android project. So I directly give instructions after creating the project part.
My project name is 'ButtonEventApp', In this project I have two java class in src and two xml file in layout.
Therefore you need to create java class named  ButtonEventAppActivity.
  Select ButtonEventApp in Package Explorer and right click the package then -> New -> Class
Name : ButtonEventActivity2

Then create a xml file in layout folder
  Select ButtonEventApp in Package Explorer and right click the layout then -> New -> Android XML File
File : main2

This is my ButtonEventAppActivity.java class
package com.buttonevent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ButtonEventAppActivity extends Activity {
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button next = (Button) findViewById(R.id.button1);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), ButtonEventActivity2.class);
                startActivityForResult(myIntent, 0);
            }
        });
    }
}


This is my ButtonEventAppActivity2.java class
package com.buttonevent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ButtonEventActivity2 extends Activity{
/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        Button next = (Button) findViewById(R.id.button2);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}

This is my main,xml file
I have added a text view and a button, By double clicking on them, we can edit their variables
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    <TextView android:layout_height="wrap_content" android:text="Page 1" android:id="@+id/textView1" android:layout_width="match_parent" android:layout_weight="0.05"></TextView>
    <Button android:text="Go Next Page" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_width="match_parent" android:layout_weight="0.05"></Button>
</LinearLayout>

This is my main2.xml file
<?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:weightSum="1">
    <TextView android:layout_height="wrap_content" android:text="Page 2" android:id="@+id/textView1" android:layout_width="match_parent" android:layout_weight="0.06"></TextView>
    <Button android:text="Go Previous Page" android:layout_height="wrap_content" android:id="@+id/button2" android:layout_weight="0.06" android:layout_width="match_parent"></Button>
    
</LinearLayout>

After that We have to add activity for the AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.buttonevent"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ButtonEventAppActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<activity android:name=".ButtonEventActivity2"></activity> 
    </application>
</manifest>

You can download source file of the project.

No comments:

Post a Comment