How to align child views to left with even space in React-Native?How to justify (left, right, center) each child independently?What is the difference between using constructor vs getInitialState in React / React Native?What is the difference between React Native and React?alignItems: center prevents flex row text wrapping in React NativeHow to use alignItems with flex value and wrap?React Native - image pushes text out screen - fixed with 3rd view?Show splash screen before show main screen in react native without using 3rd party libraryComponent won't stay wrapped within bounds (React Native)React-Native error adding a video using expo to the app.js for backgroundNot able to navigate to other page in react nativeHow to fix this error 'undefined is not an object'?

What is the difference between lands and mana?

How much theory knowledge is actually used while playing?

Why Shazam when there is already Superman?

Why does this expression simplify as such?

Quoting Keynes in a lecture

Does "he squandered his car on drink" sound natural?

What's the name of the logical fallacy where a debater extends a statement far beyond the original statement to make it true?

What to do when eye contact makes your coworker uncomfortable?

Why is so much work done on numerical verification of the Riemann Hypothesis?

Will number of steps recorded on FitBit/any fitness tracker add up distance in PokemonGo?

Creating two special characters

What kind of floor tile is this?

Why do Radio Buttons not fill the entire outer circle?

What is going on with gets(stdin) on the site coderbyte?

Is there any evidence that Cleopatra and Caesarion considered fleeing to India to escape the Romans?

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

Does the reader need to like the PoV character?

Is there a way to have vectors outlined in a Vector Plot?

What fields between the rationals and the reals allow a good notion of 2D distance?

Which was the first story featuring espers?

Delete multiple columns using awk or sed

Is this toilet slogan correct usage of the English language?

US tourist/student visa

15% tax on $7.5k earnings. Is that right?



How to align child views to left with even space in React-Native?


How to justify (left, right, center) each child independently?What is the difference between using constructor vs getInitialState in React / React Native?What is the difference between React Native and React?alignItems: center prevents flex row text wrapping in React NativeHow to use alignItems with flex value and wrap?React Native - image pushes text out screen - fixed with 3rd view?Show splash screen before show main screen in react native without using 3rd party libraryComponent won't stay wrapped within bounds (React Native)React-Native error adding a video using expo to the app.js for backgroundNot able to navigate to other page in react nativeHow to fix this error 'undefined is not an object'?













5















I'm trying to create a menu for my application in React-Native which should have multiple icons in the below way
enter image description here



The icons should be in the same row and wrapped so that if screen is bigger more icons will be on the same row.



My current code is as follows



import React from 'react';
import StyleSheet, View from 'react-native';

export default class App extends React.Component
render()
return (
<View style=styles.container>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
</View>
);



const styles = StyleSheet.create(
container:
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'space-evenly',
flexDirection: 'row',
flexWrap: 'wrap',
paddingTop: 40
,
box:
width: 100,
height: 100,
backgroundColor: 'aqua',
margin: 10,

);


The current output is as below



enter image description here



The children count may change in the future but i need to have spacing on the sides, using flex-start will give the below output which is wrong.i want to have spacing in both sides as well.



enter image description here



How do i align it to left and have the items with even space around as the image above ?










share|improve this question
























  • alignItems: 'center' would center the blocks. Change it to alignItems: 'left' and it should align to the left

    – Aniket G
    Mar 7 at 5:04











  • its react-native and left is not supported :)

    – Guruparan Giritharan
    Mar 7 at 5:05











  • Right. Change justifyContent: 'space-evenly' to justifyContent: 'flex-start' stackoverflow.com/questions/36008969/…

    – Aniket G
    Mar 7 at 5:06












  • Possible duplicate of How to justify (left, right, center) each child independently?

    – Aniket G
    Mar 7 at 5:07











  • @AniketG flext-start i'm trying to use the same row so flex start will have more space at the right which is the problem, as this is the same row its not a duplicate

    – Guruparan Giritharan
    Mar 7 at 5:10















5















I'm trying to create a menu for my application in React-Native which should have multiple icons in the below way
enter image description here



