I created a fragment Dialog file called it PhotoDialogFragment.kt
the aim of this file is to draw the dialog that has the photo saved in db, using the getScaledBitmap()
function created previously.
added an option to close the dialog also.
package com.bignerdranch.android.criminalintent
import android.app.Activity
import android.graphics.Bitmap
import android.graphics.Point
import android.os.Bundle
import android.provider.ContactsContract
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import androidx.fragment.app.DialogFragment
import java.io.File
import java.util.zip.Inflater
class PhotoDialogFragment(
val photofile : File
) : DialogFragment() {
private lateinit var imageButton2: ImageButton
private lateinit var imageView: ImageView
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.photo_dialog_fragment, container, false)
imageButton2 = view.findViewById(R.id.imageButton2) as ImageButton
imageView = view.findViewById(R.id.imageView) as ImageView
val image = getScaledBitmap(photofile.path, requireActivity())
imageView.setImageBitmap(image)
imageButton2.setOnClickListener {
dismiss()
}
return view
}
fun getScaledBitmap(path: String, activity: Activity): Bitmap {
val size = Point()
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
val display = activity.display
display?.getRealSize(size)
} else {
@Suppress("DEPRECATION")
activity.windowManager.defaultDisplay.getSize(size)
}
return getScaledBitmap(path, size.x, size.y)
}
}
here is the layout file of the fragmentdialog: photo_dialog_fragment.xml
<?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">
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.963"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.238"
android:foregroundGravity="right|top"
app:srcCompat="@android:drawable/ic_menu_close_clear_cancel" />
<ImageView
android:id="@+id/imageView"
android:layout_width="384dp"
android:layout_height="496dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
in the fragment_crime.xml
i converted the ImageView
that display the thumbnail of the photo to an ImageButton
so that i can use it in CrimeFragment.kt
<ImageButton
android:id="@+id/crime_photo"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@android:color/darker_gray"
android:cropToPadding="true"
android:scaleType="centerInside" />
Inside CrimeFragment.kt
I implemented the button and added the photoFile
as a parameter.
/**
* creating the imagebutton to start the fragmentdialog and display the photo
*/
crimePhoto.setOnClickListener {
val fragment = PhotoDialogFragment(photoFile)
fragment.show(childFragmentManager, "PhotoFragmentDialog")
}
There you go…