The Wayback Machine - https://web.archive.org/web/20240915133913/https://www.geeksforgeeks.org/creating-multiple-screen-applications-in-android/
Open In App

Creating Multiple Screen Applications in Android

Last Updated : 30 Aug, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

This article shows how to create an android application to move from one activity to another. Below are the steps for Creating a Simple Android Application to move from one activity to another activity.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

You will have XML and Activity Files, the path of both files is given below.

Step 2: Working with the ActivityOne Kotlin/Java/XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML




<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".Oneactivity"
    tools:layout_editor_absoluteY="81dp">
  
    <TextView
        android:id="@+id/question1_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="60dp"
        android:text=""
        android:textColor="@android:color/background_dark"
        android:textStyle="bold" />
  
    <!-- add button after click this we can goto another activity-->
    <Button
        android:id="@+id/first_activity_button"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_marginLeft="90dp"
        android:layout_marginTop="200dp"
        android:text="Next"
        android:textColor="@android:color/background_dark"
        android:textSize="15sp"
        android:textStyle="bold" />
</RelativeLayout>


Go to the MainActivity File and refer to the following code.

Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.

Java




import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
  
public class Oneactivity extends AppCompatActivity {
  
    // define the global variable
    TextView question1;
    // Add button Move to Activity
    Button next_Activity_button;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_oneactivity);
  
        // by ID we can use each component which id is assign in xml file
        // use findViewById() to get the Button
        next_Activity_button = (Button) findViewById(R.id.first_activity_button);
        question1 = (TextView) findViewById(R.id.question1_id);
  
        // In question1 get the TextView use by findViewById()
        // In TextView set question Answer for message
        question1.setText("Q1 - How to pass the data between activities in Android?\n" + "\n" + "Ans - Intent");
  
        // Add_button add clicklistener
        next_Activity_button.setOnClickListener(v -> {
            // Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining 
            // the components you are targeting. Intent to start an activity called SecondActivity with the following code.
            Intent intent = new Intent(Oneactivity.this, SecondActivity.class);
            // start the activity connect to the specified class
            startActivity(intent);
        });
    }
}


Kotlin




import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
  
class Oneactivity : AppCompatActivity() {
  
    // define the global variable
    private lateinit var question1: TextView
    // Add button Move to Activity
    private lateinit var next_Activity_button: Button
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_oneactivity)
  
        // by ID we can use each component which id is assign in xml file
        // use findViewById() to get the Button
        next_Activity_button = findViewById(R.id.first_activity_button)
        question1 = findViewById(R.id.question1_id)
  
        // In question1 get the TextView use by findViewById()
        // In TextView set question Answer for message
        question1.text = "Q1 - How to pass the data between activities in Android? Ans - Intent".trimIndent()
  
        // Add_button add clicklistener
        next_Activity_button.setOnClickListener {
            // Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining
            // the components you are targeting. Intent to start an activity called SecondActivity with the following code.
            val intent = Intent(this, SecondActivity::class.java)
            // start the activity connect to the specified class
            startActivity(intent)
        }
    }
}


Now we have to create another activity (SecondActivity) to move from one activity to another.

Create second activity and go to the android project > File >new > Activity > Empty Activity 

Step 3: Working with the ActivityTwo Kotlin/Java/XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML




<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".SecondActivity">
  
    <TextView
        android:id="@+id/question2_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="60dp"
        android:text=""
        android:textColor="@android:color/background_dark"
        android:textStyle="bold" />
  
    <Button
        android:id="@+id/second_activity_next_button"
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:layout_marginLeft="200dp"
        android:layout_marginTop="200dp"
        android:text="Next"
        android:textColor="@android:color/background_dark"
        android:textSize="15sp"
        android:textStyle="bold" />
  
    <Button
        android:id="@+id/second_activity_previous_button"
        android:layout_width="110dp"
        android:layout_height="40dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="200dp"
        android:text="previous"
        android:textColor="@android:color/background_dark"
        android:textSize="15sp"
        android:textStyle="bold" />
</RelativeLayout>


Go to the MainActivity File and refer to the following code.

Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.

Java




import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
  
public class SecondActivity extends AppCompatActivity {
  
    // define the global variable
    TextView question2;
    // Add button Move to next Activity and previous Activity
    Button next_button, previous_button;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
  
        // by ID we can use each component which id is assign in xml 
        // file use findViewById() to get the both Button and textview
        next_button = (Button) findViewById(R.id.second_activity_next_button);
        previous_button = (Button) findViewById(R.id.second_activity_previous_button);
        question2 = (TextView) findViewById(R.id.question2_id);
  
        // In question1 get the TextView use by findViewById()
        // In TextView set question Answer for message
        question2.setText("Q 2 - What is ADB in android?\n" + "\n" + "Ans- Android Debug Bridge");
  
