Hello Everyone,
In this blog, we are creating an android application which will allow the user to go from one activity to another using a simple button widget. Here we are going to use android components like activity, button widgets and intents. In this application we will use two activities- MainActivity and SecondActivity.
Follow the steps given below to create an android project with two activities.
1) Open Android Studio and select New Project. Select an Empty Activity. Thus your first activity (MainActivity) is created.
2) Once the new project is created, go to File -- New -- Activity -- Empty Activity.
3) Rename the activity as "SecondActivity". Also select the option to generate a Layout file. Do not change the other settings. Now, your second activity is also created.
4) Copy-paste the code given below in the respective files. Then build and run the application.
MainActivity.java
package com.irrationalsapiens.demo3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btnNextActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNextActivity = findViewById(R.id.btnGoToNextActivity);
btnNextActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent (MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="179dp"
android:layout_height="43dp"
android:text="FIRST ACTIVITY"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.129" />
<Button
android:id="@+id/btnGoToNextActivity"
android:layout_width="252dp"
android:layout_height="77dp"
android:text="Go To Next Activity"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.444" />
</androidx.constraintlayout.widget.ConstraintLayout>
SecondActivity.java
package com.irrationalsapiens.demo3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
Button btnPrevActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btnPrevActivity = findViewById(R.id.btnGoToPrevActivity);
btnPrevActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent (SecondActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="275dp"
android:layout_height="53dp"
android:text="SECOND ACTIVITY"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.129" />
<Button
android:id="@+id/btnGoToPrevActivity"
android:layout_width="252dp"
android:layout_height="77dp"
android:textSize="20sp"
android:text="Go To Previous Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintVertical_bias="0.42" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output
This is the screenshot of the first activity (MainActivity). When you click on the button "GO TO NEXT ACTIVITY" , you will be redirected to the next activity.
This is the screenshot of the first activity (MainActivity). When you click on the button "GO TO NEXT ACTIVITY" , you will be redirected to the next activity.
When you click on the "GO TO NEXT ACTIVITY", you will be redirected to this activity. Now when you click on button "GO TO PREVIOUS ACTIVITY", you will be redirected to the first activity "MainActivity".
Check out "How to Create a List in Android using ListView".
Also learn more about the Android Architecture Stack.
Clearly explained in brief. Helpful! Thanks!
ReplyDelete