Are Xiaomi phones experiencing trouble with dimensions in REACT-NATIVE?

I am currently developing a component that displays various icons for the user to choose colors. The component consists of a <View> containing a flatlist with the following code:

class Color extends Component {
    
    icons_config = {name: "square-full", size: 80, color: "black"}
    show_hide_ = false;
    options_array = [
        {option_icon: <FontAwesome5 name = {this.icons_config.name} size = {this.icons_config.size} color = "red"/>, key: "red"}, 
        {option_icon: <FontAwesome5 name = {this.icons_config.name} size={this.icons_config.size}  color = "blue"/>, key: "blue"}, 
        {option_icon: <FontAwesome5 name = {this.icons_config.name} size={this.icons_config.size}  color = "green"/>, key: "green"}, 
        {option_icon: <FontAwesome5 name = {this.icons_config.name} size={this.icons_config.size}  color = "yellow"/>, key: "yellow"},
        {option_icon: <FontAwesome5 name = {this.icons_config.name} size={this.icons_config.size}  color = "purple"/>, key: "purple"},
        {option_icon: <FontAwesome5 name = {this.icons_config.name} size={this.icons_config.size}  color = "black"/>, key: "rgba(0,0,0,0.7)"},
        {option_icon: <FontAwesome5 name = {this.icons_config.name} size={this.icons_config.size}  color = "gray"/>, key: "gray"},
        {option_icon: <FontAwesome5 name = {this.icons_config.name} size={this.icons_config.size}  color = "orange"/>, key: "orange"},
        {option_icon: <AntDesign name="closesquareo" size={80} color="black" />, key: ""} //x icon for delete color
    ]
    window_width = Dimensions.get("window").width
    window_height = Dimensions.get("window").height

    render() { 
        return (
            <>
                <View style = {this.styles.container}>
                    {this.options_array.map((option) => {
                        return (
                            <View key = {option.key} style = {this.styles.options}>
                                <TouchableOpacity style = {this.styles.touchable} onPress = {() => this.choose_color(option.key)}>
                                <Text>{option.option_icon}</Text>
                                </TouchableOpacity>
                            </View>
                        )
                    })}
                </View>
            </>
        )
    }

Here is how the stylesheet code looks like:

styles = StyleSheet.create({
    container: {
        position: "absolute",
        justifyContent: "space-around",
        width: Math.round(this.window_width) * 0.8, 
        height: Math.round(this.window_width) * 0.8,
        flexWrap: "wrap",
        flexDirection: "row",
        alignSelf: "center",
        backgroundColor: "red",
        marginTop: 200
    }, 
    options: {
        paddingTop: Math.round(this.window_width) * 0.025,
        justifyContent: "center"
    }
});

However, I encountered an issue with using

Dimensions.get("window").width
in the width and height properties to create a responsive square for all devices. It works fine on my one plus 7 pro and Huawei p9 plus, but not on my Xiaomi mi 9T. Here are some screenshots for comparison:

Xiaomi Mi 9T:

https://i.sstatic.net/2mtoj.png

One Plus 7 Pro:

https://i.sstatic.net/baaeh.png

Huawei P9 Plus:

https://i.sstatic.net/zCiCd.png

I suspect it might be related to the size of the icons, as there seems to be no problem on one plus and huawei devices. How can I resolve this issue?

Answer №1

To determine the screen dimensions, you can utilize the window object and specify the desired width and height of a card element.

The provided example illustrates this concept:

 import { StyleSheet, Dimensions } from 'react-native';
    
    const window = Dimensions.get('window');

