import React from 'react';
import { View, Text, StyleSheet, Dimensions } from 'react-native';
export default function ResponsiveBoxScreen() {
const screenWidth = Dimensions.get('window').width;
const boxSize = screenWidth * 0.5;
return (
<View style={styles.container}>
<View style={[styles.box, { width: boxSize, height: boxSize }]}>
<Text style={styles.boxText}>Responsive Box</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
},
box: {
backgroundColor: 'blue',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 8
},
boxText: {
color: 'white',
fontSize: 16,
fontWeight: 'bold'
}
});We use Dimensions.get('window').width to get the current screen width. Then we calculate boxSize as half of that width to keep the box square and responsive.
The box is styled with a blue background and centered text. We use flexbox properties justifyContent and alignItems to center the box and the text inside it.
This approach ensures the box size changes automatically on different screen sizes, making the UI responsive.