How To Work With Multiple Activities And Navigate The Activities In Android Studio

Introduction

Android is one of the most popular operating systems for the mobile. I am sure that basically one Application contains multiple activities and lots of modules. I will show you how to work with the multiple activities and navigate the activities in an Android Studio. Android is the kernel based operating system. It allows the user to modify the GUI components and the source code.

Requirements

  • Android Studio.
  • A little bit XML and Java knowledge.

Download link for an Android Studio- https://developer.android.com/studio/index.html

Steps to be followed are given below.

Carefully follow my steps to work with the multiple activities and navigate the activities in an Android Studio. I have included the source code below.

Step 1

Open an Android Studio and start a new project.

android

Step 2

Put the Application name and the company domain. If you wish to use C++ for coding the project, mark Include c++ support, followed by clicking Next.

android

Step 3

Select an Android minimum SDK. After you chose the minimum SDK; it will show the approximate percentage of the people, using SDK, followed by clicking Next.

android

Step 4

Choose the empty activity, followed by clicking Next.

android

Step 5

Put the activity name and the layout name. Android Studio basically takes Java class name, which you provide for the activity name and click Finish.

android

Step 6

Go to activity_first.xml, followed by clicking the text bottom. This XML file contains the designing code for an Android app. In activity_first.xml, copy and paste the code given below.

activity_first.xml code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     tools:context="ganeshannt.frist.FristActivity">  
  9.   
  10.     <Button  
  11.         android:id="@+id/button2"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:onClick="Ganesh"  
  15.         android:text="click third activity"  
  16.         android:textColor="@color/colorPrimary"  
  17.         app:layout_constraintTop_toTopOf="parent"  
  18.         tools:layout_editor_absoluteX="168dp"  
  19.         android:layout_alignParentBottom="true"  
  20.         android:layout_toEndOf="@+id/text"  
  21.         android:layout_marginBottom="196dp" />  
  22.   
  23.     <TextView  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="This s my first app!"  
  27.         android:id="@+id/text"  
  28.         tools:layout_editor_absoluteY="8dp"  
  29.         tools:layout_editor_absoluteX="8dp" />  
  30.     <Button  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.         android:id="@+id/button"  
  34.         android:text="click second activity"  
  35.         android:textColor="@color/colorPrimary"  
  36.         android:onClick="Ganesh"  
  37.         tools:layout_editor_absoluteX="168dp"  
  38.         app:layout_constraintTop_toTopOf="parent"  
  39.         android:layout_above="@+id/button2"  
  40.         android:layout_alignStart="@+id/button2"  
  41.         android:layout_marginBottom="40dp" />  
  42.   
  43. </RelativeLayout>   

Step 7

Create the new activity_second.xml file. (XML File directory :App -> res-> layout). Put your activity name, followed by clicking OK.

android

android

Step 8

Go to activity_second.xml, followed by clicking the text bottom. This XML file contains the designing code for an Android app. In activity_second.xml, copy and paste the code given below.

activity_second.xml code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <TextView  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_margin="20pt"  
  10.         android:text="second acticity is working...."  
  11.         android:textAllCaps="true"  
  12.         android:textColor="@color/colorPrimaryDark"/>  
  13.   
  14. </LinearLayout>   

Step 9

Repeat the step 7and create an activity_third.xml. Go to activity_third.xml, followed by clicking text bottom. This XML file contains designing the code for an Android app. In an activity_third.xml, copy and paste the code given below.

activity_third.xml code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <TextView  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_margin="20pt"  
  10.         android:text="Third activity is working ........."  
  11.         android:textAllCaps="true"  
  12.         android:textColor="@color/colorPrimary"  
  13.         />  
  14.   
  15. </LinearLayout>  
Step 10

In FirstActivity.java, copy and paste the code given below. Java programming is the backend language for an Android. Do not replace your package name, else an app will not run. The code given below contains my package name. FirstActivity.java code is given below.
  1. package ganeshannt.frist;  
  2.   
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.TextView;  
  9.   
  10. public class FristActivity extends AppCompatActivity {  
  11.       TextView textView;  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_frist);  
  17.         textView = (TextView) findViewById(R.id.text);  
  18.   
  19.     }  
  20.   
  21.     public void Ganesh(View View)  
  22.     {  
  23.        String button_text;  
  24.        button_text =((Button)View).getText().toString();  
  25.         if(button_text.equals("click second activity"))  
  26.         {  
  27.             Intent ganesh = new Intent(this,SecondActivity.class);  
  28.             startActivity(ganesh);  
  29.         }  
  30.         else if (button_text.equals("click third activity"))  
  31.         {  
  32.             Intent mass = new Intent(this,ThirdActivity.class);  
  33.             startActivity(mass);  
  34.   
  35.         }  
  36.     }  
  37. }  
Step 11 

Create the new SecondActivity.java file. (Java file directory :App -> Java-> your package name). Put your class name, followed by clicking OK.

android

android

Step 12

In SecondActivity.java, copy and paste the code given below. Do not replace your package name, else an app will not run. The code given below contains my package name.

SecondActivity.java code

  1. package ganeshannt.frist;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.support.annotation.Nullable;  
  6.   
  7. /** 
  8.  * Created by For on 4/14/2017. 
  9.  */  
  10.   
  11. public class SecondActivity extends Activity {  
  12.     @Override  
  13.     protected void onCreate(@Nullable Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_second);  
  16.   
  17.     }  
  18. }   

Step 13

Repeat the step 11and create ThirdActivity.java. Go to ThirdActivity.java, followed by copying and pasting the code given below.

ThirdActivity.java

  1. package ganeshannt.frist;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.support.annotation.Nullable;  
  6.   
  7. /** 
  8.  * Created by For on 4/14/2017. 
  9.  */  
  10.   
  11. public class ThirdActivity extends Activity {  
  12.     @Override  
  13.     protected void onCreate(@Nullable Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_third);  
  16.   
  17.     }  
  18. }   

Step 14

Add the code given below into androidminifest.xml within the Application tab. Do not replace all the code, just copy and paste the code given below.

Androidminifest.xml 

  1. <activity android:name=".SecondActivity"></activity>  
  2. <activity android:name=".ThirdActivity"></activity>   

Step 15

This is our user interface of the Application. Click Make project option.

android

Step 16

Run the Application, followed by choosing the virtual machine. Click OK.

android

Deliverables

Here, you are successfully working with the multiple activities. Navigate the created and executed activities in and an Android Studio Application. 
android

Click the second activity button and it will show the second activity page.

android

Click the third activity button and it will show the third activity page.

android

If you have any doubts, just comment below.

Up Next
    Ebook Download
    View all
    Learn
    View all