loading...

How to start another activity on button click using Kotlin

carolmusyoka profile image Carol Musyoka ・2 min read

In this tutorial you will learn how to start another activity when a button is clicked. We will begin from scratch(starting a new android studio project)

Start a new Android Studio Project

On the activity_main.xml add a textView and a button. Here is is what you can use

<TextView
    android:id="@+id/textView"
    android:layout_width="292dp"
    android:layout_height="63dp"
    android:layout_marginEnd="60dp"
    android:text="Click on the button to open another activity!"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.446"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.459" />

<Button
    android:id="@+id/button"
    android:layout_width="159dp"
    android:layout_height="63dp"
    android:text="Click Me!!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:layout_constraintVertical_bias="0.197" />

Create a new second activity

app>>java>>new>>activity>>Empty activity

From the second activity you can add an imageView and textView

You now have two activities, the main activity and the second activity. Now all we have to do is to add functionality to the button in the (MainActivity.kt)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val button = findViewById<Button>(R.id.button)
        button.setOnClickListener{
            val intent = Intent(this, second::class.java)
            startActivity(intent)
        }
}

Now run your app on your emulator or hardware device

You will get something like this

Check out the Source Code on GitHub

That’s a wrap for this post. We have learnt how to start another activity on button click for our app’s first time users.

My ‘images’ don’t quite match with the ones you see on the Drive app 😄. But I’m sure you can come up with something better! Drop ’em in the comments below.

Posted on by:

carolmusyoka profile

Carol Musyoka

@carolmusyoka

ANDROID DEV. OPEN-SOURCE ENTHUSIAST.

Discussion

pic
Editor guide
 

Hi, Carol thank you for your post. i have done the steps and i have a button on my main page taking me to the second activity. but i am trying to make a second button go to a third activity following these steps but changing button id but it wont do anything when testing. i dont know how to proceed. please help.

 

Hi. Can you please send me a link to that project on github?

 

Can you please send me a link to that project on github??

 

😐Hello, I don't seem to have it. Is there any other way I can help?