I am currently using React-Native for an Android Project. I have been trying to make a TextInput field with an icon beside the field. However, for some reasons, I notice that the flexDirection: 'row' is not working if the TextInput is one of its child component. The whole view of the one that I apply the style will automatically disappear. This is a snippet of how my code looks like:
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<View style={{flexDirection: 'row'}}>
<Image
style={{height: 30, width: 30}}
source={require('./images/email-white.png')}
/>
<TextInput
style={{height: 40}}
underlineColorAndroid={'transparent'}
placeholder={'E-mail'}
placeholderTextColor={'white'}
onChangeText={(data) => this.setState({ username: data })} />
</View>
</View>
I also tried to wrap both component inside each individual view, but the problem still persists. Is there anyone who knows how to solve this? or anyone can confirm that this is a bug?
Your code with a small modification worked fine for me. The only thing I did was adding a width to the TextInput resulting in the icon being beside the text-input.
Working code:
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<View style={{flexDirection: 'row'}}>
<Image
style={{height: 30, width: 30}}
source={require('./images/email-white.png')}
/>
<TextInput
style={{height: 40, width: 300}}
underlineColorAndroid={'transparent'}
placeholder={'E-mail'}
placeholderTextColor={'white'}
onChangeText={(data) => this.setState({ username: data })} />
</View>
</View>
I had the same problem when I was building example code from the book Mastering React Native
The code had enclosed a TextInput field in a View with a flexDirection: 'row' and the child TextInput field was not accessible. Only the TextInput border was visible. After playing around with some of the suggestions on this page I found something that worked.
If the container view has a flexDirection: 'row'. Please make sure to add a flex: 1 into your textfield input. The image flex does not seem necessary. As soon as I added the flex: 1 to the styles.input sheet, the TextInput was accessible.
The following code works for me :
<View style={globalStyles.COMMON_STYLES.pageContainer}>
<View style={styles.search}>
<Image
style={{height: 30, width: 30}}
source={require('../../images/email-white.png')}/>
<TextInput
style={styles.input}
onChangeText={text => this.setState({ searchText: text})}
value={this.state.searchText}
placeholder={'Search'}
placeholderTextColor={globalStyles.MUTED_COLOR}/>
</View>
</View>
Local Styles :
const styles = StyleSheet.create({
input: {
flex: 1,
height: 35,
color: globalStyles.TEXT_COLOR,
borderColor: globalStyles.MUTED_COLOR,
backgroundColor: globalStyles.BG_COLOR
},
search: {
borderColor: globalStyles.MUTED_COLOR,
flexDirection: 'row',
alignItems: 'center',
borderRadius: 5,
borderWidth: 1,
// marginTop: 10,
// marginBottom: 5
}
})
Global Styles (styles/global)
export const COMMON_STYLES = StyleSheet.create({
pageContainer: {
backgroundColor: BG_COLOR,
flex: 1,
marginTop: 0,
paddingTop: 20,
marginBottom: 48,
marginHorizontal: 10,
paddingHorizontal: 0
},
text: {
color: TEXT_COLOR,
fontFamily: 'Helvetica Neue'
}
});
Hopefully this provides assistance to you guys. It took me a long time get something this simple working.
Try closing your Image tag...
<Image style={{width:30, height: 30}} source={require('./icon.png')} />
Also, add some dimensions to your TextInput...
<TextInput style={{height: 40, borderColor: 'gray', borderWidth: 1}} />
You might also need to set flex: 0 on the image and flex: 1 on the TextInput
Related
I am trying to design below layout in react native.
I am not able to draw horizontal line as shown above.
Some how I have drawn but margin on the right is not being applied
Code
<View style={{ flexDirection: 'row' }}>
<Text style={styles.text}>
Search
</Text>
<View style={styles.viewStyleForLine1}></View>
</View>
Style
viewStyleForLine1: {
borderWidth: 1,
borderColor: '#cc0000',
alignSelf: 'stretch',
marginLeft: 5,
marginRight: 5,
width:"100%"
},
More edit on centering the content
Here is how my View is
<View style={{ flexDirection: 'row' }}>
<Text style={styles.text}>
Search</Text>
<View style={styles.viewStyleForLine1}></View>
</View>
Style
text: {
marginTop: 16,
fontSize: 30,
fontWeight: 'bold',
color: '#cc0000',
marginLeft: 15
},
viewStyleForLine1: {
borderBottomWidth: 1,
borderColor: '#cc0000',
marginLeft: 5,
alignItems:"center",
marginRight: 15,
alignSelf:"center",
flex:1
},
What modification I should do?
you can change the style width: "100%" to "flex:1". you want the right view to occupy the rest of width, then the "flex:1" means the rest of width belongs to it.it has a weight. the percentage you have to use in both components. the more tips about UI adapter you can read this article
viewStyleForLine1: {
borderWidth: 1,
borderColor: '#cc0000',
alignSelf: 'stretch',
marginLeft: 5,
marginRight: 5,
flex:1
},
I have tried using a FlatList and a map function to generate a list of entries, but I am getting random lines between the entries (see the blue part of my screenshot)
What is causing these and how can I remove them?
Here is my FlatList:
<FlatList
data={TriageTransfertData[0].content[0].content}
renderItem={({ item }) => (
<View style={styles.subentryContainer}>
<View style={styles.ttEntryFirst} />
<View style={styles.ttEntryContentSub}>
<Text style={styles.ttTextSub}>{item.text}</Text>
</View>
</View>
)}
keyExtractor={item => item.text} />
And here is the styling:
subentryContainer: {
flexDirection: 'row',
width: '100%',
},
ttEntryFirst: {
width: '2%',
backgroundColor: Colors.Blue
},
ttEntryContentSub: {
width: '98%',
justifyContent: 'center',
paddingLeft: responsiveWidth(2),
paddingTop: 10,
paddingBottom: 10,
borderTopWidth: 1,
borderColor: Colors.GreyLight
},
And finally, here is a screenshot (notice the unwanted line in the blue area):
I think you're talking about ItemSeparatorComponent. It's rendered by default in a FlatList, but you can override it passing a custom component or a blank code function.
Here is the brief doc.
Hope this helps!
I'm having an issue with KeyboardAvoidingView on android. It works just fine with iOS. My screen has a view components on the top half of the screen that may or may not be impacting it. I've tried deleting each of those components but KeyboardAvoidingView only works when I delete the the component that takes up 60% of the screen. This component is a YouTube player from 'react-native-youtube' which has a flex: 1 style.
My code kind of looks like this..
<KeyboardAvoidingView
behavior="padding"
keyboardVerticalOffset={200}
>
<View style={styles.first}>
<View style={styles.second}>
<TouchableHighlight />
<View style={styles.third}>
<TextInput
style={styles.textInput}
maxLength={80}
autoGrow={true}
maxHeight={130}
multiline={true}
editable={true}
onChangeText={onChange}
value={descriptionText}
autoFocus={true}
keyboardType={'default'}
autoCorrect={false}
placeholder={'Send a word'}
underlineColorAndroid={white}
selectionColor={purple}
returnKeyType={'send'}
onSubmitEditing={handleSubmit}
/>
</View>
</View>
</View>
<KeyboardAvoidingView/>
first: {
flexDirection: 'column',
justifyContent: 'flex-start',
position: 'relative',
marginTop: 0,
},
second: {
width: screenWidth,
flexDirection: 'row',
alignItems: 'center',
paddingTop: 5,
paddingBottom: 9,
},
third: {
flexDirection: 'row',
borderTopLeftRadius: 10,
borderBottomLeftRadius: 10,
paddingBottom: 5,
paddingLeft: 10,
paddingRight: 10,
width: screenWidth - 102,
minHeight: 36,
alignItems: 'center',
},
textInput: {
textAlign: 'left',
fontSize: 14,
fontWeight: '400',
flex: 1,
padding: 0,
margin: 0,
}
Is there anything here that may impact KeyboardAvoidingView's functionality? I've tried removing behavior="padding" already due to React Native docs that mention that it works better on android when that prop is removed.
Thanks!
Try to add flex to all style might help, and maybe in the KeyboardAvoidingView also.
<KeyboardAvoidingView
style={{flex: 1}}
keyboardVerticalOffset={0}
behavior={'padding'}
>
Snack Expo: https://snack.expo.io/BJT-4Fphz
Working with the keyboard can be difficult.
However, Android (for once) does make it simpler.
import { Platform } from 'react-native';
const isAndroid = Platform.OS === 'android';
componentDidMount() {
if (isAndroid) {
SoftInputMode.set(SoftInputMode.ADJUST_PAN);
}
}
As I want to overlap this icon with two other Views, as shown in the image. this code is working on IOS platform but not on android. please suggest if there is any solution for Android.
var tabs = ['Activity', 'Files', 'People'];
this.state = {
tabs
};
return (
<Container style={{backgroundColor: '#F5F5F5'}}>
<View style={styles.topStrip}>
{
this.state.tabs.map((tab, index) => (
<View key={index} >
<TouchableOpacity>
<Text style={styles.streamName}>{tab}</Text>
</TouchableOpacity>
</View>
))
}
<View style={{position: 'absolute', backgroundColor: 'transparent', alignItems: 'center', justifyContent: 'center', zIndex: 5, elevation: 24, marginTop: 15, marginLeft: 300}}>
<EIcon size={40} color='#2196f3' name={'circle-with-plus'} />
</View>
</View>
<View style={{zIndex: -1}}></View>
</Container>
);
}
}
const styles = StyleSheet.create({
topStrip: {
alignItems: 'center',
},
streamName: {
marginTop: 7,
marginBottom: 6,
flexDirection: 'row',
alignSelf: 'center'
}
}
});
<View
style={{
paddingLeft: ITEM_HEIGHT * 3,
flex: 1,
position: "absolute",
top: 0,
right: 0,
bottom: 0,
left: 0,
zIndex: -1
}}
>
<Image
style={styles.centerCircle}
resizeMode="contain"
source={Images.ballSmall}
/>
</View>
In android, to use zIndex you need css with position-properties with top,left,right,bottom css
Tested On: Zenfone 3, Android 7, React Native 0.55.4
This issue is not caused by zIndex (although it does cause problem on Android).
Your icon is put inside the upper container view. React Native on Android does not support overflow: visible. That's why you see your icon being clipped outside the container.
You may put the icon on the "page" component level to solve this issue.
You may also refer to https://stackoverflow.com/a/50991892/6025730.
can't display floating button over webview on android.
ios work fine, on android doesn't show.
render function:
<View style={{ flex: 1}}>
<View style={{position:'absolute',right:0,marginTop:90,marginRight:10,zIndex:1,height:50,width:50}}>
<TouchableHighlight style={styles.addButton}
underlayColor='#ff7043' onPress={()=>{console.log('pressed')}}>
<Text style={{fontSize: 50, color: 'black'}}>+</Text>
</TouchableHighlight>
</View>
<WebView
source={require('./index.html')}
style={{marginTop:0}}
scalesPageToFit={Platform.OS !== 'ios'}
onMessage={this.onMessage}
postMessage={"Hello from RN"}
/>
</View>
css:
const styles = StyleSheet.create({
addButton: {
backgroundColor: '#ff5722',
borderColor: '#ff5722',
borderWidth: 1,
height: 100,
width: 100,
borderRadius: 50,
alignItems: 'center',
justifyContent: 'center',
position: 'absolute',
bottom: 20,
right:20,
shadowColor: "#000000",
shadowOpacity: 0.8,
shadowRadius: 2,
shadowOffset: {
height: 1,
width: 0
}
}
});
How can I display a button over web view on android ?
The solution is pretty simple, put the Button View code under/after the WebView code
<View style={{ flex: 1}}>
<WebView
source={require('./index.html')}
style={{marginTop:0}}
scalesPageToFit={Platform.OS !== 'ios'}
onMessage={this.onMessage}
postMessage={"Hello from RN"}
/>
<View style={{position:'absolute',right:0,marginTop:90,marginRight:10,zIndex:1,height:50,width:50}}>
<TouchableHighlight style={styles.addButton}
underlayColor='#ff7043' onPress={()=>{console.log('pressed')}}>
<Text style={{fontSize: 50, color: 'black'}}>+</Text>
</TouchableHighlight>
</View>
</View>
If for some reason you can't put it under the WebView, adding a elevation style prop higher than the WebView's, alongside the zIndex style prop will work for you.
That's because the zIndex property is quite broken on Android (see the dedicated issues). To compensate for it, you must invert the rendering order of your container's children.
This can be done either by rendering the button after the WebView:
<View style={{ flex: 1 }}>
<WebView />
<View style={{ styles.button }} />
</View>
Or by reversing the display order in your container, using flexDirection:
<View style={{ flex: 1, flexDirection: 'column-reverse' }}>
<View style={{ styles.button }} />
<WebView />
</View>
Note that you can also use the android-specific elevation API. This will act as a reliable z-index and is the only way to display a shadow on Android.