Home > front end >  What is wrong with this Button.onClickListener which seems that is not working
What is wrong with this Button.onClickListener which seems that is not working

Time:03-16

I am trying implement the basic example from here, and got this code:

class MainActivity : AppCompatActivity() {
    private lateinit var imageView: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = ActivityMainBinding.inflate(layoutInflater)
        imageView = binding.imageView
        binding.button1.setOnClickListener {
            this.dispatchTakePictureIntent()
        }
        setContentView(R.layout.activity_main)
    }

    private fun dispatchTakePictureIntent() {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        try {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
        } catch (e: ActivityNotFoundException) {
            println(e.stackTrace)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            val imageBitmap = data?.extras?.get("data") as Bitmap
            imageView.setImageBitmap(imageBitmap)
        }
    }

}

the code builds and I can deploy and run the app in my phone, but when I click in the button I set the click listener, nothing happens. Anyone can tell what's wrong here?

ps.: here the layout related to this activity, if needed:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@ id/imageView"
        android:layout_width="348dp"
        android:layout_height="177dp"
        android:layout_marginStart="38dp"
        android:layout_marginTop="151dp"
        android:layout_marginEnd="25dp"
        android:background="#0091EA"
        android:contentDescription="@string/todo"
        app:layout_constraintBottom_toTopOf="@ id/button4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@ id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="158dp"
        android:layout_marginTop="100dp"
        android:layout_marginEnd="151dp"
        android:layout_marginBottom="264dp"
        android:text="@string/take_pic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/button4" />

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

You have inflated a layout via View Binding, and the button in that layout is the one you added a click listener to.

However, you never used the layout that you inflated. Instead, you called

setContentView(R.layout.activity_main)

which inflates a new copy of the layout with no connection to the instance you inflated with View Binding. Replace this line with

setContentView(binding.root)

CodePudding user response:

Did you give the application permission to read from storage?

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  • Related