The icons should be in the same row and wrapped so that if screen is bigger more icons will be on the same row.



My current code is as follows



import React from 'react';
import StyleSheet, View from 'react-native';

export default class App extends React.Component
render()
return (
<View style=styles.container>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
</View>
);



const styles = StyleSheet.create(
container:
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'space-evenly',
flexDirection: 'row',
flexWrap: 'wrap',
paddingTop: 40
,
box:
width: 100,
height: 100,
backgroundColor: 'aqua',
margin: 10,

);


The current output is as below



enter image description here



The children count may change in the future but i need to have spacing on the sides, using flex-start will give the below output which is wrong.i want to have spacing in both sides as well.



enter image description here



How do i align it to left and have the items with even space around as the image above ?










share|improve this question
























  • alignItems: 'center' would center the blocks. Change it to alignItems: 'left' and it should align to the left

    – Aniket G
    Mar 7 at 5:04











  • its react-native and left is not supported :)

    – Guruparan Giritharan
    Mar 7 at 5:05











  • Right. Change justifyContent: 'space-evenly' to justifyContent: 'flex-start' stackoverflow.com/questions/36008969/…

    – Aniket G
    Mar 7 at 5:06












  • Possible duplicate of How to justify (left, right, center) each child independently?

    – Aniket G
    Mar 7 at 5:07











  • @AniketG flext-start i'm trying to use the same row so flex start will have more space at the right which is the problem, as this is the same row its not a duplicate

    – Guruparan Giritharan
    Mar 7 at 5:10













5












5








5


1






I'm trying to create a menu for my application in React-Native which should have multiple icons in the below way
enter image description here



The icons should be in the same row and wrapped so that if screen is bigger more icons will be on the same row.



My current code is as follows



import React from 'react';
import StyleSheet, View from 'react-native';

export default class App extends React.Component
render()
return (
<View style=styles.container>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
</View>
);



const styles = StyleSheet.create(
container:
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'space-evenly',
flexDirection: 'row',
flexWrap: 'wrap',
paddingTop: 40
,
box:
width: 100,
height: 100,
backgroundColor: 'aqua',
margin: 10,

);


The current output is as below



enter image description here



The children count may change in the future but i need to have spacing on the sides, using flex-start will give the below output which is wrong.i want to have spacing in both sides as well.



enter image description here



How do i align it to left and have the items with even space around as the image above ?










share|improve this question
















I'm trying to create a menu for my application in React-Native which should have multiple icons in the below way
enter image description here



The icons should be in the same row and wrapped so that if screen is bigger more icons will be on the same row.



My current code is as follows



import React from 'react';
import StyleSheet, View from 'react-native';

export default class App extends React.Component
render()
return (
<View style=styles.container>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
</View>
);



const styles = StyleSheet.create(
container:
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'space-evenly',
flexDirection: 'row',
flexWrap: 'wrap',
paddingTop: 40
,
box:
width: 100,
height: 100,
backgroundColor: 'aqua',
margin: 10,

);


The current output is as below



enter image description here



The children count may change in the future but i need to have spacing on the sides, using flex-start will give the below output which is wrong.i want to have spacing in both sides as well.



enter image description here



How do i align it to left and have the items with even space around as the image above ?







javascript react-native






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 5:16







Guruparan Giritharan

















asked Mar 7 at 4:57









Guruparan GiritharanGuruparan Giritharan

5201621