        // Add_button add clicklistener
        next_button.setOnClickListener(v -> {
            // Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining 
            // the components you are targeting. Intent to start an activity called ThirdActivity with the following code.
            Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
            // start the activity connect to the specified class
            startActivity(intent);
        });
  
        // Add_button add clicklistener
        previous_button.setOnClickListener(v -> {
            // Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining 
            // the components you are targeting. Intent to start an activity called oneActivity with the following code
            Intent intent = new Intent(SecondActivity.this, Oneactivity.class);
            // start the activity connect to the specified class
            startActivity(intent);
        });
    }
}


Kotlin




import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
  
class SecondActivity : AppCompatActivity() {
  
    // define the global variable
    private lateinit var question2: TextView
    // Add button Move to next Activity and previous Activity
    private lateinit var next_button: Button
    private lateinit var previous_button: Button
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
  
        // by ID we can use each component which id is assign in xml 
        // file use findViewById() to get the both Button and textview
        next_button = findViewById(R.id.second_activity_next_button)
        previous_button = findViewById(R.id.second_activity_previous_button)
        question2 = findViewById(R.id.question2_id)
  
        // In question1 get the TextView use by findViewById()
        // In TextView set question Answer for message
        question2.text = "Q2 - What is ADB in android? Ans - Android Debug Bridge".trimIndent()
  
        // Add_button add clicklistener
        next_button.setOnClickListener {
            // Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining 
            // the components you are targeting. Intent to start an activity called ThirdActivity with the following code.
            val intent = Intent(this, ThirdActivity::class.java)
            // start the activity connect to the specified class
            startActivity(intent)
        }
  
        // Add_button add clicklistener
        previous_button.setOnClickListener {
            // Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining 
            // the components you are targeting. Intent to start an activity called oneActivity with the following code
            val intent = Intent(this, Oneactivity::class.java)
            // start the activity connect to the specified class
            startActivity(intent)
        }
    }
}


Now, we have to create third activity same as the second activity, and the path of this file is also the same as another.

“Now, you can create many activities like this”. Here we add TextView for messages and one Button for goto previous activity. It will be shown below

Step 4: Working with the ActivityThree Kotlin/Java/XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML




<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".ThirdActivity">
  
    <TextView
        android:id="@+id/question3_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="60dp"
        android:textColor="@android:color/background_dark"
        android:textStyle="bold" />
  
    <Button
        android:id="@+id/third_activity_previous_button"
        android:layout_width="110dp"
        android:layout_height="40dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="200dp"
        android:text="previous"
        android:textColor="@android:color/background_dark"
        android:textSize="15sp"
        android:textStyle="bold" />
</RelativeLayout>


Go to the MainActivity File and refer to the following code.

Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.

Java




import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
  
public class ThirdActivity extends AppCompatActivity {
  
    // define the global variable
    TextView question3;
    // Add button Move previous activity
    Button previous_button;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
  
        // by ID we can use each component which id is assign in xml 
        // file use findViewById() to get the Button and textview.
        previous_button = (Button) findViewById(R.id.third_activity_previous_button);
        question3 = (TextView) findViewById(R.id.question3_id);
  
        // In question1 get the TextView use by findViewById()
        // In TextView set question Answer for message
        question3.setText("Q 3 - How to store heavy structured data in android?\n" + "\n" + "Ans- SQlite database");
        // Add_button add clicklistener
        previous_button.setOnClickListener(v -> {
            // Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining 
            // the components you are targeting. Intent to start an activity called SecondActivity with the following code:
            Intent intent = new Intent(ThirdActivity.this, SecondActivity.class);
            // start the activity connect to the specified class
            startActivity(intent);
        });
    }
}


Kotlin




import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
  
class ThirdActivity : AppCompatActivity() {
      
    // define the global variable
    private lateinit var question3: TextView
    // Add button Move previous activity
    private lateinit var previous_button: Button
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_third)
  
        // by ID we can use each component which id is assign in xml
        // file use findViewById() to get the Button and textview.
        previous_button = findViewById(R.id.third_activity_previous_button)
        question3 = findViewById(R.id.question3_id)
  
        // In question1 get the TextView use by findViewById()
        // In TextView set question Answer for message
        question3.text = "Q3 - How to store heavy structured data in android? Ans - SQlite database ".trimIndent()
          
        // Add_button add clicklistener
        previous_button.setOnClickListener {
            // Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining
            // the components you are targeting. Intent to start an activity called SecondActivity with the following code:
            val intent = Intent(this, SecondActivity::class.java)
            // start the activity connect to the specified class
            startActivity(intent)
        }
    }
}


Output:



Previous Article
Next Article

Similar Reads

