What to do about diffrent screen sizes - react-native

I am learning react native for a few weeks and I made a screen and it looks good on my android emulator but when I look at it on my android phone somethings are out of place like icons or borders. I think It is about diffrent sizes of screens. Any idea how to fix this?
https://i.stack.imgur.com/rzhYn.jpg
https://i.stack.imgur.com/mhU2R.png

Yes, It could be.
You can avoid this in multiple ways.
Use dimensions, and get width and height based on the phone
https://reactnative.dev/docs/dimensions
or
https://www.npmjs.com/package/react-native-responsive-screen
For font size you can use this:
https://www.npmjs.com/package/react-native-responsive-fontsize

You have to make sure a few things while developing a component or when you style it:
Try to avoid fixed width/height or placement. Let the content grow the height of the box.
Either use flex or percentage width.
In your example, what I can see is the icon is going over the boundary of the box. To give a simple example, if you want to show a text on the left and say image on the right, use styles like this:
<View style={{display: 'flex', flexDirection: 'row', justifyContent:'space-between'}}>
<Text>Hi</Text>
<Image/>
</View>

Related

Is there a way to set an absolute positioned component's width based on the width of its flex-boxed sibling in React Native?

I'm trying to build this:
Right now I'm building this by doing all the text in a flex box and having two sibling absolutely positioned boxes with partial borders to make the lines, like this:
<ContainerView>
<TextSection />
<LeftFloat />
<RightFloat />
</View>
This isn't great on different screen sizes, though, because I have to tell those border boxes how to tall and high to be. And the text takes up different heights/widths on different screen sizes so there is no one-size-fits-all value, even when it's a screen percentage, that works for the borders on all devices.
I tried doing something inline like this:
<Text>BECOME A SUBSCRIBER</Text><HorizontalLine />
<VerticalLine /> <BulletView /> <VerticalLine />
<HorizontalLine /><Text>Tap here to learn more ></Text>
but that was worse because the horizontal and vertical lines wouldn't join up nicely to form a corner. I could manipulate them with negative margins but the effect was different between Android and iOS.
Is there a better way to build this kind of border situation?
Or is there a way for these floating boxes to find the width of the "BECOME A SUBSCRIBER" and "Tap here to learn more" elements so they can set their widths accordingly?
The View prop 'onLayout' would be helpful here. You can let the other container expand with flex, and then grab the dimensions of it. Afterwards you could calculate the size you want using screen dimensions and this layout data.
<View onLayout={e => e.nativeEvent.layout}

How to set backgroundColor like gradient?

I just can set basic color which is static, doesnt change.
Also how can I do the borderRadius part, since i made two different View's.
On here, i just use this code for the color
<View style={{ paddingHorizontal: 20, backgroundColor: "#3DB4EE" }}>
Here is how my app looks:
And here is how i want it to be seen:
There's an easy way and a hard way:
Hard Way:
Use radial-gradient CSS property, here's an example: is it possible to do a curved line with css gradient?
Easy Way:
Get an Image designed with the same background and set as background-image
It's not possible without adding library and there for you have to use https://github.com/react-native-linear-gradient/react-native-linear-gradient
For border radius of white background you have to use style property borderTopLeftRadius and borderTopRightRadius

How is the component view modified when keyboard is showing?

Good morning, this is actually my first question on StackOverflow after a few years in the industry (hope it's not too late!).
I'm working as a side project on a react native app.
I fail to understand how does the view change when the keyboard is up.
When the view shrinks (because the keyboard was shown), views overlap one to another. I want them to still be split.
I have this picture showing what I mean with overlapping views:
I thought it might be something with the paddings/margins, but I removed them all and still the same issue. Now, I know that the input height is defined by an absolute number, and that is what makes it not shrink, so it is bigger than the actual view, that's why it overlaps.
How can I keep my input having a fixed height but also maintaining a margin of separation when the view shrinks ??
Thank you very much!
Edit: I'm editing because I feel like I haven't been able to express my idea of how I need it to work. (Should I delete previous explanation? Keep picture if someone edits and deletes the first explanation).
The initial view with no keyboard has to be like this:
The inputs and buttons have to be at the bottom, the view getting all the height possible.
When opening the keyboard (by clicking on one of the inputs), I want the list to shrink so that the buttons and inputs are still visible and separated by a small margin/padding, and the list to have taken the remaining space (again respecting a small separation between views). The list will still be scrollable (it is a FlatList) when opening the keyboard.
Thank you again and sorry for the misexplanation.
You can try wrapping your content inside a ScrollView instead of a normal View like this:
<ScrollView style={{flex: 1}}>/* your content here */</ScrollView>
So when the keyboard shows up your elements won't overlap since they take the same space but you can scroll to see the elements hidden by the keyboard.
I think you can try something like this:
<View>
<ScrollView>
<KeyboardAvoidingView style={{flex:1}} behavior={Platform.select({ios:"padding", android:null })} keyboardVerticalOffset={Platform.select({ios: 0, android: 500})}>
{/*Your buttons and other elements here...*/}
</KeyboardAvoidingView>
</ScrollView>
<View>
You will also have to import KeyboardAvoidingView and ScrollView from react-native for this.

Overlaying views in React Native

Im trying to make a dropdown similar to the image below and im wondering if such a thing is possible. That is because:
overflow defaults to hidden on android and that since zIndex doesnt work all that well.
zIndex doenst work all that well, views need to be in the proper order in order to draw over others normally
The only scenario that i can think of to make this work right now is to use something like onLayout and manually calculate the position of where it is that the dropdown needs to display and then render it at the top level in absolute positioning. I worry this might look rather hybrid-y but havent tried it yet.
Other ideas?
You can definitely use zIndex to achieve this behavior, but zIndex has to be applied to adjacent views to work cross platform:
render(){
return (
<View style={{ flex: 1, alignItems: 'flex-start' }}>
<View style={{ zIndex: 2 }}>
<YourDropdownButton />
</View>
<View style={{ zIndex: 1 }}>
<YourNormalButton />
</View>
</View>
)
}
Here is a snack showcasing this with my own custom drop down
Why wouldn't you use the native dropdown, it renders correctly and works as intended on all platforms?
Either way, you can always position it absolutely yes. I've done something like this and it works, but you can have some glitches if the list gets longer and it needs to scroll. Also, you would have to implement proper closing (clicking outside, and on the main element, closing after choosing the options. etc).

React Native justifyContent no effect

Figure, why setting the justifyContent property does not work?
When you are in a container that has the flex direction set to 'row' then justify-content actually sets the location horizontally instead of vertically. So in this case you would use alignItems. Since the RN implementation of flexbox is pretty close to the standard baring a few naming differences ('flexDirection' instead of 'flex-direction', etc) I would recommend working your way through the tutorial at http://flexboxfroggy.com/. Level 10 actually shows an example of the issue you are encountering here.
try to add alignItems: 'center'