skip to Main Content

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?

2

Answers


  1. Button b = findViewById(R.id.btnGo)
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // insert code here
        }
    });
    
    Login or Signup to reply.
  2. 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
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search