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;
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
add a comment |
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
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
add a comment |
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
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
react-native
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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>;
;
add a comment |
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!
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>;
;
add a comment |
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>;
;
add a comment |
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>;
;
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>;
;
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
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
answered Mar 31 at 22:25
samgriggsamgrigg
112 bronze badges
112 bronze badges
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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