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

 

Layout2.xml

 

  • 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

Activity2.java

 Android Manifest.xml

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

declaring activity in AndroidManifest.xml

 

 

Download Full Project :

[sociallocker]

Download From Google Drive

[/sociallocker]

 

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