Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native - TextInput cursor placed in the middle instead of top in Android

I am rendering a TextInput in my react-native app which has a height of 100 px. The cursor for typing in the text is displayed in the center instead of the top in Android. The cursor appears fine for iOS. Below is a sketch which demonstrates the problem:

https://sketch.expo.io/SJVIk30ig

The TextInput is rendered using below code:

<TextInput style={{height: 100, padding: 5, backgroundColor: colorThemes.Blue.lightGrey}} 
          multiline={true}
          value={this.state.value}
          onChange={this.onTextInputChange}
          underlineColorAndroid={'transparent'}
          autoCorrect={true} autoFocus={true}
          autoCapitalize={'sentences'}
        />
like image 230
Varun Gupta Avatar asked Mar 21 '17 13:03

Varun Gupta


4 Answers

Duplication of this question

Set style property textAlignVertical to top on the TextInput

    <TextInput style={{height: 100, padding: 5, 
      backgroundColor: colorThemes.Blue.lightGrey, 
      textAlignVertical: 'top'}} 
      multiline={true}
      value={this.state.value}
      onChange={this.onTextInputChange}
      underlineColorAndroid={'transparent'}
      autoCorrect={true} autoFocus={true}
      autoCapitalize={'sentences'}
    />
like image 172
Varun Gupta Avatar answered Oct 17 '22 04:10

Varun Gupta


Just add below style to your InputText, please have a look to the code

// This is the style for InputText
        const styles = StyleSheet.create({
                inputBox: {
                    fontSize: 13,
                    color: '#404040',
                    // Setting up TextInput height as 50 pixel.
                    height: 150,
                    // Set border width.
                    borderWidth: 1,
                    // Set border Hex Color Code Here.
                    borderColor: '#404040',
                    // Set border Radius.
                    borderRadius: 5,
                    //Set background color of Text Input.
                    backgroundColor: "#FFFFFF",
                    // this for setting cursor to top
                    textAlignVertical: 'top'
                }
            });

    // here is the InputText , we are setting style



<TextInput
             multiline={true}
             style={styles.inputBox}
             placeholder={'Type here...'}
             value={this.state.text}
             onChangeText={newText => this._sendChanges(newText)}
                    />

enter image description here

like image 23
Lovekush Vishwakarma Avatar answered Oct 17 '22 05:10

Lovekush Vishwakarma


Add textAlignVertical: 'top' into your <TextInput> styles

like image 2
Lahiru Amarathunge Avatar answered Oct 17 '22 05:10

Lahiru Amarathunge


we can keep cursor position center using textAlign:'center'

inputstyle: {
    color: colors.white
    paddingLeft: 4,
    textAlign: 'center',
}
like image 1
Pradip Koravi Avatar answered Oct 17 '22 05:10

Pradip Koravi