Home > Back-end >  Upload image to one of 3 separate ImageViews, after clicking one of 3 buttons
Upload image to one of 3 separate ImageViews, after clicking one of 3 buttons

Time:12-04

I have an activity where you can upload 3 different pictures after clicking one of the 3 different buttons. You can only choose images one by one, as opposed to choosing 3 in one go. Each "button" is actually a clickable ImageView, which when clicked should prompt the user to choose a picture and then the picture they select should populate the ImageView that they clicked. I know how to work with just 1 imageView per activity so I'm currently stuck.

My thought process is as follows:

  1. Have 3 different Boolean variables which are linked to 3 different clickable ImageViews.

     private var clickedView1: Boolean = false
     //same for the other views
    
  2. When 1 of the views is clicked(OnClickListeners), toggle the Boolean linked to it so that it's true.

     upload1.setOnClickListener {
         clickedView1 = true
         uploadPicture()
     }
     //same for the other views
    
  3. If the boolean updates to true, populate the corresponding imageView (done in the onActivityResult).

         if (clickedView1) {
             if (requestCode == 0 && resultCode == Activity.RESULT_OK && data != null) {
    
             selectedPhotoUri1 = data.data
             ImagesList?.plus(selectedPhotoUri1)
             val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedPhotoUri1)
             upload1.setImageBitmap(bitmap)
             }
         } else if { 
             //same thing for the other views
         }
    

In my head that makes sense, but I know it's wrong and when I program it and launch the activity, I have to be strategic RE the order I populate the image views (i.e. it only works from the last ImageView, going to the first). Any other order doesn't work and will always populate the first ImageView, no matter which is clicked.

Is there an efficient and accurate way of doing the population OnClickListeners?

CodePudding user response:

Most likely Android recreates your Activity while you're picking an image. You didn't provide the code of uploadPicture() method but I suppose that it opens image picker in another app. So, values of your variables may be lost, including clickedView1 and others. You can check it by putting a log in onActivityResult() which shows values of these variables.

If I'm right then one of possible solutions is to save your variables' values in onSaveInstanceState() and restore them in onCreate() like this:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    clickedView1 = savedInstanceState?.getBoolean(KEY_CLICKED_VIEW_1)
}

override fun onSaveInstanceState(outState: Bundle?) {
    outState?.putBoolean(KEY_CLICKED_VIEW_1, clickedView1)
    super.onSaveInstanceState(outState)
}

For future improvements you can also consider

  • Using one int instead of three Booleans to remember chosen ImageView
  • Using different request codes for different ImageViews when picking an image instead of saving state.

CodePudding user response:

This is the solution: (I can only mark it as correct in 2 days)

Following the explanation that I detailed in the question, the only thing that I did was to update the boolean variable for when an image view is not clicked to false, otherwise, whenever you click it once, it keeps the variable as true and I guess this causes the confusion as to which view should be populated.

  • Related