Please help: I want camerax preview use case to fi...
# compose
s
Please help: I want camerax preview use case to fill the whole size of the screen in compose, I am using a PreviewView in AndroidView and it is not filling the full screen, I tried creating same activity with xml (no compose) and is working as expected ? Or is there a direct way in compose to not use PreviewView() at all to show a preview use case full screen ? In xml I had it
match_parent
for height and width not sure how can achieve that here, it just put black screen top and bottom but preview size remains the same if I do 
Modifier.fillmaxSize()
j
You should probably set the layout params to get the expected behaviour
s
okay, but the question is which layout params should be used ? it is pure compose activity ?
tried
Copy code
LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
but no effect
j
Uhm I doubt that the Preview inherits from LinearLayout. Guessing here, but still pretty sure that would not even compile
How are you setting it? And is that all the setup you need for the preview? Doubt that too
s
well this is the setting of preview, used the same setting in
xml PeviewView
Copy code
private fun startCamera() {
    val cameraProviderFuture = ProcessCameraProvider.getInstance(this)

    cameraProviderFuture.addListener({
        // Used to bind the lifecycle of cameras to the lifecycle owner
        val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()

        // Preview
        val preview = Preview.Builder()
            .build()
            .also {
                it.setSurfaceProvider(previewView.surfaceProvider)
            }

        // Select back camera as a default
        val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

        try {
            // Unbind use cases before rebinding
            cameraProvider.unbindAll()

            // Bind use cases to camera
            cameraProvider.bindToLifecycle(
                this, cameraSelector, preview
            )

        } catch (exc: Exception) {
            Log.e(DEBUG_TAG, "Use case binding failed", exc)
        }

    }, ContextCompat.getMainExecutor(this))
}
n
You might try setting
previewView.implementationMode = PreviewView.ImplementationMode.COMPATIBLE
which seems to be more stable for me. I've put in a bug report for a similar problem: https://issuetracker.google.com/issues/179291080
1
I tried some of your code, mixed with a few of the things that I've tried.
This is the Android View layout I used.
c
mine looks like this ⬆️ i had issues with the ratio of the preview on some devices and
previewView.implementationMode = PreviewView.ImplementationMode.COMPATIBLE
fixed it (i don't remember why i didn't use the
update
lambda in the end 😅)
n
I think initially, the
update
lambda didn't exist. But, it's also going to restart the camera use cases, which might not be what you want.
s
Thanks @Neal Sanche @Cyril Find you guys saved the refactor back to xml.
😁 2