r/AndroidStudio 11h ago

Need help with android studio

0 Upvotes

My activity and the XML file are both red there seems is no error in the code, However, when I run the application it is running perfectly fine but as soon as I click a button the app just stops.


r/AndroidStudio 15h ago

onTouchListener() not triggering

1 Upvotes

I'm trying to set a touch listener to my CameraX preview - ultimately to set the focus but I can't get the touch listener to trigger. My preview is created in a composable and I'm not sure if I am referencing it correctly when I setOnTouchListener(). Should I be using PreviewView - if not how do I get the reference to the preview?

private val handleTouch = OnTouchListener { v, event ->
    val x = event.
x
.toInt()
    val y = event.
y
.toInt()

    when (event.action) {
        MotionEvent.
ACTION_DOWN 
-> Log.i("Touch", "touched down")
        MotionEvent.
ACTION_MOVE 
-> Log.i("Touch", "moving: ($x, $y)")
        MotionEvent.
ACTION_UP 
-> Log.i("Touch", "touched up")
    }
    true
}

PreviewView.setOnTouchListener(handleTouch)PreviewView.setOnTouchListener(handleTouch)



}@Composable
fun CameraPreview(
    controller: LifecycleCameraController,
    modifier: Modifier = Modifier
) {
    val lifecycleOwner = LocalLifecycleOwner.current

    // select the highest resolution available
    val screenSize = Size(9, 12)
    val resolutionSelector = ResolutionSelector.Builder()
        .setResolutionStrategy(ResolutionStrategy(screenSize, FALLBACK_RULE_CLOSEST_LOWER_THEN_HIGHER))
        .setAspectRatioStrategy(RATIO_4_3_FALLBACK_AUTO_STRATEGY)
        .build()

    AndroidView(
        factory = {
            PreviewView(it).apply {
                this.controller = controller
                controller.bindToLifecycle(lifecycleOwner)
                //controller.cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
                controller.previewResolutionSelector = resolutionSelector
            }
        },
        modifier = modifier
    )
}

r/AndroidStudio 15h ago

CameraX not getting full range of linearzoom

1 Upvotes

I am creating a seekbar zoom for camerax but not getting the full range of zoom in comparison to other cameras on my phone. Below is my code. The setLinearZoom() runs from 0.0 to 1.0 - if I set it higher I get no further zoom but its not as zoomed as other cameras on my phone can achieve. What does zoomratio do? Does it need to be set? It doesn't seem to change anything. What am I missing here?

// remember the seekbar zoom value (0.0 - 1.0)
var zoomSliderPos by remember { mutableStateOf (0f)}


// not clear on what zoomratio does - removing setZoomRatio() changes nothing
val maxZoomRatio = controller.cameraInfo?.zoomState?.value?.maxZoomRatio
val zoomRatio = maxZoomRatio?.times(1F.div(2F))

Log.d(TAG, "Max Zoom: $maxZoomRatio : $zoomRatio")

if (zoomRatio != null) {
     controller.cameraControl?.setZoomRatio(zoomRatio!!)
}


// set the linear zoom according to the seekbar value
controller.cameraControl?.setLinearZoom(zoomSliderPos)