androidkotlinandroid-galleryandroid-popupwindow

Why my imageView doesn't change with registerForActivityResult?


I want to make an app, where i click on button on bottomNavigationView and PopupWindow opens, i can choose image (thumbnail) and Video from my gallery, add description and title. Then add provided data to recyclerView. But i have a problem with picking image from gallery, it seems to work, i can pick image from gallery, but it doesn't change image inside popupWindow as it suposed to.

MainActivity.kt

class MainActivity : AppCompatActivity() {

private val home = HomeFragment()
private val player = PlayerFragment()
private val profile = ProfileFragment()
private val settings = SettingsFragment()



private val getPreviewImage = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
    val dialogView: View = layoutInflater.inflate(R.layout.popup, null)
    dialogView.imageChange.setImageURI(it)
})

private val getPreviewVideo = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
    val dialogView: View = layoutInflater.inflate(R.layout.popup, null)
    dialogView.VideoSelect.setImageURI(it)
})

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


    val popupButton: FloatingActionButton = findViewById(R.id.fab)
    val bottomNav: BottomNavigationView = findViewById(R.id.bottomNavigationView)

    bottomNav.background = null
    bottomNav.menu.findItem(R.id.placeholder).isEnabled = false
    replaceFragment(home)

    bottomNav.setOnItemSelectedListener {
        when (it.itemId) {
            R.id.home -> replaceFragment(home)
            R.id.player -> replaceFragment(player)
            R.id.profile -> replaceFragment(profile)
            R.id.settings -> replaceFragment(settings)
        }
        true
    }
    popupButton.setOnClickListener {
        showDialog()
    }

}
private fun replaceFragment(fragment: Fragment) {
    val transaction = supportFragmentManager.beginTransaction()
    transaction.replace(R.id.fragment_container, fragment)
    transaction.commit()
}

private fun showDialog() {

    val title = RecyclerAdapter().titles
    val description = RecyclerAdapter().details
    val video = RecyclerAdapter().videos

    val dialog = Dialog(this)
    val dialogView: View = layoutInflater.inflate(R.layout.popup, null)

    val imageView = dialogView.findViewById<ImageView>(R.id.imageChange)
    val videoView = dialogView.findViewById<ImageView>(R.id.VideoSelect)

    val buttonImage = dialogView.findViewById<Button>(R.id.addImage)

    val titleEditText = dialogView.findViewById<EditText>(R.id.titleEdit) //popUp edit field title
    val descEditText = dialogView.findViewById<EditText>(R.id.description) //popUp pole edit field description


    dialogView.addImage.setOnClickListener {
        getPreviewImage.launch("image/*")
    }

    dialogView.addVideo.setOnClickListener {
        getPreviewVideo.launch("video/*")
    }

    dialogView.addButton.setOnClickListener {
        if (titleEditText.text.isEmpty()){
            Toast.makeText(applicationContext, "add required data", Toast.LENGTH_SHORT).show()
        }else{
            Toast.makeText(applicationContext, "Added", Toast.LENGTH_SHORT).show()
        }
    }
    dialog.setContentView(dialogView)
    dialog.show()
  }
}

Solution

  • Try to use a global variable for the dialogView layout and check for the URI existence:

    class MainActivity : AppCompatActivity() {
    
        private val home = HomeFragment()
        private val player = PlayerFragment()
        private val profile = ProfileFragment()
        private val settings = SettingsFragment()
    
        private var dialogView: View? = null
        
        private val getPreviewImage = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
            it?.let { uri -> 
                dialogView?.findViewById<ImageView>(R.id.imageChange)?.setImageURI(it)
            } ?: run {
                Log.e("MainActivity", "URI not present")
            }
        })
        
        private val getPreviewVideo = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
            it?.let { uri -> 
                dialogView?.findViewById<ImageView>(R.id.VideoSelect)?.setImageURI(it)
            } ?: run {
                Log.e("MainActivity", "URI not present")
            }
        })
        
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
        
            val popupButton: FloatingActionButton = findViewById(R.id.fab)
            val bottomNav: BottomNavigationView = findViewById(R.id.bottomNavigationView)
        
            bottomNav.background = null
            bottomNav.menu.findItem(R.id.placeholder).isEnabled = false
            replaceFragment(home)
        
            bottomNav.setOnItemSelectedListener {
                when (it.itemId) {
                    R.id.home -> replaceFragment(home)
                    R.id.player -> replaceFragment(player)
                    R.id.profile -> replaceFragment(profile)
                    R.id.settings -> replaceFragment(settings)
                }
                true
            }
            popupButton.setOnClickListener {
                showDialog()
            }
        
        }
        private fun replaceFragment(fragment: Fragment) {
            val transaction = supportFragmentManager.beginTransaction()
            transaction.replace(R.id.fragment_container, fragment)
            transaction.commit()
        }
        
        private fun showDialog() {
        
            val title = RecyclerAdapter().titles
            val description = RecyclerAdapter().details
            val video = RecyclerAdapter().videos
        
            val dialog = Dialog(this)
            dialogView = layoutInflater.inflate(R.layout.popup, null)
        
            val buttonImage = dialogView?.findViewById<Button>(R.id.addImage)
        
            val titleEditText = dialogView?.findViewById<EditText>(R.id.titleEdit) //popUp edit field title
            val descEditText = dialogView?.findViewById<EditText>(R.id.description) //popUp pole edit field description
        
            dialogView?.addImage.setOnClickListener {
                getPreviewImage.launch("image/*")
            }
        
            dialogView?.addVideo.setOnClickListener {
                getPreviewVideo.launch("video/*")
            }
        
            dialogView?.addButton.setOnClickListener {
                if (titleEditText.text.isEmpty()){
                    Toast.makeText(applicationContext, "add required data", Toast.LENGTH_SHORT).show()
                }else{
                    Toast.makeText(applicationContext, "Added", Toast.LENGTH_SHORT).show()
                }
            }
    
            dialog.setContentView(dialogView)
            dialog.show()
          }
        }