skip to Main Content

I am trying to make an application on android studio and I’m trying it for the first time. I can’t make any button clickable. I want to click a button and open another page. setonclicklistener is turning red when I write it. How can I make it work? I’m using Kotlin.

2

Answers


  1. If the whole setOnClickListener function has turned red in Android Studio, it means that you are trying to call it on a class/type that does not have that function, i.e. you are trying to call setOnClickListener on a class that does not extend View. Check the error message and check the class/type that you are trying to call setOnClickListener on and make sure you are calling it on a Button (or any component that is extending a View, or a View itself).

    If you are new to Android (Kotlin) programming, check some tutorials for sample applications to get a quick introduction to the concepts that are unique to the Android programming environment.

    Login or Signup to reply.
  2. In onCreateMethod you can simply write

    myButton.setOnClickListener{
    //your action
    }
    

    You can also check https://stackoverflow.com/a/34593645/15529296

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search