Prerequisite

If you not understand this lecture Starting new Activity on button click .you should also read my previous lectures.  Project creation using android studio . After read lecture you will also able to create activity of your app.

Overview

Activity is a user interface Almost all the activity are interact with user.Activity is window in which you can place their different component of user interface.Activity display information and invite the user to act in a number of ways.In android, an activity is represent a single screen.

Introduction

In this lecture well we describe how to working multiple activates in a same application in previous lecture (Greeting appears here) only one activity we will use. But In this lecture  we are  describes how to working multiple activity on a same project.After read this lecture  i hope you will create activity of your application.

Description of activity

Activity is a basic building block in android.Android Activity always has a User Interface.Android application consists of number of activity.when you create new activity it can be define in manifest file.In android, an activity is represent a single screen. Most applications have multiple activities to represent different screens. for example one activity to display a list of the application , another activity to display the application status.

You will done this section

  1. Detail Activity state
  2. create new activity
  3. create new layouts
  4. Declaring the activity in the manifest
  5. layout. XML code description
  6. Main Activity code description
  7. output

Detail Activity state

In android have four activity state. this helps  in Android  to identify activities that are no longer in use. that are describe in below pic.

Alt Tag activity state in android

  • Activity running Activities are considered active or running.
  • Paused  When the device goes to sleep, or an activity is still visible when you playing a game and suddenly cal coming  then you will paused a activity.And activity still alive all state.
  • Activity background Stopped activities still try to retain their state and member information for as long as possible.
  • Stop activity It is possible for an activity that is anywhere from paused to stopped.

Getting Started

Now we add two activity each activity associated two files. First is XML file that handle GUI and second is java class file that handle all logic that  are use to built an application. First we create a java class file  and assign  name of a class then we create a new layout and connected the new layout with java class file .then repeats this steps again  to create third activity and third layout like that create  an other java class file  assign the name of a class and  create a new layout and connected the new java class file with new layout. Next step is  include  all new activity in manifest file.  All detail is provides in steps.for more detail click on  Starting new activitiy on a button click.

Step1 = SecondActivity ->Right click-> the ->package name ->select ->New option-> and click-> java class that will show in below.  

Alt Tag create a new class

Step2=Step1->WriteName->Ok.

Alt Tag assign name of class

Now a new class  will add in this package  that show in below pic name SecondActivity.

Alt Tag second activity add in project

Now we create a another layout in XML file

Step3=Second_layout ->Right click> the ->layout folder-> select ->New option-> and click ->layout resource folder->

Alt Tag second layout add in project

Step4=Step3->WriteName->Ok.

Alt Tag assign name to layout

Now a new layout add in layout folder that show in below pic name second_layout.

Alt Tag add layout in project

Code are write in SecondActivity:

After write this code in second activity then  SecondActivity and second layout will connected.

Now we add a new activity and new layout . purpose of create new activity is  data pass from one activity to another . First of all you will create a  new activity and new layout  in your project. All the procedure how can create  new activity and  new layout is describe step by step. Create  an other java class file  assign the name of a class(thirdActivity) and  create a new layout and name of  layout is third_layout and connected the new java class file with new layout…(third_layout).

Step5=ThirdActivity->Right click-> the-> package name-> select ->New option-> and click-> java class->

Alt Tag add third activity

Step6=Step5->WriteName->Ok.

Alt Tag add assign name third activity

Now we create a Third _layout in XML file.

Step7=Third_layout ->Right click-> the ->layout folder-> select ->New option-> and click ->layout resource folder->

Alt Tag add third layout

Step8=Step7->WriteName->Ok

Alt Tag assign name to layout

  • Now a new class will add in  this package like  name ThirdActivity and  a new layout  will add in layout folder name  like third_layout that are show in below pic.

Alt Tag third layout add in project

 

Click to open popup

Subscribe and Receive the following . . .

  • Free Tip / Tricks
  • Latest Programming Tutorials
  • News about Best Tools

Code are write in ThirdActivity:

After paste this code in third activity then  ThirdActivity and third layout will connected

 New Activity in manifest

if you create new activity now you  add these files<activity android:name=Second Activity/><activity android:name=Third Activity/>  into manifests file. so open the manifest folder and open the AndroidManifest.xml and registered all the activity into manifest file.

open the AndroidManifest.xml and place that code

XML Layout 

First of you will open a XML file of layout and set  Relative Layout and specify explain the text view Text view consists four elements height and width id and text .first of all we will  set height and width of text view After that you will assign id to text.and also assign text message to text view.

Different property that are used in layout.xml file  explain here

  • warp_content   warp_content means height and width similar to the text that are entered.
  • Match_parent   means height and width similar to parent.
  • Android text property show text message in output screen.
  •  Android on Click when user click on a button this method execute.

 Note : First of all you will remove these these line of code in layout XML .

After delete the above code our code is look like this.In this example we are going to create a simple UI that includes only (Text View) with Different attributes  And write the following code of starting new activity in a button click .

Now open the Graphical layout editor to preview the User Interface you created.

Alt Tag click on a button and start activity

Code that are placed in java class file MainActivity.java

Now open MainActivity.java and include a text view variable. After this step we will connected the layout.xml  file and main _activity .this line will connected the main_activity and layout.xml.

when user click on a button this method will execute.

Two buttons are define inside this method and  if else condition used if user click on a first button then first condition will invoke.  if user not click on a first button and click on second button then second button will invoke.inside these button intent used intent basically used start a  new activity.

Code that are placed in java class file MainActivity.java

Android code starting new activity

Intent which is use start new activity.when you start a new activity then you will place this code in exact position i.After place this code you will move one activity to another.

Define the Button

when you click on first button this code will invoke. Intent  start working .  when user click on button it can move from one activity to next activity.

when you click second button this code  will invoke. Intent start working .  when user click on button it can move from one activity to next activity.

Run the application

Alt tag android activity image

Output if you click in first button this message will show and Starting new Activity

Alt Tag click on first button

Output if you click in second button this message will show and Startinng  new Activity

Alt Tag click on second button

 

“Thank you”  for reading this lecture, Hope you got the idea.

  • James Nunn

    Thanks great help easy to understand.

    • Thank you for support .We are continuously trying to improve the quality of previous and upcoming lectures. So stay connected

  • Leo Júnior

    Hey thanks for share this! U rock!

  • TPR Sharq Trading

    Error:(14, 6) error: reached end of file while parsing

    • Euan

      will most likely be that your missing } at the end

  • TPR Sharq Trading

    package cyberspark.com.cyberspark;

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

    /**
    * Created by user on 11/8/2015.
    */
    public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_layout );
    }

  • Mario German Agudelo

    Hi thanks for sharing your knowledge: I have an application which has two buttons on the main activity, the idea is that by giving click on them, I send activity recyclerview which would be the same for the two buttons, the only thing is that by giving click button 1 to display information only on that button and the button 2 to give click to show only the information that button, I appreciate all the information you can give me, thank you.

  • Angela Smith

    Thank you so much for taking the time to write this out in an easy to understand format. It is great!

  • Eunrie Manuel

    please help meee i dont know what is the problem of this R.id