DEV Community

Carol Musyoka
Carol Musyoka

Posted on

How to start another activity on button click using Kotlin

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" />
Enter fullscreen mode Exit fullscreen mode

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)
        }
}
Enter fullscreen mode Exit fullscreen mode

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.

Oldest comments (5)

Collapse
 
ajmdve profile image
Alberto

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.

Collapse
 
carolmusyoka profile image
Carol Musyoka

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

Collapse
 
slimaneda profile image
slimaneda

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

Collapse
 
carolmusyoka profile image
Carol Musyoka

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

Collapse
 
imimbabel profile image
imimbabel

thanks for sharing.