5201621












  • alignItems: 'center' would center the blocks. Change it to alignItems: 'left' and it should align to the left

    – Aniket G
    Mar 7 at 5:04











  • its react-native and left is not supported :)

    – Guruparan Giritharan
    Mar 7 at 5:05











  • Right. Change justifyContent: 'space-evenly' to justifyContent: 'flex-start' stackoverflow.com/questions/36008969/…

    – Aniket G
    Mar 7 at 5:06












  • Possible duplicate of How to justify (left, right, center) each child independently?

    – Aniket G
    Mar 7 at 5:07











  • @AniketG flext-start i'm trying to use the same row so flex start will have more space at the right which is the problem, as this is the same row its not a duplicate

    – Guruparan Giritharan
    Mar 7 at 5:10

















  • alignItems: 'center' would center the blocks. Change it to alignItems: 'left' and it should align to the left

    – Aniket G
    Mar 7 at 5:04











  • its react-native and left is not supported :)

    – Guruparan Giritharan
    Mar 7 at 5:05











  • Right. Change justifyContent: 'space-evenly' to justifyContent: 'flex-start' stackoverflow.com/questions/36008969/…

    – Aniket G
    Mar 7 at 5:06












  • Possible duplicate of How to justify (left, right, center) each child independently?

    – Aniket G
    Mar 7 at 5:07











  • @AniketG flext-start i'm trying to use the same row so flex start will have more space at the right which is the problem, as this is the same row its not a duplicate

    – Guruparan Giritharan
    Mar 7 at 5:10
















alignItems: 'center' would center the blocks. Change it to alignItems: 'left' and it should align to the left

– Aniket G
Mar 7 at 5:04





alignItems: 'center' would center the blocks. Change it to alignItems: 'left' and it should align to the left

– Aniket G
Mar 7 at 5:04













its react-native and left is not supported :)

– Guruparan Giritharan
Mar 7 at 5:05





its react-native and left is not supported :)

– Guruparan Giritharan
Mar 7 at 5:05













Right. Change justifyContent: 'space-evenly' to justifyContent: 'flex-start' stackoverflow.com/questions/36008969/…

– Aniket G
Mar 7 at 5:06






Right. Change justifyContent: 'space-evenly' to justifyContent: 'flex-start' stackoverflow.com/questions/36008969/…

– Aniket G
Mar 7 at 5:06














Possible duplicate of How to justify (left, right, center) each child independently?

– Aniket G
Mar 7 at 5:07





Possible duplicate of How to justify (left, right, center) each child independently?

– Aniket G
Mar 7 at 5:07













@AniketG flext-start i'm trying to use the same row so flex start will have more space at the right which is the problem, as this is the same row its not a duplicate

– Guruparan Giritharan
Mar 7 at 5:10





@AniketG flext-start i'm trying to use the same row so flex start will have more space at the right which is the problem, as this is the same row its not a duplicate

– Guruparan Giritharan
Mar 7 at 5:10












3 Answers
3






active

oldest

votes


















1














for Box use Dimensions, Based on screen width divide box width



const styles = StyleSheet.create(
container:
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'flex-start',
flexDirection: 'row',
flexWrap: 'wrap',
paddingTop: 40
,
box:
width: (Dimensions.get('window').width / 3) - 20, /* minus some value for adjust the gap between boxes */
height: 100,
backgroundColor: 'aqua',
margin: 10,

);





share|improve this answer

























  • Whats the logic behind 3.2 ?

    – Guruparan Giritharan
    Mar 7 at 7:21











  • pre row if you need 3 box width / 3 but it will divide equally based on screen size. you need some gape between each box so I divided with 3.2

    – koneri ranjith kumar
    Mar 7 at 8:33











  • 3 may change based on screen size, i'm trying a way to calculate that

    – Guruparan Giritharan
    Mar 7 at 8:36











  • you can do this way also ==> width: (Dimensions.get('window').width / 3) - 20 // minus 20 is for some value for adjust the gap between boxes, I added the StyleSheet code. can you check is this working?

    – koneri ranjith kumar
    Mar 7 at 9:39












  • I took a different approach and solved the problem, thanks for your suggestion.

    – Guruparan Giritharan
    Mar 7 at 10:50


















0














One option is to add extra 'fake' boxes which will fill the available space in last row:



<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=styles.box></View>
<View style=[styles.box, styles.boxFake]></View>
<View style=[styles.box, styles.boxFake]></View>
<View style=[styles.box, styles.boxFake]></View>
<View style=[styles.box, styles.boxFake]></View>
<View style=[styles.box, styles.boxFake]></View>

