What is the best way to keep a button at the bottom of a page in React Native while also preventing it from being obscured

Is there a way to ensure that the button stays fixed at the bottom of the screen, even when the keyboard is open?

Currently, I am facing an issue where the button moves up along with the keyboard.

I attempted using

position: "absolute", bottom: "0"
for the button element, but it did not have any impact on the button's position.

Below is the code snippet:

 return (
    <KeyboardAvoidingView
      behavior="padding"
      style={styles.container}
    >
      <StatusBar style="light" />
      <View style={styles.inputContainer}>
        <TextInput
          placeholder="First name (required)"
          onChangeText={(name) =>
            setFirstName((prev) => name)
          }
          style={styles.input}
        />
        <TextInput
          placeholder="Last name (optional)"
          onChangeText={(lastname) =>
            setFirstName((prev) => lastname)
          }
          style={styles.input}
        />
      </View>
      <Button
        onPress={() => null}
        title="Next"
        buttonStyle={styles.button}
      />
    </KeyboardAvoidingView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "space-between",
  },
  inputContainer: {
    width: 350,
    top: "20%",
    alignSelf: "center",
  },
  input: {
    borderBottomWidth: 3,
    height: 50,
  },
  button: {
    backgroundColor: Colors.blue,
    width: 300,
    marginBottom: 80,
    height: 50,
    alignSelf: "center",
  },
});

Here is how the layout appears when the keyboard is opened:

https://i.sstatic.net/4HKGvm.jpg

https://i.sstatic.net/O1SOZm.jpg

Answer №1

There is a difference in the behavior of the avoiding view between the two platforms.

<KeyboardAvoidingView
          behavior= {Platform.OS === "ios"? "padding":"height"}
          style={styles.container}
        >
          <StatusBar style="light" />
          <View style={styles.inputContainer}>
            <TextInput
              placeholder="First name (required)"
              onChangeText={(name) =>
                setFirstName((prev) => name)
              }
              style={styles.input}
            />
            <TextInput
              placeholder="Last name (optional)"
              onChangeText={(lastname) =>
                setFirstName((prev) => lastname)
              }
              style={styles.input}
            />
          </View>
          <Button
            onPress={() => null}
            title="Next"
            buttonStyle={styles.button}
          />
 </KeyboardAvoidingView>

I believe this solution should address your issue.

If you require more control over keyboard behavior, you can programmatically modify the default behavior using a specified package.

React-Native-Android-Keyboard-Adjust - This package has been beneficial in various instances and is particularly effective.

Note: Please be aware that this package is compatible with Android devices only.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Determining the screen width of mobile devices across the board

While using the Google Chrome inspector to simulate the Galaxy S5 (360px) screen, I am encountering issues with detecting the correct screen width. The CSS for 360px is being overridden by the 768px CSS instead. Is there a more effective solution to this ...

The button is not properly aligned at the center of the flex container

My attempt to center a button in a fluid container with 100% vertical height and provide it with an offset from the bottom of the screen is almost successful, but the button is not perfectly centered - it is offset from the left. You can view the example i ...

Divide the table header column "th" into two separate columns

Can someone assist me in achieving the output displayed in the image? I want to separate the header th into two lines like the blue line shown. I need two headers for a single td column. Please help, thank you. https://i.sstatic.net/SwILH.png <style& ...

Problem with Django development server not correctly processing mimetype for CSS files

Here, I am using python 3.3.5 and django 1.6.4. <link rel="stylesheet" href="css/style.css" type="text/css"> While running the django development server (manage.py runserver), the above css file is being served as text/plain which is causing the st ...

Creating a website layout using DIV elements that adjusts the background and buttons based on the screen

I'm currently tackling a straightforward HTML page but facing some challenges with the divs. The goal is to have a fullscreen background with four horizontal buttons aligned next to each other at the bottom. These buttons are currently linked to the b ...

Is anyone else experiencing issues with their imagemap due to Twitter Bootstrap?

I'm excited to ask my first question here as a beginner. I am truly grateful for all the assistance and guidance I have received from this site. I hope that the questions I ask can also benefit others in some way :) Although imagemaps may not be used ...

Guide to adding a new font to your Ember project

I am currently in the process of developing a website utilizing the MEEN Stack framework (Mongo, Ember, Express, Node). One challenge I am encountering is importing the Lato font family into my project to serve as the main font for the entire site. Despite ...

Ensure the box is perfectly centered vertically within the container, stopping once it reaches a certain threshold

Check out this diagram: https://i.sstatic.net/tGZAR.png I'm working on creating a web page with a banner (the grey box) that has a height of 80vh, if possible. This banner includes a div with text inside (the blue box) that needs to be vertically c ...

Navigation bar with a full-page slider

I am trying to incorporate my bootstrap nav bar into a full-width slider, similar to the layout in this example. https://i.sstatic.net/LE9wP.jpg This is the full width slider http://codepen.io/grovesdm/pen/MazqzQ Currently, I have positioned the nav ba ...

Exclude cascading classes in CSS

Help Needed with HTML Lists! <li>A.</li> <li><a href="#">B.</a></li> <li><a class=tr href="#">C.</a></li> <li class=tr>D.</li> <li class=notr>E.</li> I am trying to selec ...

Tips for ensuring that your sidebar remains contained within the boundaries of the outer div

Here is my Vue code utilizing bootstrap-vue. I am attempting to place the sidebar within the card body, but it's not displaying as intended. How can I adjust it to fit properly inside either the outer div or the b-card body? <template> <di ...

The MUI text input is failing to span the full width of the input field; it abruptly stops halfway through

It's difficult to articulate, but here is an image illustrating the issue: here the input stops The developer tools are displaying:edge dev tools I am using the Material UI Input component with React, and for some unknown reason, the text cursor hal ...

Creating personalized columns in a BootstrapVue table

I'm having an issue with the b-table component in bootstrap-vue. The array containing my items is structured like this: [{ id: 1, name: "Test", phone: 555-111-666, address: "Some Address", //etc... }] I have a couple of question ...

Select a specific element to apply a CSS selector for a button

One of the challenges I am facing involves a webpage with multiple submit buttons. My goal is to iterate through each button and click on them one by one. While I am aware that this can be achieved using xpath like this (//button[@class='submit' ...

Include a unique identifier to a file name, and then update any mentions of it using Gulp

I am trying to automate the process of renaming a CSS file and updating the reference in my PHP file. Currently, I have a file named base.css and a PHP file called styles.php containing the following code: wp_enqueue_script('base', get_template_ ...

Error Encountered when Employing react-redux connect in react-native Development

I have experience using redux with react-dom, but I am facing challenges integrating it into my react-native application. When adding the connect HOC to my component in a simple boilerplate react-native app, I encounter the following error: https://i.sst ...

I am experiencing an issue where textboxes are disappearing from my application after it has been

After compiling and running my web app in the browser, the textboxes appear perfectly fine. However, once I publish it, the textboxes become invisible. I suspect it's a CSS error since the textboxes still exist, but their background blends into the ov ...

The autocomplete selection box is not appearing correctly

I am attempting to implement the jquery-ui autocomplete combobox in a .Net Core 2.2 web application, but I seem to be encountering a CSS issue. Here is how it currently appears: https://i.sstatic.net/wXaBg.png In addition to the jquery-ui CSS, I am also u ...

What could be the reason why the toggle active function is not functioning as expected in this code

I need some guidance on how to properly share code on stackoverflow. For the complete code, you can view it on Codepen: Codepen const hamburger = document.querySelector(".hamburger"); const navMenu = document.querySelector(" ...

Trigger parent element based on child hover

Imagine having a structure like this: <div class="bigWrap"> <div class="element"> ... <div class="HoverThisElement"></div> ... </div> ...