Multiple click listeners on buttons

15,547

Solution 1

First of all implement OnClickListener in your Activity, like

class MainActivity : Activity , OnClickListener

then override its implementation like

func onClick(v:View) {  
   //use when here like
   case R.id.youview -> {
   // do your work on click of view
    }

Don't forgot to set clicklistener on your View.

  yourView.setOnClickListener(this)

Or for better understanding go step by step -

  1. Implement OnClickListener in your Activity.

  2. Compiler will ask you to implement overrided methods. Implement those.

  3. Copy paste your java code which you wrote inside onClick method, that can be converted by kotlin itself or write down when conditions.

Solution 2

For multiple onClickListeners in kotlin (version:1.1.60), the following helped me. Hope it'll be useful to someone else too.

Implement OnClickListener to activity like:

class YourActivity : AppCompatActivity(), View.OnClickListener

set your button in onCreate():

val button = findViewById<Button>(R.id.buttonId)

and assign onclick to the button in onCreate():

button.setOnClickListener { onClick(button) }

and in the override method of onClick() :

 override fun onClick(v: View) {
    when (v.id) {
        R.id.buttonId -> { //your code  }
        ..
        ..
        ..
        else -> { //your code  }
   }
 }

Solution 3

Yes, in Kotlin you can do it like this:

view.setOnClickListener(object : View.OnClickListener {
    override fun onClick(v: View?) {
        when(v?.id) {
            R.id.imgBack -> {/* do your code */}
            R.id.twoButton -> {/* do your code */}
            R.id.threeButton -> {/* do your code */}
            else -> {/* do your code */}
        }
    }
}

Solution 4

You can try this following code:

class Testing:AppCompatActivity(), View.OnClickListener {
  private val mButton1:Button
  private val mButton2:Button
  protected fun onCreate(savedInstanceState:Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_testing)
    mButton1 = findViewById(R.id.btn_click) as Button
    mButton2 = findViewById(R.id.btn_click1) as Button
    mButton1.setOnClickListener(this)
    mButton2.setOnClickListener(this)
  }
  fun onClick(view:View) {
    when (view.getId()) {
      R.id.btn_click -> {
        Toast.makeText(this, "button 1", Toast.LENGTH_SHORT).show()
      }
      R.id.btn_click1 -> {
        Toast.makeText(this, "button 2", Toast.LENGTH_SHORT).show()
      }
      else -> {}
    }
  }
}

I hope this is help you.

Solution 5

This code worked for me:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    imgBack.setOnClickListener(this)
    twoButton.setOnClickListener(this)
    threeButton.setOnClickListener(this)
}

override fun onClick(view:View) {
    when (view.id) {
        R.id.imgBack -> {
            Toast.makeText(this, "imgBack", Toast.LENGTH_SHORT).show()
        }
        R.id.twoButton -> {
            Toast.makeText(this, "twoButton", Toast.LENGTH_SHORT).show()
        }
        else -> {}
    }
}

Don't forget implement View.OnClickListener in your class.

Share:
15,547

Related videos on Youtube

Anand Rajput
Author by

Anand Rajput

Updated on June 04, 2022

Comments

  • Anand Rajput
    Anand Rajput almost 2 years

    I want to know how to add multiple click events to buttons defined in XML, as previously, in Java, we implemented View.onClickListener interface and did the rest of the work in onClick method.
    Example:

    @Override
    public void onClick(View v) {
    
    switch (v.getId()) {
    
        case R.id.oneButton:
            // do your code
            break;
    
        case R.id.twoButton:
            // do your code
            break;
    
        case R.id.threeButton:
            // do your code
            break;
    
        default:
            break;
        }
    
    }
    

    I'm making a basic calculator app with the new Kotlin but it seems Kotlin has no such provisions, instead my code looks too long and verbose, as I'm attaching events to all buttons individually.
    Can someone tell me how to do the same way in Kotlin? Thanks

    • BakaWaii
      BakaWaii over 6 years
      You can try to copy this code to Android Studio and see what you get.
  • Anand Rajput
    Anand Rajput over 6 years
    I have tried but the view object can't be cast to Button. Can you post the full code where the view object was declared? Thanks
  • Arnold Brown
    Arnold Brown about 6 years
    Question asked for Kotlin and you gave the answer in Java.
  • SMBiggs
    SMBiggs over 3 years
    This seems the most "kotlinish," but the code won't compile. The when statement needs to handle the nullable with !!. or ?. In my case I chose to throw an exception, so that line should be when(v!!.id) {
  • Kharda
    Kharda over 3 years
    @SMBiggs oh yes, you're right. Just realize it. You can add a null checker for the v, like when(v?.id) {. I've updated the answer. Thanks!
  • Vivek Thummar
    Vivek Thummar over 3 years
    In java i'm declaring buttons like this : Button btn1, btn2;, how can i declare multiple buttons in single line in kotlin?
  • rockstar
    rockstar over 3 years
    Using this syntax is discouraged, thus some newer languages like Kotlin don't even allow them.
  • Vivek Thummar
    Vivek Thummar about 3 years
    in java we do something like - case R.id.oneButton: case R.id.twoButton: // do your code break; when we have two different buttons for same task, how can we achieve this in kotlin??
  • Vivek Thummar
    Vivek Thummar about 3 years
    ohk, i found the way to do the same in kotlin, case R.id.oneButton, case R.id.twoButton -> and it's done...