How to round button before rendering?Android and calculating the size of a one-line string in a given font and font size?React Native Fetch does not render response until after clicking screenShow splash screen before show main screen in react native without using 3rd party libraryOverlay button on top of Image in React NativeReact Native Elements ListItem borderBottom Length ControlMake border-radius greater than half of heightNot able to navigate to other page in react nativenavigate from another screen on click navigationDrawerLayout menuDisplay always side menu (drawer) on large devices like tablets [react-native-navigation]Problem iterating through onLayout calls for providing row heights of items to FlatList's getItemLayoutHow to detect if screen has rounded corners in react-native

Decrease spacing between a bullet point and its subbullet point

How can I use my cell phone's light as a reading light?

Who goes first? Person disembarking bus or the bicycle?

Why the Cauchy Distribution is so useful?

Where are the Wazirs?

With a data transfer of 50 GB estimated 5 hours, are USB-C claimed speeds inaccurate or to blame?

Perl regex matching apostrophe fails

Strong Password Detection in Python

Array or vector? Two dimensional array or matrix?

Sense of humor in your sci-fi stories

How to evaluate the performance of open source solver?

What was the profession 芸者 (female entertainer) called in Russia?

How to understand flavors and when to use combination of them?

Writing an ace/aro character?

Why did Old English lose both thorn and eth?

What was the nature of the known bugs in the Space Shuttle software?

Is this really the Saturn V computer only, or are there other systems here as well?

Tesco's Burger Relish Best Before End date number

run bash scripts in folder all at the same time

Would denouncing cheaters from an exam make me less likely to receive penalties?

Gaining Proficiency in Vehicles (water)

stuck in/at beta

What does the multimeter dial do internally?

What was the significance of Spider-Man: Far From Home being an MCU Phase 3 film instead of a Phase 4 film?



How to round button before rendering?


Android and calculating the size of a one-line string in a given font and font size?React Native Fetch does not render response until after clicking screenShow splash screen before show main screen in react native without using 3rd party libraryOverlay button on top of Image in React NativeReact Native Elements ListItem borderBottom Length ControlMake border-radius greater than half of heightNot able to navigate to other page in react nativenavigate from another screen on click navigationDrawerLayout menuDisplay always side menu (drawer) on large devices like tablets [react-native-navigation]Problem iterating through onLayout calls for providing row heights of items to FlatList's getItemLayoutHow to detect if screen has rounded corners in react-native






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








7















I want to create a button component that will automatically have rounded corners, no matter its dimension.



As you know, to achieve rounded corners, one way to achieve it is to specify the border radius as half of the height of the button.



The way I implemented is that in the custom component I use the onLayout function like this:



 onLayout(event: LayoutChangeEvent) 
const height = event.nativeEvent.layout;
this.setState( borderRadius: height / 2 );



The problem is that the button will initially appear on screen as a rectangle and only after a millisecond, it will round the corners causing a flicker.



My guess is that onLayout is called after the component renders.



How would one go about implementing this? Thanks!










share|improve this question






















  • Mind sharing your button component and style too?

    – wicky
    Mar 26 at 2:34











  • If you want to set borderRadius before onLayout then you can use this.setState( borderRadius: 50 ); in componentDidMount() method. But if you want to set borderRadius "onLayout" then you can use timeout of 1000ms. Let me know if it works.

    – Pradnya Choudhari
    Mar 26 at 10:34












  • You could also just make the initial borderRadius style a super high number. It will never be "more than rounded", but would appear rounded no matter how big or small.

    – Jono
    Mar 28 at 16:22

















7















I want to create a button component that will automatically have rounded corners, no matter its dimension.



As you know, to achieve rounded corners, one way to achieve it is to specify the border radius as half of the height of the button.



The way I implemented is that in the custom component I use the onLayout function like this:



 onLayout(event: LayoutChangeEvent) 
const height = event.nativeEvent.layout;
this.setState( borderRadius: height / 2 );



The problem is that the button will initially appear on screen as a rectangle and only after a millisecond, it will round the corners causing a flicker.



My guess is that onLayout is called after the component renders.



How would one go about implementing this? Thanks!