// reset all styles like backgroundColor, border, etc.
const styles = StyleSheet.create(
boxFake:
backgroundColor: 'transparent'

);


You can easy calculate the number of necessary 'fake' boxes by the formula:




fakeBoxes = boxesPerRow - totalBoxes % boxesPerRow







share|improve this answer






























    0














    I took a different approach by using another view as a wrapper and doing the calculation of its width, this is easier to decide the column widths.
    The only problem is that we should know the width of the item, wont be a problem in my case.
    The code will be as below.



    import React from 'react';
    import StyleSheet, View, ScrollView from 'react-native';

    export default class App extends React.Component

    constructor(props)
    super(props);
    this.state =
    width: 110
    ;


    render()
    //width of child is 110
    const width = `$100 / parseInt(this.state.width / 110)%`;
    return (
    <ScrollView>
    <View style=styles.container onLayout=this.onLayout.bind(this)>
    <View style=[styles.wrapper, width: width ]>
    <View style=styles.box></View>
    </View>
    <View style=[styles.wrapper, width: width ]>
    <View style=styles.box></View>
    </View>
    <View style=[styles.wrapper, width: width ]>
    <View style=styles.box></View>
    </View>
    <View style=[styles.wrapper, width: width ]>
    <View style=styles.box></View>
    </View>
    <View style=[styles.wrapper, width: width ]>
    <View style=styles.box></View>
    </View>
    </View>
    </ScrollView>
    );


    onLayout(e)
    if (this.state.width !== e.nativeEvent.layout.width)
    this.setState(
    width: e.nativeEvent.layout.width
    )




    const styles = StyleSheet.create(
    container:
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'flex-start',
    justifyContent: 'flex-start',
    flexDirection: 'row',
    flexWrap: 'wrap',
    paddingTop: 40
    ,
    box:
    width: 100,
    height: 100,
    backgroundColor: 'green',
    ,
    wrapper:
    marginVertical: 10, alignItems: 'center'

    );





    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%2f55036364%2fhow-to-align-child-views-to-left-with-even-space-in-react-native%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









      1














      for Box use Dimensions, Based on screen width divide box width



      const styles = StyleSheet.create(
      container:
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'flex-start',
      flexDirection: 'row',
      flexWrap: 'wrap',
      paddingTop: 40
      ,
      box:
      width: (Dimensions.get('window').width / 3) - 20, /* minus some value for adjust the gap between boxes */
      height: 100,
      backgroundColor: 'aqua',
      margin: 10,

      );





      share|improve this answer

























      • Whats the logic behind 3.2 ?

        – Guruparan Giritharan
        Mar 7 at 7:21











      • pre row if you need 3 box width / 3 but it will divide equally based on screen size. you need some gape between each box so I divided with 3.2

        – koneri ranjith kumar
        Mar 7 at 8:33











      • 3 may change based on screen size, i'm trying a way to calculate that

        – Guruparan Giritharan
        Mar 7 at 8:36











      • you can do this way also ==> width: (Dimensions.get('window').width / 3) - 20 // minus 20 is for some value for adjust the gap between boxes, I added the StyleSheet code. can you check is this working?

        – koneri ranjith kumar
        Mar 7 at 9:39












      • I took a different approach and solved the problem, thanks for your suggestion.

        – Guruparan Giritharan
        Mar 7 at 10:50















      1














      for Box use Dimensions, Based on screen width divide box width



      const styles = StyleSheet.create(
      container:
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'flex-start',
      flexDirection: 'row',
      flexWrap: 'wrap',
      paddingTop: 40
      ,
      box:
      width: (Dimensions.get('window').width / 3) - 20, /* minus some value for adjust the gap between boxes */
      height: 100,
      backgroundColor: 'aqua',
      margin: 10,

      );





      share|improve this answer

























      • Whats the logic behind 3.2 ?

        – Guruparan Giritharan
        Mar 7 at 7:21











      • pre row if you need 3 box width / 3 but it will divide equally based on screen size. you need some gape between each box so I divided with 3.2

        – koneri ranjith kumar
        Mar 7 at 8:33











      • 3 may change based on screen size, i'm trying a way to calculate that

        – Guruparan Giritharan
        Mar 7 at 8:36











      • you can do this way also ==> width: (Dimensions.get('window').width / 3) - 20 // minus 20 is for some value for adjust the gap between boxes, I added the StyleSheet code. can you check is this working?

        – koneri ranjith kumar
        Mar 7 at 9:39












      • I took a different approach and solved the problem, thanks for your suggestion.

        – Guruparan Giritharan
        Mar 7 at 10:50













      1












      1








      1







      for Box use Dimensions, Based on screen width divide box width



      const styles = StyleSheet.create(
      container:
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'flex-start',
      flexDirection: 'row',
      flexWrap: 'wrap',
      paddingTop: 40
      ,
      box:
      width: (Dimensions.get('window').width / 3) - 20, /* minus some value for adjust the gap between boxes */
      height: 100,
      backgroundColor: 'aqua',
      margin: 10,

      );





      share|improve this answer















      for Box use Dimensions, Based on screen width divide box width



      const styles = StyleSheet.create(
      container:
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'flex-start',
      flexDirection: 'row',
      flexWrap: 'wrap',
      paddingTop: 40
      ,
      box:
      width: (Dimensions.get('window').width / 3) - 20, /* minus some value for adjust the gap between boxes */
      height: 100,
      backgroundColor: 'aqua',
      margin: 10,

      );






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 7 at 9:28

























      answered Mar 7 at 6:10









      koneri ranjith kumarkoneri ranjith kumar

      75111




      75111












      • Whats the logic behind 3.2 ?

        – Guruparan Giritharan
        Mar 7 at 7:21











      • pre row if you need 3 box width / 3 but it will divide equally based on screen size. you need some gape between each box so I divided with 3.2

        – koneri ranjith kumar
        Mar 7 at 8:33











      • 3 may change based on screen size, i'm trying a way to calculate that

        – Guruparan Giritharan
        Mar 7 at 8:36











      • you can do this way also ==> width: (Dimensions.get('window').width / 3) - 20 // minus 20 is for some value for adjust the gap between boxes, I added the StyleSheet code. can you check is this working?

        – koneri ranjith kumar
        Mar 7 at 9:39












      • I took a different approach and solved the problem, thanks for your suggestion.

        – Guruparan Giritharan
        Mar 7 at 10:50

















      • Whats the logic behind 3.2 ?

        – Guruparan Giritharan
        Mar 7 at 7:21











      • pre row if you need 3 box width / 3 but it will divide equally based on screen size. you need some gape between each box so I divided with 3.2

        – koneri ranjith kumar
        Mar 7 at 8:33











      • 3 may change based on screen size, i'm trying a way to calculate that

        – Guruparan Giritharan
        Mar 7 at 8:36











      • you can do this way also ==> width: (Dimensions.get('window').width / 3) - 20 // minus 20 is for some value for adjust the gap between boxes, I added the StyleSheet code. can you check is this working?

        – koneri ranjith kumar
        Mar 7 at 9:39












      • I took a different approach and solved the problem, thanks for your suggestion.

        – Guruparan Giritharan
        Mar 7 at 10:50
















      Whats the logic behind 3.2 ?

      – Guruparan Giritharan
      Mar 7 at 7:21





      Whats the logic behind 3.2 ?

      – Guruparan Giritharan
      Mar 7 at 7:21













      pre row if you need 3 box width / 3 but it will divide equally based on screen size. you need some gape between each box so I divided with 3.2

      – koneri ranjith kumar
      Mar 7 at 8:33





      pre row if you need 3 box width / 3 but it will divide equally based on screen size. you need some gape between each box so I divided with 3.2

      – koneri ranjith kumar
      Mar 7 at 8:33













      3 may change based on screen size, i'm trying a way to calculate that

      – Guruparan Giritharan
      Mar 7 at 8:36





      3 may change based on screen size, i'm trying a way to calculate that

      – Guruparan Giritharan
      Mar 7 at 8:36













      you can do this way also ==> width: (Dimensions.get('window').width / 3) - 20 // minus 20 is for some value for adjust the gap between boxes, I added the StyleSheet code. can you check is this working?

      – koneri ranjith kumar
      Mar 7 at 9:39






      you can do this way also ==> width: (Dimensions.get('window').width / 3) - 20 // minus 20 is for some value for adjust the gap between boxes, I added the StyleSheet code. can you check is this working?

      – koneri ranjith kumar
      Mar 7 at 9:39














      I took a different approach and solved the problem, thanks for your suggestion.

      – Guruparan Giritharan
      Mar 7 at 10:50





      I took a different approach and solved the problem, thanks for your suggestion.

      – Guruparan Giritharan
      Mar 7 at 10:50













      0














      One option is to add extra 'fake' boxes which will fill the available space in last row:



      <View style=styles.box></View>
      <View style=styles.box></View>
      <View style=styles.box></View>
      <View style=styles.box></View>
      <View style=styles.box></View>
      <View style=[styles.box, styles.boxFake]></View>
      <View style=[styles.box, styles.boxFake]></View>
      <View style=[styles.box, styles.boxFake]></View>
      <View style=[styles.box, styles.boxFake]></View>
      <View style=[styles.box, styles.boxFake]></View>

      // reset all styles like backgroundColor, border, etc.
      const styles = StyleSheet.create(
      boxFake:
      backgroundColor: 'transparent'

      );


      You can easy calculate the number of necessary 'fake' boxes by the formula:




      fakeBoxes = boxesPerRow - totalBoxes % boxesPerRow







      share|improve this answer



























        0














        One option is to add extra 'fake' boxes which will fill the available space in last row:



        <View style=styles.box></View>
        <View style=styles.box></View>
        <View style=styles.box></View>
        <View style=styles.box></View>
        <View style=styles.box></View>
        <View style=[styles.box, styles.boxFake]></View>
        <View style=[styles.box, styles.boxFake]></View>
        <View style=[styles.box, styles.boxFake]></View>
        <View style=[styles.box, styles.boxFake]></View>
        <View style=[styles.box, styles.boxFake]></View>

        // reset all styles like backgroundColor, border, etc.
        const styles = StyleSheet.create(
        boxFake:
        backgroundColor: 'transparent'

        );


        You can easy calculate the number of necessary 'fake' boxes by the formula:




        fakeBoxes = boxesPerRow - totalBoxes % boxesPerRow







        share|improve this answer

























          0












          0








          0







          One option is to add extra 'fake' boxes which will fill the available space in last row:



          <View style=styles.box></View>
          <View style=styles.box></View>
          <View style=styles.box></View>
          <View style=styles.box></View>
          <View style=styles.box></View>
          <View style=[styles.box, styles.boxFake]></View>
          <View style=[styles.box, styles.boxFake]></View>
          <View style=[styles.box, styles.boxFake]></View>
          <View style=[styles.box, styles.boxFake]></View>
          <View style=[styles.box, styles.boxFake]></View>

          // reset all styles like backgroundColor, border, etc.
          const styles = StyleSheet.create(
          boxFake:
          backgroundColor: 'transparent'

          );


          You can easy calculate the number of necessary 'fake' boxes by the formula:




          fakeBoxes = boxesPerRow - totalBoxes % boxesPerRow







          share|improve this answer













          One option is to add extra 'fake' boxes which will fill the available space in last row:



          <View style=styles.box></View>
          <View style=styles.box></View>
          <View style=styles.box></View>
          <View style=styles.box></View>
          <View style=styles.box></View>
          <View style=[styles.box, styles.boxFake]></View>
          <View style=[styles.box, styles.boxFake]></View>
          <View style=[styles.box, styles.boxFake]></View>
          <View style=[styles.box, styles.boxFake]></View>
          <View style=[styles.box, styles.boxFake]></View>

          // reset all styles like backgroundColor, border, etc.
          const styles = StyleSheet.create(
          boxFake:
          backgroundColor: 'transparent'

          );


          You can easy calculate the number of necessary 'fake' boxes by the formula:




          fakeBoxes = boxesPerRow - totalBoxes % boxesPerRow








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 7:28









          King JulienKing Julien

          3,8542272115




          3,8542272115





















              0














              I took a different approach by using another view as a wrapper and doing the calculation of its width, this is easier to decide the column widths.
              The only problem is that we should know the width of the item, wont be a problem in my case.
              The code will be as below.



              import React from 'react';
              import StyleSheet, View, ScrollView from 'react-native';

              export default class App extends React.Component

              constructor(props)
              super(props);
              this.state =
              width: 110
              ;


              render()
              //width of child is 110
              const width = `$100 / parseInt(this.state.width / 110)%`;
              return (
              <ScrollView>
              <View style=styles.container onLayout=this.onLayout.bind(this)>
              <View style=[styles.wrapper, width: width ]>
              <View style=styles.box></View>
              </View>
              <View style=[styles.wrapper, width: width ]>
              <View style=styles.box></View>
              </View>
              <View style=[styles.wrapper, width: width ]>
              <View style=styles.box></View>
              </View>
              <View style=[styles.wrapper, width: width ]>
              <View style=styles.box></View>
              </View>
              <View style=[styles.wrapper, width: width ]>
              <View style=styles.box></View>
              </View>
              </View>
              </ScrollView>
              );


              onLayout(e)
              if (this.state.width !== e.nativeEvent.layout.width)
              this.setState(
              width: e.nativeEvent.layout.width
              )




              const styles = StyleSheet.create(
              container:
              flex: 1,
              backgroundColor: '#fff',
              alignItems: 'flex-start',
              justifyContent: 'flex-start',
              flexDirection: 'row',
              flexWrap: 'wrap',
              paddingTop: 40
              ,
              box:
              width: 100,
              height: 100,
              backgroundColor: 'green',
              ,
              wrapper:
              marginVertical: 10, alignItems: 'center'

              );





              share|improve this answer



























                0














                I took a different approach by using another view as a wrapper and doing the calculation of its width, this is easier to decide the column widths.
                The only problem is that we should know the width of the item, wont be a problem in my case.
                The code will be as below.



                import React from 'react';
                import StyleSheet, View, ScrollView from 'react-native';

                export default class App extends React.Component

                constructor(props)
                super(props);
                this.state =
                width: 110
                ;


                render()
                //width of child is 110
                const width = `$100 / parseInt(this.state.width / 110)%`;
                return (
                <ScrollView>
                <View style=styles.container onLayout=this.onLayout.bind(this)>
                <View style=[styles.wrapper, width: width ]>
                <View style=styles.box></View>
                </View>
                <View style=[styles.wrapper, width: width ]>
                <View style=styles.box></View>
                </View>
                <View style=[styles.wrapper, width: width ]>
                <View style=styles.box></View>
                </View>
                <View style=[styles.wrapper, width: width ]>
                <View style=styles.box></View>
                </View>
                <View style=[styles.wrapper, width: width ]>
                <View style=styles.box></View>
                </View>
                </View>
                </ScrollView>
                );


                onLayout(e)
                if (this.state.width !== e.nativeEvent.layout.width)
                this.setState(
                width: e.nativeEvent.layout.width
                )




                const styles = StyleSheet.create(
                container:
                flex: 1,
                backgroundColor: '#fff',
                alignItems: 'flex-start',
                justifyContent: 'flex-start',
                flexDirection: 'row',
                flexWrap: 'wrap',
                paddingTop: 40
                ,
                box:
                width: 100,
                height: 100,
                backgroundColor: 'green',
                ,
                wrapper:
                marginVertical: 10, alignItems: 'center'

                );





                share|improve this answer

























                  0












                  0








                  0







                  I took a different approach by using another view as a wrapper and doing the calculation of its width, this is easier to decide the column widths.
                  The only problem is that we should know the width of the item, wont be a problem in my case.
                  The code will be as below.



                  import React from 'react';
                  import StyleSheet, View, ScrollView from 'react-native';

                  export default class App extends React.Component

                  constructor(props)
                  super(props);
                  this.state =
                  width: 110
                  ;


                  render()
                  //width of child is 110
                  const width = `$100 / parseInt(this.state.width / 110)%`;
                  return (
                  <ScrollView>
                  <View style=styles.container onLayout=this.onLayout.bind(this)>
                  <View style=[styles.wrapper, width: width ]>
                  <View style=styles.box></View>
                  </View>
                  <View style=[styles.wrapper, width: width ]>
                  <View style=styles.box></View>
                  </View>
                  <View style=[styles.wrapper, width: width ]>
                  <View style=styles.box></View>
                  </View>
                  <View style=[styles.wrapper, width: width ]>
                  <View style=styles.box></View>
                  </View>
                  <View style=[styles.wrapper, width: width ]>
                  <View style=styles.box></View>
                  </View>
                  </View>
                  </ScrollView>
                  );


                  onLayout(e)
                  if (this.state.width !== e.nativeEvent.layout.width)
                  this.setState(
                  width: e.nativeEvent.layout.width
                  )




                  const styles = StyleSheet.create(
                  container:
                  flex: 1,
                  backgroundColor: '#fff',
                  alignItems: 'flex-start',
                  justifyContent: 'flex-start',
                  flexDirection: 'row',
                  flexWrap: 'wrap',
                  paddingTop: 40
                  ,
                  box:
                  width: 100,
                  height: 100,
                  backgroundColor: 'green',
                  ,
                  wrapper:
                  marginVertical: 10, alignItems: 'center'

                  );





                  share|improve this answer













                  I took a different approach by using another view as a wrapper and doing the calculation of its width, this is easier to decide the column widths.
                  The only problem is that we should know the width of the item, wont be a problem in my case.
                  The code will be as below.



                  import React from 'react';
                  import StyleSheet, View, ScrollView from 'react-native';

                  export default class App extends React.Component

                  constructor(props)
                  super(props);
                  this.state =
                  width: 110
                  ;


                  render()
                  //width of child is 110
                  const width = `$100 / parseInt(this.state.width / 110)%`;
                  return (
                  <ScrollView>
                  <View style=styles.container onLayout=this.onLayout.bind(this)>
                  <View style=[styles.wrapper, width: width ]>
                  <View style=styles.box></View>
                  </View>
                  <View style=[styles.wrapper, width: width ]>
                  <View style=styles.box></View>
                  </View>
                  <View style=[styles.wrapper, width: width ]>
                  <View style=styles.box></View>
                  </View>
                  <View style=[styles.wrapper, width: width ]>
                  <View style=styles.box></View>
                  </View>
                  <View style=[styles.wrapper, width: width ]>
                  <View style=styles.box></View>
                  </View>
                  </View>
                  </ScrollView>
                  );


                  onLayout(e)
                  if (this.state.width !== e.nativeEvent.layout.width)
                  this.setState(
                  width: e.nativeEvent.layout.width
                  )




                  const styles = StyleSheet.create(
                  container:
                  flex: 1,
                  backgroundColor: '#fff',
                  alignItems: 'flex-start',
                  justifyContent: 'flex-start',
                  flexDirection: 'row',
                  flexWrap: 'wrap',
                  paddingTop: 40
                  ,
                  box:
                  width: 100,
                  height: 100,
                  backgroundColor: 'green',
                  ,
                  wrapper:
                  marginVertical: 10, alignItems: 'center'

                  );






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 7 at 10:50









                  Guruparan GiritharanGuruparan Giritharan

                  5201621




                  5201621



























                      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%2f55036364%2fhow-to-align-child-views-to-left-with-even-space-in-react-native%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

                      Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                      Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                      Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved