Is there a way in React Native to position text similar to the example I provided?

i am struggling to place text in the format shown below:

{item.Sampling_Request_ID}   {item.Facility_Name}

{item.Sampling_Due_Date}     {item.Water_Source_Name}

i have attempted to align the text as indicated in the example, but I am facing difficulties and need guidance on how to achieve it. the desired text format is illustrated in the example provided.

here is the method I am using to try and achieve this:

function renderItem({ item, index }) {
    let name = '';

    if (item.Facility_Name) {
      name = item.Consumer_Name;
    } else {
      name = item.Consumer_Name;
    }
    return (
      <TouchableOpacity
        style={{
          flex: 1,
          paddingVertical: 15,
          paddingHorizontal: 10,
          flexDirection: 'row',
          justifyContent: 'space-between',
          alignItems: 'center',
          borderWidth: 1,
          borderColor: 'white',
          backgroundColor: index % 2 == 0 ? '#dde6eb' : '#d1f0da',
        }}
        onPress={() => selectItem(data)}
      >
        <View
          style={{
            flex: 1,
            alignItems: 'flex-start',
          }}
        >
          <Text style={styles.lightText}>
           {item.Facility_Name}
          </Text>
        </View>
        <View
          style={{
            alignItems: 'flex-end',
          }}
        >
          <Text style={styles.lightText}>{item.Sampling_Request_ID}</Text>
        </View>
         <View
          style={{
            flex: 1,
          }}
        >
          <Text style={styles.lightText}>{item.Water_Source_Name}</Text>
        </View>
<View
          style={{
            flex: 1,
          }}
        >
          <Text style={styles.lightText}>{item.Sampling_Due_Date}</Text>
        </View>
     
      </TouchableOpacity>
    );
  }

Answer №1

Give this a try using the following method. Hopefully, it will do the trick.

 <TouchableOpacity
    style={{flex: 1}}
    onPress={onPress}
 >
    <View style = {{ width: '100%' }}>
      <View style = {{ flex: 1, flexDirection: 'row', justifyContent: 'space-between'}}>
        <Text>{item.Sampling_Request_ID}</Text>
        <Text>{item.Facility_Name}</Text>
      </View>
      <View style = {{ flex: 1, flexDirection: 'row', justifyContent: 'space-between'}}>
        <Text>{item.Sampling_Due_Date}<</Text>
        <Text>{item.Water_Source_Name}</Text>
      </View>
     </View>
 </TouchableOpacity>

Answer №2

Give this a try

<TouchableOpacity
        style={{
          flex: 1,
          paddingVertical: 15,
          paddingHorizontal: 10,
          // flexDirection: 'row',
          // justifyContent: 'space-between',
          alignItems: 'center',
          borderWidth: 1,
          borderColor: 'white',
          backgroundColor: index % 2 == 0 ? '#dde6eb' : '#d1f0da',
        }}
        onPress={() => selectItem(data)}
      >
        <View
          style={{
            flex: 1,
            flexDirection: 'row',
            justifyContent: 'space-between'
          }}
        >
          <Text style={styles.lightText}>
           {item.Sampling_Request_ID}
          </Text>
          <Text style={styles.lightText}>{item.Facility_Name}</Text>
        </View>
        <View
          style={{
            flex: 1,
            flexDirection: 'row',
            justifyContent: 'space-between'
          }}
        >
          <Text style={styles.lightText}>
           {item.Sampling_Due_Date}
          </Text>
          <Text style={styles.lightText}>{item.Water_Source_Name}</Text>
        </View>
       
     
      </TouchableOpacity>

Answer №3

To enhance the user experience, consider grouping Sampling_Request_ID and Facility_Name in a single parent View with flexDirection set to row. Similarly, group Sampling_Due_Date and Water_Source_Name in their own View as well. The code snippet below demonstrates how this can be implemented:

function displayItem({ item, index }) {
  let name = "";

  if (item.Facility_Name) {
    name = item.Consumer_Name;
  } else {
    name = item.Consumer_Name;
  }
  return (
    <TouchableOpacity
      style={{
        flex: 1,
        paddingVertical: 15,
        paddingHorizontal: 10,
        //flexDirection: 'row',
        justifyContent: "space-between",
        alignItems: "center",
        borderWidth: 1,
        borderColor: "white",
        backgroundColor: index % 2 == 0 ? "#dde6eb" : "#d1f0da",
      }}
      onPress={() => selectItem(data)}
    >
      <View
        style={{
          flex: 1,
          flexDirection: "row",
          alignItems: "center",
          justifyContent: "center",
        }}
      >
        <View
          style={{
          }}
        >
          <Text style={styles.lightText}>{item.Sampling_Request_ID}</Text>
        </View>
        <View
          style={{
            flex: 1,
          }}
        >
          <Text style={styles.lightText}>{item.Facility_Name}</Text>
        </View>
      </View>
      <View
        style={{
          flex: 1,
          flexDirection: "row",
          alignItems: "center",
          justifyContent: "center",
        }}
      >
        <View
          style={{
            flex: 1,
          }}
        >
          <Text style={styles.lightText}>{item.Sampling_Due_Date}</Text>
        </View>
        <View
          style={{
            flex: 1,
          }}
        >
          <Text style={styles.lightText}>{item.Water_Source_Name}</Text>
        </View>
      </View>
    </TouchableOpacity>
  );
}

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

The behavior of JavaScript replace varies between Chrome and IE

