Opening an activity on a button click in Android

June 19, 2012
By

In this article, we will see , how to open an activity from the main activity by a button click.


1. Create a new Android Project namely OpenActivity

New Android Project

Figure 1: New Android Project


2. Select build target for the application

Android Build Target

Figure 2: Android Build Target


3. Enter application details

Application Details

Figure 3: Application Details


4. Open and update the file /res/values/strings.xml with the highlighted code given below


<?xml version="1.0" encoding="utf-8"?>
<resources highlight="3,4,5">
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">Open Activity Demo</string>
    <string name="lblBtn">Open Activity Demo</string>
    <string name="lblTxt">This is another activity</string>
</resources>

5. Create a new layout file called another.xml in the directory /res/layout and update it with the given below code


<?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/txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/lblTxt"
        android:gravity="center"
    />
</LinearLayout>

6. Create a new class file called AnotherActivity.java in the directory /res/src/in/wptrafficanalyzer/openactivity and update the file with the given below code

package in.wptrafficanalyzer.openactivity;

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

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

7. Open and update the file /res/layout/main.xml with the given below 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:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/lblBtn"
    />
</LinearLayout>


8. Open and update the file /res/src/in/wptrafficanalyzer/openactivity/MainActivity.java


package in.wptrafficanalyzer.openactivity;

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 {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("in.wptrafficanalyzer.AnotherActivity");
                startActivity(intent);
            }
        };

        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(listener);
    }
}


9. Update the file /AndroidManifest.xml with the given below code


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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <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=".AnotherActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="in.wptrafficanalyzer.AnotherActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>
</manifest>

10. Run the application

MainActivity Screen

Figure 4 : MainActivity Screen

Another Activity Screen

Figure 5: Another Activity Screen

Note : On clicking the button “Open Activity Demo”, the activity shown in the Figure 5 will be displayed


11. Download Project Source Code


12. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , ,

2 Responses to Opening an activity on a button click in Android

  1. Feroz on March 4, 2015 at 3:06 pm

    Sir,

    i have pdf file near about 200 pages.
    how to get its content on screen in android eclipse / android studio.

    wants show its content as chapter
    how to programme this type of application.

    kindly help

  2. hany on July 21, 2015 at 1:19 pm

    i want open 3 activity with 3 buttons

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner