Home > Software engineering >  Android DataBinding new button setOnClickListener
Android DataBinding new button setOnClickListener

Time:03-26

I need to use the Android Camera. Therefore I am using following Template: https://github.com/android/camera-samples/tree/main/Camera2SlowMotion

I added a Butten to the fragment_camera.xml https://github.com/android/camera-samples/blob/main/Camera2SlowMotion/app/src/main/res/layout/fragment_camera.xml

<Button
    android:id="@ id/change_focus_button"
    android:layout_width="115dp"
    android:layout_height="81dp"
    android:text="@string/focusButtonText"
    tools:layout_editor_absoluteX="13dp"
    tools:layout_editor_absoluteY="473dp" />

In the CameraFragment.kt I now want to call a function when the button is clicked. In my understanding I therefore need to create a setOnClickListener.

https://github.com/android/camera-samples/blob/main/Camera2SlowMotion/app/src/main/java/com/example/android/camera2/slowmo/fragments/CameraFragment.kt

I thought about adding following line in Line 309. that would call a function i then create. But the "." after fragmentCameraBinding.changeFocusButton is always red and won't go away.

fragmentCameraBinding.changeFocusButton.setOnClickListener { chnageFocuslen() } 

What do I need to change / where do I need to add the OnClickListener?

Thank you very much in advance!

CodePudding user response:

I had to download the sample because I couldn't see what the issue was from the snippets in your post.

The issue is that CameraFragment has two layout files, one in the default folder layout, and one in layout-land for landscape orientation. So you need to add your change_focus_button XML to both layout files to make it not nullable when you access it via fragmentCameraBinding in CameraFragment.

  • Related