Android Programming Tutorials

Android Development Tutorial #4 – Switching Between Activities – Example

Written by Jana Raj

We Already Seen Some Of The Basic Android Development Tutorial On Tech4bros , In This Android Development Tutorial #4 We Will See How to Switch Between Two Activities Using Button .

What is An Activity ?

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI setContentView(View) . 

Source : developer.android.com

Example_sketch

Initial Setup :

For Initial Setup of SDK and AVD follow this tutorial – Android Development Tutorial #1 : Setting Up ADT & Developing Hello World App.

File Tree :

file tree tutorial 4

Project :

  • Create new project in eclipse by selecting File -> New -> Android Application Project. Give a Application name, Project name and Package name.

switch betwwn activities

 

  • Create 2 Layouts Named “Layout 1.xml“,”Layout 2.xml” By Doing Right Click on Layout Folder Under res/layout Using New > Android XML File

Layout1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Activity1" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="88dp"
        android:text="@string/layout_1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="43dp"
        android:text="@string/go_to_layout_2" />

</RelativeLayout>

 

Layout2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Activity1" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="@string/layout_2"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:text="@string/go_to_layout_1" />

</RelativeLayout>

 

  • Create 2 Activity Files Under Source Folder Named “Activity1.java“,”Activity2.java” By Doing Right Click on src Folder New >  Class 

creating new java class

 

  • We Are Using OnClickListener() Function in Activity File i.e , When A Button of ID = xxx Clicked What it have to do ? It Have to Open the Second layout And For the Activity2 Vice Versa .

Activity1.java

package com.tech4bros.switchactivity;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
public class Activity1 extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout1);
    final Button switchact =(Button)findViewById(R.id.button1);
    switchact.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        Intent act2 = new Intent(view.getContext(),Activity2.class);
        startActivity(act2);
      }
    });
}}

Activity2.java

package com.tech4bros.switchactivity;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
public class Activity2 extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout2);
    final Button switchact =(Button)findViewById(R.id.button2);
    switchact.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        Intent act2 = new Intent(view.getContext(),Activity2.class);
        startActivity(act2);
      }
    });
}}

 Android Manifest.xml

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

The Above Code Should be Added In Order to Declare the Created Activity

declaring activity in AndroidManifest.xml

 

 

Download Full Project :

 

About the author

Jana Raj

Janarthanan is the founder and Chief Editor of Tech4bros .He is a Student of Computer Science and Engineering ,a full time Geek, part time Blogger and Quarter time Coder. He loves Blogging, Web Designing and making new friends . Google Plus

Leave a Reply

1 Comment