Thursday, 15 January 2015

How to call an activity on Button click.

Step 1:

Create a project named "CallingActivity".

Step 2:

Paste the following code in your MainActivity.java file.

package com.example.callingactivity;

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

public class MainActivity extends Activity
{
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i = new Intent();
i.setClass(getApplicationContext(), Class2.class);
startActivity(i);
}
});
}
}

Step 3:

Make another class named Class.java.

Step 4:

Paste the following code in your Class.java file.

package com.example.callingactivity;
import android.app.Activity;
import android.os.Bundle;

public class Class2 extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.class2_layout);
}

}

Step 5:

Now go to res folder and then layout folder and open the activity_main.xml file.

Step 6:

Paste the following code in activity_main.xml file.
You will have this screen.

Step 7 :

Now Go to the layout folder make xml file named class2_layout.xml .

Step 8 :

Paste the following code in your class2_layout.xml file.

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Activity Two"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


Step 9 :

Now go to AndroidMenifest.xml file and paste the following code.

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

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

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

</manifest>

Step 10:

Now run your project.
This screen will be shown.
Click on button.
Now you will have this screen.

Thank you.

No comments:

Post a Comment