Picutre doesn't appears in the alert dialog box

122 Views Asked by At

I am using PhotoView to implement Pinch to Zoom feature. Fortunately enough, my app is able to fetch the picture from the given uri, but when it comes to display it, it only shows a transparent box.

image_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <com.github.chrisbanes.photoview.PhotoView
        android:id="@+id/zoom_iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Kotlin code to achieve the functionality:

 ... 
   binding.image.setOnClickListener(){
       ImageLayoutDialog()
   }
 ...
 ...
 ...
 private fun ImageLayoutDialog() {
        val imageLayoutBinding =
            ImageLayoutBinding.inflate(LayoutInflater.from(requireContext()))
        val photoView: PhotoView = imageDialogBinding.zoomIv
        val mBuilder = AlertDialog.Builder(requireContext())
            .setView(
                imageLayoutBinding .root
            )

        Glide.with(requireActivity())
            .load(customUri)
            .into(photoView)
        val mAlertDialog : AlertDialog =  mBuilder.create()
        mAlertDialog.show()
        mAlertDialog.setCanceledOnTouchOutside(false)
    }

When I click on image I get a transparent screen. But I am expecting the contents of customUri there. Any solution to this?

EDIT 1.0

Now the dialog box loads up. But it doesn't shows the image, after setting the view.

1

There are 1 best solutions below

0
MACROSystems On

In this case I would recommend you to use other type of Dialogs. Like a DialogFragment, or an AppCompatDialog.

Solution using an AppCompatDialog (which I think will be the most similar to your need):

class ImageDialog (private var context: Context, private var yourUri: Uri) : AppCompatDialog(context) {

private var _binding: DialogBinding? = null
private val binding get() = _binding!!

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

   val layoutInflater = LayoutInflater.from(context)
    _binding = DialogBinding.inflate(layoutInflater)

    setContentView(binding.root)

   //here you can load your Uri with Glide.

}

}

Instantiating the AppCompatDialog:

val imageDialog = ImageDialog(requireContext(), yourUri)
                    imageDialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
                    imageDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                    navigateToChangeUserAddressDialog.show()
            

I know is not an alert dialog, but I am trying to find a solution :D

You can always add personalized buttons to that dialog to dismiss it once its clicked with a imageDialog.cancel(), or you can also pass a lambda if you want to execute some logic in the view once its clicked.