Creating Multiple Screen Applications in Android
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" ?>
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" />
< 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 {
TextView question1;
Button next_Activity_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_oneactivity);
next_Activity_button = (Button) findViewById(R.id.first_activity_button);
question1 = (TextView) findViewById(R.id.question1_id);
question1.setText( "Q1 - How to pass the data between activities in Android?\n" + "\n" + "Ans - Intent" );
next_Activity_button.setOnClickListener(v -> {
Intent intent = new Intent(Oneactivity. this , SecondActivity. 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() {
private lateinit var question1: TextView
private lateinit var next_Activity_button: Button
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_oneactivity)
next_Activity_button = findViewById(R.id.first_activity_button)
question1 = findViewById(R.id.question1_id)
question1.text = "Q1 - How to pass the data between activities in Android? Ans - Intent" .trimIndent()
next_Activity_button.setOnClickListener {
val intent = Intent( this , SecondActivity:: class .java)
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" ?>
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 {
TextView question2;
Button next_button, previous_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
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);
question2.setText( "Q 2 - What is ADB in android?\n" + "\n" + "Ans- Android Debug Bridge" );
next_button.setOnClickListener(v -> {
Intent intent = new Intent(SecondActivity. this , ThirdActivity. class );
startActivity(intent);
});
previous_button.setOnClickListener(v -> {
Intent intent = new Intent(SecondActivity. this , Oneactivity. 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() {
private lateinit var question2: TextView
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)
next_button = findViewById(R.id.second_activity_next_button)
previous_button = findViewById(R.id.second_activity_previous_button)
question2 = findViewById(R.id.question2_id)
question2.text = "Q2 - What is ADB in android? Ans - Android Debug Bridge" .trimIndent()
next_button.setOnClickListener {
val intent = Intent( this , ThirdActivity:: class .java)
startActivity(intent)
}
previous_button.setOnClickListener {
val intent = Intent( this , Oneactivity:: class .java)
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" ?>
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 {
TextView question3;
Button previous_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
previous_button = (Button) findViewById(R.id.third_activity_previous_button);
question3 = (TextView) findViewById(R.id.question3_id);
question3.setText( "Q 3 - How to store heavy structured data in android?\n" + "\n" + "Ans- SQlite database" );
previous_button.setOnClickListener(v -> {
Intent intent = new Intent(ThirdActivity. this , SecondActivity. 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() {
private lateinit var question3: TextView
private lateinit var previous_button: Button
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_third)
previous_button = findViewById(R.id.third_activity_previous_button)
question3 = findViewById(R.id.question3_id)
question3.text = "Q3 - How to store heavy structured data in android? Ans - SQlite database " .trimIndent()
previous_button.setOnClickListener {
val intent = Intent( this , SecondActivity:: class .java)
startActivity(intent)
}
}
}
|
Output:
Last Updated :
30 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...