skip to Main Content

Using Kotlin in Android Studio, I created a form where user can fill in necessary details AND click an imageButton to launch the camera, take picture and submit the form. I want to add a validation where the user is prevented from submitting the form if they did not take a photo.

I have tried to validate by using imageButton.drawable == null but it did not display the error toast.

Here are the relevant parts of my codes:

class FormActivity : AppCompatActivity() {

    var selectedPhotoUri : Uri? = null

    companion object {
        const val REQUEST_FROM_CAMERA = 1001
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_artactivity)

        ...

        val imageButton = findViewById<ImageButton>(R.id.imageButton)

        // launch camera
        imageButton.setOnClickListener {
            takePhotoUsingCamera()
        }

        val submitButton = findViewById<Button>(R.id.submitButton)

        submitButton.setOnClickListener {
            submitForm(userId.toString(), HRWAnswer, ResultAnswer)
        }
    }

    private fun takePhotoUsingCamera(){
        ImagePicker.with(this).cameraOnly()
            .crop()
            .start(REQUEST_FROM_CAMERA)
    }

    // to access the image captured
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode){
                REQUEST_FROM_CAMERA -> {
                    selectedPhotoUri = data!!.data
                    val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedPhotoUri)
                    val bitmapDrawable = BitmapDrawable(bitmap)

                    val imageButton = findViewById<ImageButton>(R.id.imageButton)
                    imageButton.setImageDrawable(bitmapDrawable)
                }
            }
        }
    }

    fun submitForm(userId : String, TestOption: String, ResultOption: String){

        ...

        val imageButton = findViewById<ImageButton>(R.id.imageButton)

        if (imageButton.drawable == null) {
            Toast.makeText(this, "Image of your Test result is required!", Toast.LENGTH_LONG).show()
            imageButton.requestFocus()
        }

        ...
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    as stated by Mike M. in the comment above, it will never be null due to its drawable set layout. i have changed my approach to achieve my target outcome.


  2. Check this may it helps you.

    class FormActivity : AppCompatActivity() {
         lateinit var imageButton : ImageButton//HERE YOU NEED TO ADD BUTTON.Dont add buttons initialization multiple time in once class.
            var selectedPhotoUri : Uri? = null
        
            companion object {
                const val REQUEST_FROM_CAMERA = 1001
            }
        
            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                setContentView(R.layout.activity_artactivity)
        
                ...
        
                imageButton = findViewById<ImageButton>(R.id.imageButton)
        
                // launch camera
                imageButton.setOnClickListener {
                    takePhotoUsingCamera()
                }
        
                val submitButton = findViewById<Button>(R.id.submitButton)
        
                submitButton.setOnClickListener {
                    submitForm(userId.toString(), HRWAnswer, ResultAnswer)
                }
            }
        
            private fun takePhotoUsingCamera(){
                ImagePicker.with(this).cameraOnly()
                    .crop()
                    .start(REQUEST_FROM_CAMERA)
            }
        
            // to access the image captured
            override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
                super.onActivityResult(requestCode, resultCode, data)
                if (resultCode == RESULT_OK) {
                    when (requestCode){
                        REQUEST_FROM_CAMERA -> {
                            selectedPhotoUri = data!!.data
                            val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedPhotoUri)
                            val bitmapDrawable = BitmapDrawable(bitmap)
        
                            val imageButton = findViewById<ImageButton>(R.id.imageButton)
                            imageButton.setImageDrawable(bitmapDrawable)
                        }
                    }
                }
            }
        
            fun submitForm(userId : String, TestOption: String, ResultOption: String){
        
                ...
        
              
        
                if (imageButton.drawable == null) {
                    Toast.makeText(this, "Image of your Test result is required!", Toast.LENGTH_LONG).show()
                    imageButton.requestFocus()
                }
        
                ...
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search