Open a New Activity Using a Button.

Pamela Chemutai
3 min readNov 17, 2021
Photo by NeONBRAND on Unsplash

To begin, we are going to add two layout files to our project, one activity_main.xml and the other activity_main2.xml, our aim is to open the activity_main2.xml using a button or a text in the first activity (activity_main.xml).

Using a Button

1. Res file(.xml)

First lets work with the res/layout file;

  • Add a button in the first_activity.xml.
  • You can check out the code below for the first activity;
<?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">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="340dp"
android:text="@string/btnText"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • Here’s the outcome; To Note; make sure to add an id.
https://github.com/Chemutaiselim
  • Lets also create the second_activity.xml
  • You can use the code below;
<?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=".MainActivity2">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Text"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • Here’s the outcome;
https://github.com/Chemutaiselim
  • That’s it for the res/xml file.

2. Kotlin Code (.kt file)

In the MainActivity.kt we are going to add the following in the class MainActivity;

  1. findViewById Method
  2. setOnClickLitsener
  3. Intent Constructor
  4. StartActivity Method

1. findViewById

As the name suggests findViewById method literally enables us to find a view using the views’ id. We normally use a syntax as the one below.

val button = findViewById<View>(R.id.IDofView)/*
View is the name of the 'widget' used in the res/layout file
in our case it will be Button
IDofView is the id of the widget we set in the res/layout file in our case it will be what we had set earlier, button.
*/

2. setOnClickLitsener

This method is used to specify the action to be taken when the button is clicked.

button.setOnClickListener{

//Action to be done

}

3. Intent Constructor

Intent has lot of uses which includes Launching an Activity. It takes in two parameters;

a) This: This is a package context, context enables us to access basic application functions e.g. resources.

b)Class: In our case the class parameter is used to specify the activiy we are goin to start.

val intent = Intent(this, ActivityToStart::class.java)//ActivityToStart in our case will be replaced with MainActivity2

4. startActivity

This is the method is used to start an instance of the activity required to be started, in our case MainActivity.

startActivity(intent)//or instead of using a variable, we can just  startActivity(Intent(this, ActivityToStart::class.java))

So combining the whole code;

--

--