I am implementing on Click listener in Kotlin and it is not working. When i click on button nothing happens. Below is the code:
class MainActivity : AppCompatActivity(), View.OnClickListener{
var area: MaterialButton? = null; var length: MaterialButton? = null
var time: MaterialButton? = null;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
area = findViewById(R.id.button_area)
length = findViewById(R.id.button_length)
time = findViewById(R.id.button_time)
setClickListeners()
}
private fun setClickListeners() {
area?.setOnClickListener(this)
length?.setOnClickListener(this)
time?.setOnClickListener(this)
}
fun toggleDrawer(view: View) {
showToast("Drawer")
}
fun openSettings(view: View) {}
override fun onClick(v: View) {
when (v.id) {
R.id.button_area, R.id.button_length,
R.id.button_time -> showToast("Click")
else ->{
showToast("Drawer")
}
}
}
private fun showToast(str: String){
Toast.makeText(this,str,Toast.LENGTH_LONG).show()
}
}
XML onClick attribute is not working.
<include
layout="@layout/toolbar_content"/>
I have included layout with include property, in main activity(xml). Included view onClick methods are:
fun toggleDrawer(view: View) {
showToast("Drawer")
}
fun openSettings(view: View) {}
They are not working. Infact i am getting error. Could not find a method toggleDrawer(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class com.google.android.material.button.MaterialButton with id 'drawer_icon'. I have declared these methods in MaterialButton tag. The layout of this button is toolbar_content. How to resolve all these issues.