    styles = StyleSheet.create({

    container: {
         width: (window.width) * 0.8,
         height: (window.width) * 0.8
    }, 
   
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for modifying the function of an Electron application using an external JavaScript file

This is the project structure I envision. https://i.stack.imgur.com/ocRp9.jpg kareljs folder houses an Electron app, and upon running npm start within that directory, a window appears and executes the run method of karel.js when the button Run Karel is ...

Why is jQuery not recognizing the variable, but when I manually input the exact same value it works?

Explained using a code snippet: $(".myParent .myChild:nth-child(3n)").css('border-top-color','#ffffff'); √ Success myVar = "3n"; $(".myParent .myChild:nth-child(myVar)").css('border-top-color','#ffffff'); ...

Issues arise with req.body being undefined when using node.js in conjunction with Express version 4.16.X and Body-Parser version

Recently, I began using node.js to construct a RESTFul API. Currently, my focus lies in inserting data through POST requests with JSON in the body. However, I encountered an issue where the req.body always returned as undefined. Upon investigating further ...

The function chartobject-1.render() is returning an error message stating: "Unable to locate the specified container within the document. This issue is occurring in the

I've encountered an issue while trying to integrate FusionCharts into my Vue.js project. Every time I attempt to render the charts within my components, I consistently run into this error. Here's a snippet of one of the components: <template&g ...

Requesting Axios.get for the value of years on end

I'm grappling with obtaining a JSON file from the server. The endpoint requires a year parameter, which needs to be set as the current year number as its value (e.g., ?year=2019). Furthermore, I need to fetch data for the previous and upcoming years a ...

How can we enhance the efficiency of rendering text on the screen?

Imagine having a <p> tag inside a <div> with specific properties: div { height: 100px; width: 100px; overflow: hidden; } My goal is to continuously add words to the <p> tag until an overflow is detected, meaning stop when the f ...

Convert the HTML content into a PDF while retaining the CSS styles using either JavaScript or Django

Looking to create a PDF of an HTML element with background color and images. Seeking a solution, either client-side or server-side using Django/Python. Tried jsPDF on the client side, but it does not support CSS. Considering ReportLab for Django, but uns ...

How can transitions be activated in Bootstrap 4 carousel?

Having an issue with my Bootstrap 4 carousel where I need to move 4 slides at a time. I have tried using the code below: $(this).carousel(4); However, the problem is that the carousel only jumps to that index without showing any transitions. Using $(this) ...

Detecting single letters in a sentence and changing their appearance using CSS

Looking to make a subtle change to text? I need to swap out single letters in a passage (I have a cat that ate a fish). Any ideas on how to do this? The goal is to input a block of text into a textbox, then display it in a div. I've had difficulty fi ...

Can I obtain a dictionary containing the connections between labels and phrases from the classifier?

I've implemented a LogisticRegressionClassifier using the natural library for node: const natural = require('natural'); const classifier = new natural.LogisticRegressionClassifier(); classifier.addDocument('category1', 'sent ...

Passing an ID in Next.js without showing it in the URL

I am looking to transfer the product id from the category page to the product page without showing it in the URL Category.js <h2> <Link href={{ pathname: `/product/car/${title}`, query: { id: Item.id, }, }} as={`/p ...

Using jQuery to implement conditional logic in HTML

Is there a way to exclude tags/variables in a jQuery HTML method? For example, if the company field is empty, it should not appear in the HTML. $('.modal-body').html(` <p><span style="font-weight:bold;">Name:< ...

The troubleshooting guide for resolving issues with AngularJS data binding functionality

Exploring the world of AngularJS and encountering a minor setback with a basic example that seems to be refusing to load Angular. Within this particular example, there is a straightforward data binding test in which {{}} is utilized to display a hardcoded ...

utilizing the .on method for dynamically inserted elements

I have a code snippet that triggers an AJAX request to another script and adds new <li> elements every time the "more" button is clicked. The code I am using is as follows: $(function(){ $('.more').on("click",function(){ var ID = $(th ...

Creating JSX elements in React by mapping over an object's properties

I need to display the properties of an object named tour in JSX elements using React without repeating code. Each property should be shown within a div, with the property name as a label and its value next to it. Although I attempted to iterate over the o ...

methods for detaching event listener from bootstrap carousel indicator elements

I am currently utilizing the bootstrap5 carousel feature and I am seeking a way to trigger a custom event when either the previous or next indicators are clicked. My goal is to stop the default bootstrap event from being triggered, however my attempts at v ...

Mapbox GL - Incorporate a non-scaling polygon feature during zoom operations

Is there a method to incorporate a polygon that maintains its size regardless of zooming? Currently, when adding a polygon circle shape with a 10-meter radius and zooming out, the polygon shrinks in size as expected. However, is it feasible to include a s ...

Accessing data from localhost using Vue.js and Axios is restricted due to CORS policy and the error "Access to XMLHttpRequest" may be

Having a minor issue... Trying to retrieve a basic json file from my server for testing purposes (not an actual API). Utilizing VueJS and axios. Here is my code : getServicesAPI() { axios.get("http://51.91..../dist/API/Services.json").the ...

How can we best store the component's state in the URL in AngularJS?

I am working with a reusable widget that has its own state. This state includes the content of the search bar (2), one or more select boxes (1), and the tree where the user can pick the currently active element (3). My goal is to create a locationManager ...

Mixing box-shadow and blur filters can result in unusual artifacts appearing in Webkit browsers

Recently, I encountered an intriguing and frustrating bug in WebKit browsers. Consider a scenario where there is a parent DIV (let's say .wrapper) and a child DIV (.main). You apply a box-shadow on the .main DIV and a blur filter on the .wrapper DIV. ...