Sitemap
Ted James

Ted James

The world is my office, and every destination is my inspiration

Cannot assign onClick to a button in kotlin

2 min readSep 13, 2024

I am working on an android studio project which is partially built in kotlin and the rest in in java. I have never worked with kotlin before this project and currently I have found myself spending hours trying to implement a simple onclick to a button. I know this question has been asked a lot but most of the solutions I have tried do not work.

here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/folioPageFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
android:id="@+id/webViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/indicatorLayout">

<com.folioreader.ui.view.WebViewPager
android:id="@+id/webViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<com.folioreader.ui.view.FolioWebView
android:id="@+id/folioWebView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>

<com.folioreader.ui.view.LoadingView
android:id="@+id/loadingView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:maxVisibleDuration="10000"
tools:visibility="invisible" >
</com.folioreader.ui.view.LoadingView>

<com.folioreader.ui.view.VerticalSeekbar
android:id="@+id/scrollSeekbar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@+id/indicatorLayout"
android:layout_alignParentRight="true"
android:layout_marginRight="2dp"
android:animateLayoutChanges="true"
android:thumb="@drawable/thumb"
android:visibility="invisible" />

<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" tools:layout_editor_absoluteY="655dp"
tools:layout_editor_absoluteX="161dp"
android:id="@+id/buttonVasko"
android:layout_marginTop="530dp"
android:layout_marginLeft="150dp"

android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"

/>
<LinearLayout
android:id="@+id/indicatorLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:visibility="gone">

<TextView
android:id="@+id/minutesLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#888888"
android:textSize="7sp" />

<TextView
android:id="@+id/pagesLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#bbbbbb"
android:textSize="8sp" />
</LinearLayout>

this is the beginning of the onCreateView function:

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?
): View? {

buttonVasko.setOnClickListener(object:View.OnClickListener {
override fun onClick(view:View) {
Toast.makeText(context,"WORK!!!", Toast.LENGTH_LONG).show()
}// end onClick
})

this is the error I receive when running the code:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.folioreader.android.sample, PID: 3862
java.lang.IllegalStateException: Could not find method zoko(View) in a
parent or ancestor Context for android:onClick attribute defined on view
class androidx.appcompat.widget.AppCompatButton with id 'buttonVasko'

I also tried placing the setOnClickListener at the very end in the onCreateView function in case there was a line of code that needed to be written before the setOnClickListener but the result is the same.

Get Ted James’s stories in your inbox

Join Medium for free to get updates from this writer.

Full FolioPageFragment is here: https://codeshare.io/5PlwDw

Solution

You’ve already inflated your fragment as mRootView. You can set your onClick listener in onViewCreated. Here is the code

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.buttonVasko).setOnClickListener {

}
}

And you should delete these static view imports.

import kotlinx.android.synthetic.main.folio_page_fragment.*
import kotlinx.android.synthetic.main.folio_page_fragment.view.*

Hope it will work.

Answered By — Ensar Bayhan

Answer Checked By — Senaida (FixIt Volunteer)

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

creative-commons-by-21pxcreative-commons-sa-21px

--

--

Ted James
Ted James

Written by Ted James

The world is my office, and every destination is my inspiration