share|improve this question






















  • Mind sharing your button component and style too?

    – wicky
    Mar 26 at 2:34











  • If you want to set borderRadius before onLayout then you can use this.setState( borderRadius: 50 ); in componentDidMount() method. But if you want to set borderRadius "onLayout" then you can use timeout of 1000ms. Let me know if it works.

    – Pradnya Choudhari
    Mar 26 at 10:34












  • You could also just make the initial borderRadius style a super high number. It will never be "more than rounded", but would appear rounded no matter how big or small.

    – Jono
    Mar 28 at 16:22













7












7








7








I want to create a button component that will automatically have rounded corners, no matter its dimension.



As you know, to achieve rounded corners, one way to achieve it is to specify the border radius as half of the height of the button.



The way I implemented is that in the custom component I use the onLayout function like this:



 onLayout(event: LayoutChangeEvent) 
const height = event.nativeEvent.layout;
this.setState( borderRadius: height / 2 );



The problem is that the button will initially appear on screen as a rectangle and only after a millisecond, it will round the corners causing a flicker.



My guess is that onLayout is called after the component renders.



How would one go about implementing this? Thanks!










share|improve this question














I want to create a button component that will automatically have rounded corners, no matter its dimension.



As you know, to achieve rounded corners, one way to achieve it is to specify the border radius as half of the height of the button.



The way I implemented is that in the custom component I use the onLayout function like this:



 onLayout(event: LayoutChangeEvent) 
const height = event.nativeEvent.layout;
this.setState( borderRadius: height / 2 );



The problem is that the button will initially appear on screen as a rectangle and only after a millisecond, it will round the corners causing a flicker.



My guess is that onLayout is called after the component renders.



How would one go about implementing this? Thanks!







react-native






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 16 at 18:16









Toma Radu-PetrescuToma Radu-Petrescu

8089 silver badges31 bronze badges




8089 silver badges31 bronze badges












  • Mind sharing your button component and style too?

    – wicky
    Mar 26 at 2:34











  • If you want to set borderRadius before onLayout then you can use this.setState( borderRadius: 50 ); in componentDidMount() method. But if you want to set borderRadius "onLayout" then you can use timeout of 1000ms. Let me know if it works.

    – Pradnya Choudhari
    Mar 26 at 10:34












  • You could also just make the initial borderRadius style a super high number. It will never be "more than rounded", but would appear rounded no matter how big or small.

    – Jono
    Mar 28 at 16:22

















  • Mind sharing your button component and style too?

    – wicky
    Mar 26 at 2:34











  • If you want to set borderRadius before onLayout then you can use this.setState( borderRadius: 50 ); in componentDidMount() method. But if you want to set borderRadius "onLayout" then you can use timeout of 1000ms. Let me know if it works.

    – Pradnya Choudhari
    Mar 26 at 10:34












  • You could also just make the initial borderRadius style a super high number. It will never be "more than rounded", but would appear rounded no matter how big or small.

    – Jono
    Mar 28 at 16:22
















Mind sharing your button component and style too?

– wicky
Mar 26 at 2:34





Mind sharing your button component and style too?

– wicky
Mar 26 at 2:34













If you want to set borderRadius before onLayout then you can use this.setState( borderRadius: 50 ); in componentDidMount() method. But if you want to set borderRadius "onLayout" then you can use timeout of 1000ms. Let me know if it works.

– Pradnya Choudhari
Mar 26 at 10:34






If you want to set borderRadius before onLayout then you can use this.setState( borderRadius: 50 ); in componentDidMount() method. But if you want to set borderRadius "onLayout" then you can use timeout of 1000ms. Let me know if it works.

– Pradnya Choudhari
Mar 26 at 10:34














You could also just make the initial borderRadius style a super high number. It will never be "more than rounded", but would appear rounded no matter how big or small.

– Jono
Mar 28 at 16:22





You could also just make the initial borderRadius style a super high number. It will never be "more than rounded", but would appear rounded no matter how big or small.

– Jono
Mar 28 at 16:22












3 Answers
3






active

oldest

votes


















0














Before the borderRadius is calculated, you could return transparent button, this would prevent this flickering effect...



