DEV Community

MinSoo Kim
MinSoo Kim

Posted on

An unfolded Galaxy Fold pushed my camera button off the screen. The fix was one function

I opened a camera screen I'd built on an unfolded Galaxy Z Fold5. The take-photo button wasn't there.

Neither was the "Photo 1 of 3" line above it. The preview had filled the screen and pushed both of them off the bottom.

The screen is simple. A title, a line of instructions, a 3:4 front-camera preview with two guide boxes drawn on top, then the counter and the button. On the Fold, with the inner screen open, it didn't fit.

What I got wrong

A screenshot showed the button was gone but gave me no numbers, so I pulled the view tree with uiautomator dump. The camera's SurfaceView ran from y=412 to y=2176, and there was no node for the button at all.

The mistake was mine. I wanted the preview to match a 3:4 photo, so I'd set its wrapper to { width: '100%', aspectRatio: 3 / 4 }. Width first, height follows. On the Fold that wrapper was 1686 pixels wide, so it asked for about 2248 pixels of height, which is more than the screen had left after the title and instructions. The SurfaceView bottom at 2176 was just where the screen ended.

The fix

A 3:4 box has two ways to be too big: too wide for the space, or too tall. Sizing it from the width only covers the first. On a narrow phone the width runs out first, and that case is fine. On an unfolded Fold the screen is wide and the height runs out first.

So the box needs the space it actually has, and then the largest 3:4 rectangle that fits inside it:

export function fitAspect(w: number, h: number, ratio = 3 / 4): { width: number; height: number } {
  if (w <= 0 || h <= 0) return { width: 0, height: 0 };
  const width = Math.min(w, h * ratio);
  return { width, height: width / ratio };
}
Enter fullscreen mode Exit fullscreen mode

h * ratio is how wide the box could be if it used the full height. Whichever is smaller, that or the real width, wins.

The space comes from onLayout on a wrapper that takes whatever is left after the text and the button:

const [area, setArea] = useState({ width: 0, height: 0 });

<View style={s.camArea} onLayout={(e) => setArea(e.nativeEvent.layout)}>
  <View style={[s.camWrap, fitAspect(area.width, area.height)]}>
    <CameraView ref={cam} style={StyleSheet.absoluteFill} facing="front" animateShutter={false} />
    <Guide box={SKIN_BOX} />
    <Guide box={PAPER_BOX} />
  </View>
</View>
Enter fullscreen mode Exit fullscreen mode
camArea: { flex: 1, marginTop: 16, alignItems: 'center', justifyContent: 'center' },
camWrap: { borderRadius: 20, overflow: 'hidden', backgroundColor: '#000' },
Enter fullscreen mode Exit fullscreen mode

camArea gets flex: 1, so it's the part that shrinks. The preview inside it never asks for more than that. The guide boxes are positioned as fractions of the preview, so they keep their place in the frame at any size.

Before the first layout pass the area is 0 by 0, and the function returns 0 by 0 instead of a box with a made-up size.

Checking it

Three unit tests: a narrow phone where width runs out (342 by 520 gives 342 by 456), a wide area where height runs out (the box stays inside the height and keeps 3:4), and the 0 case. The rest of the suite still passed, 23 tests in all.

Then the same Fold, unfolded. The preview came out at 930 by 1241 pixels, and the button, the counter and the instructions were all on screen. That run was layout only.

Later that day I took the three photos on the fixed build, and the result screen came up normally. I didn't note whether the phone was open for those.

Takeaway

Size an aspect-ratio box from the space it's given, not from the width alone.

My camera screen hasn't been on a narrow phone yet.

This post was drafted with AI help. The numbers and facts come from my own logs and repo.

Top comments (6)

Collapse
 
mosmox_studio profile image
Mosmox Studio •

Since the wrapper re-runs onLayout whenever the window size changes, this also covers folding and unfolding mid-session. One more case worth a quick check: split-screen. The window gets short there, so the height-first branch of fitAspect kicks in even on a regular phone.

Collapse
 
danorie profile image
MinSoo Kim •

I haven't tried split screen yet. I'll test it and let you know.

Collapse
 
fireflyteamapps profile image
Firefly Team •

Pulling real numbers with uiautomator dump instead of guessing from a screenshot is a good habit. One thing worth checking on the Fold: if the native preview fills its view with a center crop, guide boxes placed as fractions of the view only line up with the saved photo when the view matches the camera's output ratio exactly. Comparing one captured frame against the guide positions would confirm they still land in the same spot.

Collapse
 
danorie profile image
MinSoo Kim •

Thanks to you, I also handled cameras with a different aspect ratio. I haven't compared an actual photo against the guide yet.

Collapse
 
userxnom profile image
Abdullah Naseer •

This is the kind of bug that only shows up when a real device embarrasses your assumptions. Sizing from width alone is such a natural default — on a normal phone it works perfectly, which is exactly why it survives until a Fold or a tablet exposes it. The fix is elegant because it inverts the thinking: the space decides, and the preview fits inside it, instead of the preview demanding space.

The detail I'd steal is guarding the 0x0 area before the first layout pass — that's the part that would have bitten me in production after 'fixing' it. One question: have you tried it in split-screen or multi-window on the Fold, where the area shrinks again mid-session? That's where onLayout-driven sizing either proves itself or needs a second look.

Collapse
 
prikshit profile image
Prikshit •

Pulling the view tree with uiautomator dump to get real numbers is a step worth copying, because a screenshot only shows that something is missing. Split screen on the Fold could still bite, since the area left after the text can get very short and fitAspect will return a tiny preview that technically fits. Did you set a minimum height where the screen falls back to scrolling?