Listeners with multiple button clicks
I want to know how to add multiple button click events defined in XML, just like before, in Java we have implemented the interface View.onClickListener
and done the rest of the work in the method onClick
.
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 building a basic calculator app with new Kotlin, but it seems like Kotlin doesn't have such clauses, instead my code looks too long and verbose as I attach events to all buttons individually.
Can someone tell me how to do the same in Kotlin? thank
source to share
First of all, OnClickListener
in your own Activity
, for example
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 forget to install clicklistener in your View
.
yourView.setOnClickListener(this)
Or for a better understanding, go step by step -
-
OnClickListener
in yourActivity
. -
The compiler will ask you to implement the overridden methods. Implement those.
-
Copy and paste your Java code that you wrote into a
onClick
method that can be converted by Kotlin himself, or by writing,when
conditions.
source to share
For multiple onClickListeners, the kotlin
(version:1.1.60)
following helped me. Hope it will be helpful to someone else as well.
OnClickListener
for actions such as:
class YourActivity : AppCompatActivity(), View.OnClickListener
set your button to onCreate()
:
val button = findViewById<Button>(R.id.buttonId)
and assign onclick
to button in onCreate()
:
button.setOnClickListener { onClick(button) }
and in the override method onClick()
:
override fun onClick(v: View) {
when (v.id) {
R.id.buttonId -> { //your code }
..
..
..
else -> { //your code }
}
}
source to share
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 */}
}
}
}
source to share
You can try the 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 -> {}
}
}
}
Hope this helps you.
source to share
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 to implement View.OnClickListener
in your class.
source to share
Try below.
public class MainActivity extends Activity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button xyz = (Button) findViewById(R.id.xyz);
xyz.setOnClickListener(this);
}
@Override
public void onClick(View v) {
}}
More details at https://androidacademic.blogspot.in/2016/12/multiple-buttons-onclicklistener-android.html
source to share