This JavaScript code successfully replaces a string in Chrome: myUrl = someUrl.replace('%2C%7B%22itn%22%3A%5B%22%20guidelines%20%22%5D%7D', ''); However, when tested in Internet Explorer, it does not replace the string as expected. T ...

Incorporate CSS styling from a separate .css file into a material component

Just starting out with material UI. I have a css file that looks like this: .bgItem { font-size: 14px; } My component setup is as follows: <MenuItem key={status.Id} value={status.Value} classes={css.bgItem}> {status.Description} </Men ...

The fuse-sidebar elements are not being properly highlighted by Introjs

I have recently developed an angular project that utilizes the fuse-sidebar component. Additionally, I am incorporating introjs into the project. While introjs is functioning properly, it does not highlight elements contained within the fuse-sidebar. The ...

Toggling different <div> sections with button clicks

I have a specific objective that I am working towards. For more information, please visit this page: Located at the bottom right corner is a <div> which contains two buttons on either side. Currently, this <div> houses a single Google chart. ...

Utilizing Multiple Checkboxes for Precision Search Refinement

Big thanks to Khalid Ali for the support provided up until now. I am currently working with an array of songs that each have descriptions, keywords, etc. I have a set of checkboxes that I want to use to refine a search. Essentially, if someone selects the ...

Developing a buffer entity from a Javascript/Nodejs fetch response

Struggling with creating a buffer object to manipulate/parse through JSON response fetched via API. My fetch code is functional: const fetch = require("node-fetch"); const asset = 'Build_Artifacts' const url2 = 'http://localhost:8081/se ...

Using React and Redux to update a property in the state with a modified array

I am currently developing a React app and utilizing Redux for state management. Below is the code snippet I am working with: menu.actions.js: import { apiUrl, apiConfig } from '../../util/api'; import { ADD_CATEGORY, GET_MENU } from './men ...

Incorporating rounded corners to an embedded YouTube video

Check out this JSFiddle link. The border-radius property appears to be working correctly at first, but then the rounded corners suddenly disappear shortly after loading. Is there a way to apply rounded corners to YouTube videos that are embedded on a webp ...

The connection between Mongoose and the nodejs app cannot be established

I've been attempting to integrate Mongoose with my Node.js environment, but I'm encountering an unexpected error. Previously, I was able to connect with Mongoose using the same commands, but for the past few days, it's been throwing errors. ...

Angular.js can dynamically add a class to an element based on the current page being viewed

Currently, my goal is to assign the class post to my div with id #wrap when I am on the page /post. Here's what I have so far: $routeProvider when('/post', { templateUrl : 'views/post.php', controller : 'postCtrl&ap ...

Exploring AngularJS: Leveraging ng-model within a custom directive featuring iterations and dynamically generated HTML elements

Trying to implement a directive for a grid, I encountered an issue where passing in a column definition that includes an HTML control with ng-model and ng-click directives resulted in an error: "Error: [$rootScope:infdig] 10 $digest() iterations reached. ...

Caution: Attempting to access a non-existent 'sequelize' property within a circular dependency in the module exports

Issue Nodemon server.js [nodemon] 2.0.15 [nodemon] to restart at any time, enter `rs` [nodemon] watching path(s): *.* [nodemon] watching extensions: js,mjs,json [nodemon] starting `node server.js` Warning: connect.session() MemoryStore is not designe ...

The polygon array feature is not functioning as expected in the Google Maps API

I'm currently facing an issue with a code that is supposed to draw polygons from an array in the Google Maps API, but unfortunately, it doesn't seem to be working as expected. <!DOCTYPE html> <html> <head> <script src ...

What is the best way to make a CSS class appear in my javascript using the method I have been using before?

I am facing an issue in making this code work properly. It functions correctly for my caption element as there is only one caption tag in my HTML. However, the same code does not work for my TR element since it requires a for loop to iterate through multip ...

Customized progress bar for monitoring lengthy JavaScript calculations

I am currently working on a project that involves using javascript with jquery and bootstrap. My main objective is to have a visually appealing progress bar displayed during heavy javascript computation. Despite knowing the exact progress state of the comp ...

issue with excessive content overflow

I'm working with a div structure where the outer div has the following CSS properties: position: relative; overflow: hidden; min-heigth: 450px; Inside this outer div, there is another div with the following CSS properties: position: absolute; top: ...

Customizing particle textures in Three.js Particle System for unique visual effects

I have a scene in Three.js with 1000 particles and I've loaded 2 different textures. var particleCount = 1000; var texture1 = THREE.ImageUtils.loadTexture( 'texture1.png' ); var texture2 = THREE.ImageUtils.loadTexture( 'texture2.png&ap ...

Creating a website using the Hugo Tikva theme and Bootstrap-4 for a seamless and modern design

Embarking on a complete overhaul of my company's website, I've decided to utilize the Hugo framework and Bootstrap for the revamp. While web development isn't my expertise, I'm in need of guidance on how to effectively leverage a Hugo t ...

Developing asynchronous AJAX requests while an alert popup is displayed

My browser is set to send keep-alive asynchronous calls to the server every 20 seconds using window.setTimeout(). The issue arises when an alert() message appears, causing the AJAX calls to pause. If the user delays dismissing the popup, it may lead to the ...

Utilizing State as a Prop传传传

I am trying to display a button in some child components while hiding it in others. I attempted to use useState in the parent component but was unable to pass it to the child components. Is there a way to achieve this? const [showRepo, setShowRepo] = u ...