My DialogFragment, PhotoCloseupFragment, will always send in 0 for measuredView.width and measuredView.height unless I use dp values for my ImageView layout_width and layout_height.
PhotoCloseupFragment:
class PhotoCloseupFragment : DialogFragment() {
private val args: PhotoCloseupFragmentArgs by navArgs()
private var _binding: FragmentPhotoCloseupFragmentBinding? = null
private val binding
get() = checkNotNull(_binding) {
"Cannot access binding because it is null. Is the view visible?"
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding =
FragmentPhotoCloseupFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.closeup.apply {
doOnLayout { measuredView ->
val scaledBitmap = getScaledBitmap(
args.photoFilePath,
measuredView.width, // ALWAYS 0 ------
measuredView.height // ALWAYS 0 ------
)
setImageBitmap(scaledBitmap)
tag = args.photoFilePath
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ImageView
android:id="@+id/closeup"
android:layout_width="250dp" <!--breaks if match_parent, wrap_content, or 0dp-->
android:layout_height="250dp" <!--breaks if match_parent, wrap_content, or 0dp-->
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:importantForAccessibility="no" />
</FrameLayout>
With wrap_content: