Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button in TextInput in react native

Tags:

react-native

How can I insert and style a button in text input in react native like this: enter image description here

Can I use any code like this?

<Textinput>
   <Button></Button>
</Textinput>
like image 903
Hossein Avatar asked Jan 25 '17 23:01

Hossein


People also ask

How do I create a button in React Native?

import { View, Button, StyleSheet, TouchableOpacity, Text } from "react-native"; To create custom buttons, you need to customize the <TouchableOpacity /> component and include the <Text /> component inside of it to display the button text.

How do you Unfocus TextInput React Native?

To unfocus a TextInput in React Native, we can use the Keyboard. dismiss method. to set the onSubmitEditing prop to Keyboard. dismiss .

How do I add an icon to TextInput react?

To put an icon inside a TextInput in React Native, we can use the TextInput component in the react-native-paper module. <TextInput label="Password" secureTextEntry right={<TextInput. Icon name="eye" />} />; We set the right prop to the Icon to show the eye icon on the right of the input.


3 Answers

Sorry for the delay, but something like this should work:

<View style={{flexDirection:'row', width: window.width, margin: 10, padding:4, alignItems:'center', justifyContent:'center', borderWidth:4, borderColor:'#888, borderRadius:10, backgroundColor:'#fff'}}>
  <View style={{flex:4}}>
    <TextInput
        onChangeText = {(textEntry) => {this.setState({searchText: textEntry})}}
        style={{backgroundColor:'transparent'}}
        onSubmitEditing = {()=>{this.onSubmit(this.state.searchText)}}
      />
  </View>
  <View style={{flex:1}}>
    <Button onPress={ () => this.onSubmit(this.state.searchText) }>
        <Image source={ require('../images/searchImage.png') } style={ { width: 50, height: 50 } } />
    </Button>
  </View>
</View>

where you adjust the size based on your image and Button is imported like:

import Button from '../components/Button';

I like to keep the button in an external folder, where it is like:

import React, { Component } from 'react';
import { Text, TouchableOpacity } from 'react-native';

class Button extends Component {
  handlePress(e) {
    if (this.props.onPress) {
        this.props.onPress(e);
    }
  }
  render() {
    return (
        <TouchableOpacity
            onPress={ this.handlePress.bind(this) }
            style={ this.props.style } >
            <Text>{ this.props.children }</Text>
        </TouchableOpacity>
    );
  }
}
export default Button;

Good luck!

like image 195
kwishnu Avatar answered Nov 07 '22 08:11

kwishnu


wrapping both in a View with flexDirection:row should get you there.

If you want to get more advanced, you could look at the react-native-textinput-effects package that will give you some very nicely styled inputs.

like image 26
Talor A Avatar answered Nov 07 '22 09:11

Talor A


<View style={{flexDirection:'row'}}>
    <View>
      <TextInput
          style={{alignItems:'center',justifyContent:'center',backgroundColor:'white'}}
          value = {this.state.searchString}
          onChangeText = {(searchString) => {this.setState({searchString})}}
          placeholder = 'Search'
          keyboardType = 'web-search'
          onSubmitEditing = {()=>{this._fetchResults()}}
          ref = 'searchBar'
          />
    </View>
    <TouchableHighlight style={{alignItems:'center',justifyContent:'center'}} onPress = {()=>{this._fetchResults()}} underlayColor = 'transparent'>
        <View>
          <Icon name="search" size = {20} color = "#4285F4" />
        </View>
    </TouchableHighlight>
</View>

if you are not using react-native-vector-icons replace icon with .png magnifying glass image

like image 36
Vikram Belde Avatar answered Nov 07 '22 09:11

Vikram Belde