Home > Back-end >  How to import a button to add a listener in Kotlin?
How to import a button to add a listener in Kotlin?

Time:12-10

I know this is a pretty simple question but idk how to do it:

I have this button:

 <Button
        android:id="@ id/btnGo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_gravity="center" />

I want to add a listener in my main class, but I can't import the button, how to do it?

CodePudding user response:

Button b = findViewById(R.id.btnGo)
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // insert code here
    }
});

CodePudding user response:

Import

   import android.view.View;
   import android.widget.Button;

Java

Button button = findViewById(R.id.btnGo);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // code is here
            }
        });

Kotlin

 val button: Button = findViewById(R.id.btnGo)
        button.setOnClickListener {
            // code is here
        }
  • Related