Android | Creating a Splash Screen
A splash screen is mostly the first screen of the app when it is opened. It is a constant screen which appears for a specific amount of time, generally shows for the first time when the app is launched. The Splash screen is used to display some basic introductory information such as the company logo, content, etc just before the app loads completel
2 min read
How to Capture a Screenshot and Screen Recording of an Android Device Using Android Studio?
Android Studio is the official IDE (Integrated Development Environment) for Android app development and it is based on JetBrains’ IntelliJ IDEA software. Android Studio provides many excellent features that enhance productivity when building Android apps. Whenever we are developing an app and in that device, you don't know how to capture screenshot
2 min read
Creating a Anagram Checker Android App in Android Studio with Kotlin
An anagram of a word is another word that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other. In this article, we will be building an Anagram Checker android app in Android Studio using Kotlin and XML. The app will check whether the entered words are anagrams of e
3 min read
Batch Import Images for Different Screen Resolution in Android Studio
While Developing an App, we require many images as an icon in our app. Here we are going to explain how Import Images for Different Screen resolutions in Android Studio using Batch Drawable. For Android, you need to create these graphics that have to be compatible and supporting all devices. Step By Step Implementation Step 1: First click on File
1 min read
How to Get Screen Dimensions as Pixel in Android?
Many times while building an android application we want to place elements dynamically within our android application. We can place these elements by specifying their position relative to the screen dimensions within the android application. For this, we will need to get device screen dimensions. In this article, we will take a look at How to get s
3 min read
How to Get Screen Width and Height in Android using Jetpack Compose?
Android applications are being developed for different device orientations to support a huge range of devices. So that users with different device size configurations can use the application. Many applications need to get the height and width of the device screen to create UI. In this article, we will take a look at How to get Screen Width and Heig
5 min read
Correct Way to Add a Splash Screen in Android
Google previously advised against utilizing a Splash Screen for Android Applications. It was pointless. Then, when they released the Material Design Specifications, several of us noticed that the Splash Screen had been renamed Launch Screen. Ideal for displaying your brand. So, what is the distinction between a terrible LaunchScreen and a good Laun
3 min read
How to scale different Views to all screen sizes in Android Studio?
In this article, it is shown how to change the size of View in Android App Development (like TextView, etc), so that they can modify the content that is displayed on the screen. Note: For this article, XML visualizer instead of Android Studio. Below are the various methods to change the size of Views in Android: Hard-coding the values in dp (densit
4 min read
Screen Orientations in Android with Examples
Screen Orientation, also known as screen rotation, is the attribute of activity element in android. When screen orientation change from one state to other, it is also known as configuration change. States of Screen orientation There are various possible screen orientation states for any android application, such as: ActivityInfo.SCREEN_ORIENTATION_
3 min read
Locking Screen Orientation in Android
If there is a scenario where you need your entire application to be in portrait mode or landscape mode or one of the Activity should be in portrait mode and all the other activities should be set at auto screen orientation, then here this article makes you do that in simple steps. So in this article, we are going to learn the following two things.
4 min read
How to Keep the Device Screen On in Android?
In Android it's seen that screen timeout will be set for 30 seconds or it is set by the user manually in system settings, to avoid battery drain. But there are cases where applications like stopwatch, document reader applications, games, etc, need the screen to be always awake. In this article its been demonstrated, how to keep the device screen aw
4 min read
How to Change the Screen Orientation Programmatically using a Button in Android?
Generally, the screen orientation of any application is Portrait styled. But when it comes to gaming or any other multimedia service such as watching a video, the screen orientation must change functionally from Portrait to landscape or vice-versa when the functionality is not required. So a developer has to explicitly write a program where the ori
3 min read
How to Align Navigation Drawer and its Elements towards the Left or Right of the Screen in Android?
The Navigation Drawer is a layout that can be seen in certain applications, that consists of some other activity shortcuts (Intents). This drawer generally can be seen at the left edge of the screen, which is by default. A Button to access the Navigation Drawer is by default provided at the action bar. So now the question is why is it is necessary
3 min read
How to Detect Touch Event on Screen Programmatically in Android?
Detecting a touch confirms that the screen is fully functional. Responding to touch is something that a developer deals with. As Android devices have a touch-based input, things are programmed upon application of touch. For explicitly calling methods within the application, a touch action must be recognized. Such methods can have special functions.
5 min read
How to Find the Screen Resolution of a Device Programmatically in Android?
Screen Resolution refers to the number of pixels on display. A higher resolution means more pixels and more pixels provide the ability to display more visual information. This entity is widely used in applications related to the broadcasting of real-time visuals such as live video, gaming, etc for optimization and frame conversions. The same inform
3 min read
Sliding Up Screen in Android
Sliding Up Screen is another common feature that we see in most of the apps. This Sliding Up Screen can be used for displaying some content after clicking the button or to display Menu. Using this Sliding Up Screen in our App improves the User Experience. In this article, we are going to see how to implement the Sliding Up Screen in Android. A samp
3 min read
How to Implement Preferences Settings Screen in Android?
In many apps, we have seen the Settings screen which is most common in most of the apps. This settings screen is used to manage the preferences of the users. For creating this settings screen android provides a feature to make a settings preferences screen. In this article, we will take a look at implementing the preferences setting screen in Andro
7 min read
How to Prevent Screenshot Or Screen Recorder in Android?
In some situations, we don't want to allow to take screenshots or screen recordings of our android application. Here we are going to explain how to prevent Android from taking a screenshot or screen recording when the app goes to the background. Generally, when we take a screenshot, we will see a Screen Capture notification in the notification bar
3 min read
Screen Mirroring of Android Smartphone using Raspberry Pi
Raspberry Pi consists series of small single-board computers. It was developed in the United Kingdom by the Raspberry Pi foundation in association with Broadcom. The first Raspberry Pi model was launched in 2012 and by the time it became popular because of its wide applications in many fields like many used Raspberry Pi to learn programming skills,
4 min read
How to Create a Splash Screen With Layer-List in Android?
Splash Screen is an initial screen that gets displayed on the app's launch. So to either display your logo or to display your app's name on the startup of the screen. Similar to what WhatsApp or Instagram apps do on their launch. They display a simple logo of their app. Now, here comes the problem, To do this you make a launcher activity, and in th
3 min read
How to Enable Full-Screen Mode in Android?
In this article, we are going to learn how to enable full screen in Android Studio. Like if we are especially viewing an image then we want to view that on full screen. Here what we are going to implement is that we are basically hiding the navigation, status bar, and enabling the full-screen mode. We are going to discuss 4 different methods for do
1 min read
Dynamic Splash Screen in Android
A Dynamic Splash screen is a more personalized image or graphic that is displayed when an application is loaded or launched. Dynamic Splash Screen consists of animation or moving graphics. It appears for a fraction of a second. It creates a sense of anticipation or excitement for the user and helps organizations with branding and establishing ident
3 min read
How to Create an Onboarding Screen in Android?
Hello geeks, today we are going to learn that how we can add Onboarding Screen to our android application in the android studio so that we can provide a better user experience to the user of the application. What is Onboarding Screen? The onboarding screen can be understood as a virtual unboxing of an application. Users go through a series of scree
4 min read
How to Create Splash Screen Without an Extra Activity in Android?
A splash screen is the first screen of the app when it is opened. It is used to display some basic introductory information such as the company logo, content, etc just before the app loads completely. In this article, we will follow the best practice to create a Splash screen that is without creating an extra activity for it and it will be gone as
3 min read
Animated Splash Screen in Android Using Jetpack Compose
Jetpack Compose is Android's advanced toolkit for creating materialistic UI in a very simpler form. It does not require any kind of XML files in Android Studio also it helps to create native applications as well. It is recently launched in the Android Studio Arctic Fox version. Jetpack Compose Functions are declared as: @Composable fun MessageCard(
4 min read
Splash Screen API in Android 12
Android 12 introduces the SplashScreen API, which allows all apps to use a new app to start an animation. When you or the user starts the application it fires up. The new experience incorporates common design components into every app launch, but it is also configurable, allowing your app to retain its distinct branding. You may use the SplashScree
4 min read
How to Change Alert Dialog Position on Screen in Android?
AlertDialog in Android is an alert message that appears in the form of a pop-up consisting of four elements namely a title, a message, a positive button, and a negative button. The positive and the negative buttons are clickable and can be programmed for performing an action. However, the AlertDialog appears in the center of the screen whenever cal
2 min read
How to Find Dots-Per-Inch (DPI) of Screen in Android Programmatically?
Dots-Per-Inch or DPI is a measure of pixel density over the physical area on the screen. A pixel is the smallest unit of any screen display. and the sum of all the pixels present on the screen is termed as Screen Resolution. The pixels available to the user are called Viewport and in this article, we will show you how you could fetch the DPI of the
2 min read
How to Get Touch Coordinates of Screen in Android?
In Android, the app screen is a layout that can be used to display various UI elements like TextView, Button, ImageView, etc. These UI elements are positioned according to a set of pre-defined rules and developer changes. In simple words, each bit on any app screen defines a coordinate that can be used to track UI elements as well as user touch on
3 min read
Detect Screen Orientation in Android using Jetpack Compose
In Android, Screen Orientation is an important aspect where the user would like to run an activity in Fullscreen or a Wide Landscaped mode. Most commonly running applications that can switch between portrait and landscape mode can be Image Viewers, Video Players, Web Browsers, etc. In such applications, it is important to detect screen orientation
2 min read
Article Tags :
Practice Tags :