// you pass radius, and height from component state

const MyButton = ( radius, height ) =>
if (radius === null) return <View style= backgroundColor: transparent >...</View>

else return <View style= borderRadius: radius, backgroundColor: 'red' >...</View>;
;





share|improve this answer






























    0














    To do this precisely, you would need to know the size that the string would take up once rendered. I wasn't able to find a React Native API for this (and I'm assuming you couldn't either), but I know both Android and iOS have such APIs. So therefore the solution would be to create a native module (https://facebook.github.io/react-native/docs/native-modules-android.html) for in iOS and Android which exposes a method called "measureText" or something. Then in each native class you'd use the corresponding API:



    • Android API mentioned in this answer should work: https://stackoverflow.com/a/7329169/3930970

    • iOS API: https://developer.apple.com/documentation/foundation/nsstring/1531844-sizewithattributes

    I haven't actually tried this so I'm curious if something like this ends up working. Cheers!






    share|improve this answer






























      0














      You can use the lifecycle method componentWillMount() to calculate the border radius:



      componentWillMount() 
      const height = Dimensions.get('window');
      radius = height / 2;




      This method is only called one time, which is before the initial
      render. Since this method is called before render().




      And then, you can style your button using the calculated radius:



      <TouchableOpacity
      style=[styles.button, borderRadius: radius ]
      onPress=() => alert('Hello World!')
      >


      Here is a working demo.






      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55200039%2fhow-to-round-button-before-rendering%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Before the borderRadius is calculated, you could return transparent button, this would prevent this flickering effect...



        // you pass radius, and height from component state

        const MyButton = ( radius, height ) =>
        if (radius === null) return <View style= backgroundColor: transparent >...</View>

        else return <View style= borderRadius: radius, backgroundColor: 'red' >...</View>;
        ;





        share|improve this answer



























          0














          Before the borderRadius is calculated, you could return transparent button, this would prevent this flickering effect...



          // you pass radius, and height from component state

          const MyButton = ( radius, height ) =>
          if (radius === null) return <View style= backgroundColor: transparent >...</View>

          else return <View style= borderRadius: radius, backgroundColor: 'red' >...</View>;
          ;





          share|improve this answer

























            0












            0








            0







            Before the borderRadius is calculated, you could return transparent button, this would prevent this flickering effect...



            // you pass radius, and height from component state

            const MyButton = ( radius, height ) =>
            if (radius === null) return <View style= backgroundColor: transparent >...</View>

            else return <View style= borderRadius: radius, backgroundColor: 'red' >...</View>;
            ;





            share|improve this answer













            Before the borderRadius is calculated, you could return transparent button, this would prevent this flickering effect...



            // you pass radius, and height from component state

            const MyButton = ( radius, height ) =>
            if (radius === null) return <View style= backgroundColor: transparent >...</View>

            else return <View style= borderRadius: radius, backgroundColor: 'red' >...</View>;
            ;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 16 at 18:24









            Hend El-SahliHend El-Sahli

            1,4651 gold badge3 silver badges16 bronze badges




            1,4651 gold badge3 silver badges16 bronze badges























                0














                To do this precisely, you would need to know the size that the string would take up once rendered. I wasn't able to find a React Native API for this (and I'm assuming you couldn't either), but I know both Android and iOS have such APIs. So therefore the solution would be to create a native module (https://facebook.github.io/react-native/docs/native-modules-android.html) for in iOS and Android which exposes a method called "measureText" or something. Then in each native class you'd use the corresponding API:



                • Android API mentioned in this answer should work: https://stackoverflow.com/a/7329169/3930970

                • iOS API: https://developer.apple.com/documentation/foundation/nsstring/1531844-sizewithattributes

                I haven't actually tried this so I'm curious if something like this ends up working. Cheers!






                share|improve this answer



























                  0














                  To do this precisely, you would need to know the size that the string would take up once rendered. I wasn't able to find a React Native API for this (and I'm assuming you couldn't either), but I know both Android and iOS have such APIs. So therefore the solution would be to create a native module (https://facebook.github.io/react-native/docs/native-modules-android.html) for in iOS and Android which exposes a method called "measureText" or something. Then in each native class you'd use the corresponding API:



                  • Android API mentioned in this answer should work: https://stackoverflow.com/a/7329169/3930970

                  • iOS API: https://developer.apple.com/documentation/foundation/nsstring/1531844-sizewithattributes

                  I haven't actually tried this so I'm curious if something like this ends up working. Cheers!






                  share|improve this answer

























                    0












                    0








                    0







                    To do this precisely, you would need to know the size that the string would take up once rendered. I wasn't able to find a React Native API for this (and I'm assuming you couldn't either), but I know both Android and iOS have such APIs. So therefore the solution would be to create a native module (https://facebook.github.io/react-native/docs/native-modules-android.html) for in iOS and Android which exposes a method called "measureText" or something. Then in each native class you'd use the corresponding API:



                    • Android API mentioned in this answer should work: https://stackoverflow.com/a/7329169/3930970

                    • iOS API: https://developer.apple.com/documentation/foundation/nsstring/1531844-sizewithattributes

                    I haven't actually tried this so I'm curious if something like this ends up working. Cheers!






                    share|improve this answer













                    To do this precisely, you would need to know the size that the string would take up once rendered. I wasn't able to find a React Native API for this (and I'm assuming you couldn't either), but I know both Android and iOS have such APIs. So therefore the solution would be to create a native module (https://facebook.github.io/react-native/docs/native-modules-android.html) for in iOS and Android which exposes a method called "measureText" or something. Then in each native class you'd use the corresponding API:



                    • Android API mentioned in this answer should work: https://stackoverflow.com/a/7329169/3930970

                    • iOS API: https://developer.apple.com/documentation/foundation/nsstring/1531844-sizewithattributes

                    I haven't actually tried this so I'm curious if something like this ends up working. Cheers!







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 31 at 22:25









                    samgriggsamgrigg

                    112 bronze badges




                    112 bronze badges





















                        0














                        You can use the lifecycle method componentWillMount() to calculate the border radius:



                        componentWillMount() 
                        const height = Dimensions.get('window');
                        radius = height / 2;




                        This method is only called one time, which is before the initial
                        render. Since this method is called before render().




                        And then, you can style your button using the calculated radius:



                        <TouchableOpacity
                        style=[styles.button, borderRadius: radius ]
                        onPress=() => alert('Hello World!')
                        >


                        Here is a working demo.






                        share|improve this answer



























                          0














                          You can use the lifecycle method componentWillMount() to calculate the border radius:



                          componentWillMount() 
                          const height = Dimensions.get('window');
                          radius = height / 2;




                          This method is only called one time, which is before the initial
                          render. Since this method is called before render().




                          And then, you can style your button using the calculated radius:



                          <TouchableOpacity
                          style=[styles.button, borderRadius: radius ]
                          onPress=() => alert('Hello World!')
                          >


                          Here is a working demo.






                          share|improve this answer

























                            0












                            0








                            0







                            You can use the lifecycle method componentWillMount() to calculate the border radius:



                            componentWillMount() 
                            const height = Dimensions.get('window');
                            radius = height / 2;




                            This method is only called one time, which is before the initial
                            render. Since this method is called before render().




                            And then, you can style your button using the calculated radius:



                            <TouchableOpacity
                            style=[styles.button, borderRadius: radius ]
                            onPress=() => alert('Hello World!')
                            >


                            Here is a working demo.






                            share|improve this answer













                            You can use the lifecycle method componentWillMount() to calculate the border radius:



                            componentWillMount() 
                            const height = Dimensions.get('window');
                            radius = height / 2;




                            This method is only called one time, which is before the initial
                            render. Since this method is called before render().




                            And then, you can style your button using the calculated radius:



                            <TouchableOpacity
                            style=[styles.button, borderRadius: radius ]
                            onPress=() => alert('Hello World!')
                            >


                            Here is a working demo.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Apr 1 at 7:45









                            Hristo EftimovHristo Eftimov

                            4,9228 gold badges34 silver badges57 bronze badges




                            4,9228 gold badges34 silver badges57 bronze badges



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55200039%2fhow-to-round-button-before-rendering%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript