Enobong Ndedde
4 min readJul 17, 2020

--

Creating an onClick listener in Kotlin

Hello there,

are you a beginner in android development and trying to move from one activity to another or making a simple toast when button is clicked?

this article or write up as most people call it will basically help to enhance your knowledge as a beginner in android development.

previously, while i was still a beginner i had an issue with this and mostly if you are a kind of person that struggles understanding videos then you should probably look into this, most videos do not go deeply into the code while explaining (Yes) it actually happened me once on a video in was following in youtube and as a result of that, in got stucked at a particular point until i got to realise where the error was coming from. so basically these are some of the things that this article will help you overcome. Okay no much biting around the bush. Let’s dive into the main aspect of this article.

So over here the first thing you probably need to do is to download your android studio as this will help u in the structuring and design of your application. You can type https://developer.android.com/studio on your browser to redirect you to the download page

This is how your download page will look like

Now after that is done you can now proceed to installing it. Then you can now create a new activity basically, for beginners i will recommend you use the empty activity just to go straight into what you want to do and having a less bug or probably confusing yourself.

Just for clarification purposes this is how it will look like after installation.
Now you can create an empty activity and move on to the next step

After this is done, android studio builds your app. Now after this, two screens become available for you to access the “MainActivity.kt” and “activity_main.xml. Now the “.kt” file helps you to wright the java code for your application programmatically and the xml helps you in the structuring of and design of your app. Now move ahead and click on the xml file the first thing u will see is the layout mostly for new android studios it will be a constraint layout, the second thing you will see it the default textview “Hello World” now note that this textview is a type of (widget) which we will explain in the next lesson. Now you can either choose to allow it or delete it, and then add a button that will help you to move from the current activity that you are to the second activity. Note if you are still using constraint layout, you will have to constrain your view to (Top,left,right, and bottom). as a short way of doing this, you give android studio a chance to auto complete what you are typing by using “cttt, cbtb, cltl, crtr” its as simple as that.

This is while you are still using constraint layout.

sorry please give your an id as “button” it will be written as

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="textview"
app:layout_constraintEnd_toEndOf="@id/parent"
app:layout_constraintLeft_toLeftOf="@id/parent"
android:layout_marginTop="10dp"
android:text="Click me"/>

Now after this all you need to do is make the button clickable. now there are two ways of accomplishing this bit here i would tell you about the programmatic aspect of it, but before you do this, go to your Project>app>”Right click you will see New” New>Activity>EmptyActivity. You can now give your activity a name that you want and click finish, your project will build and the same screen as before when you created android project pops up. Now all you need to do is go to the mainactivity.kt file, there you will see a default line of code which i will explain later in another article. now under the “setContentView(R.layout.activity_example)” write this

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

Now that is all for that. Run your app and test it, you can play with this by creating other activities. I hope this was helpful. Thanks

If you encounter any error you can dm me @EnobongNdedde

--

--