Sunday, 31 May 2015

Android activity example | Button click event

No comments
In this tutorial, we show you how to interact with activity by implement On button click activity. when a button is clicked, go one screen to another screen.Open one XML file to another XML file on button click.Most applications have multiple activities to represent different screens, for e.g. one activity to display a list of the application status and another activity is to display the application functions,settings etc...

CREATE NEW ANDROID PROJECT

In this example we need Two XML file and Two java file.
XML:
1) activity_main.xml
2) activity_main2.xml


JAVA:
1) MainActivity.java
2) MainActivity2.java

Step 1: Write code into activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"
android:orientation="vertical" >



    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First screen (activity_main.xml)"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click me" />

</LinearLayout>

Step 2: Write code into activity_main2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second screen (activity_main2.xml)"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Step 3: Write code into MainActivity.java

package com.example.activityapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}

public void addListenerOnButton() {

final Context context = this;

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

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

Intent intent = new Intent(context, MainActivity2.class);
                startActivity(intent);   

}

});

}

}


Step 4: Write code into MainActivity2.java

package com.example.activityapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity2 extends Activity {

Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}

}

Step 5: write code into AndroidManifest.xml

Register second activity in Androidmanifest.xml file.

        <activity
            android:name=".MainActivity2"
            android:label="@string/app_name" >
        </activity>

IT's look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activityapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.activityapp.MainActivity"
            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=".MainActivity2"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

Step 6:Now Run Your Project:




No comments :

Post a Comment

Follow me Share