Junaid Khan
Junaid Khan

Reputation: 315

On Click implementation not working in Kotlin

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.

Upvotes: 1

Views: 1187

Answers (1)

iknow
iknow

Reputation: 9852

If You want to set onClick to specific View in the included layout use this in onCreate:

// Apply click listener to one view in the included layout
val includedLayoutButton: View = findViewById<View>(R.id.includedLayout).findViewById(R.id.butInIncludedLayout)

includedLayoutButton.setOnClickListener {
    Log.d("in", "on click")
}

main.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/includedLayout"
        layout="@layout/layout_test" />

</androidx.constraintlayout.widget.ConstraintLayout>

layout_test.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/butInIncludedLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</FrameLayout>

Upvotes: 1

Related Questions