How can I stop horizontal scrolling in React Native Android WebView?

I'm encountering an issue with webview and scrolling on different devices. I have managed to disable scrolling using the scrollEnabled property for IOS, but unfortunately, this does not work for Android. Despite trying various scripts, none of them seem to solve the problem.

Initially, everything appears fine, but when a user swipes to the right side, the text/div moves off-screen.

https://i.sstatic.net/xonC5.png

This is what occurs:

https://i.sstatic.net/7v88V.png

This is my current webview setup:

<WebView
  bounces={false}
  ref={(element) => { this.webViewRef = element; }}
  javaScriptEnabled
  scalesPageToFit={false}
  onShouldStartLoadWithRequest={this._onShouldStartLoadWithRequest}
  style={{
    height: this.state.webViewHeight,
  }}
  scrollEnabled={false}
  source={{
    html: `
          <!DOCTYPE html>
          <html>
            <head>${injectedStyle(platformStyles.fontSizes.base || 16, webViewWidth)}</head>
            <body>
              <div id="baseDiv" class="ex1">${content}</div>
              ${injectedJS}
            </body>
          </html>
    `,
    baseUrl: 'http://s3.eu-central-1.amazonaws.com',
  }}
  onNavigationStateChange={this._processNavigationStateChange}
  automaticallyAdjustContentInsets={false}
/>

I also attempted to use CSS styles such as overflow: 'hidden', but it did not resolve the issue:

const injectedStyle = (fontSize, widhtPlatform) => `<style type="text/css">
    ...
    body {
            display: flex;
            overflow: hidden;
            background-color: orange;
    ...
        }
    div.ex1 {
      overflow: hidden;
  background-color: coral;
  border: 1px solid blue;
    ...
    }
    </style>`;

To further attempt disabling scroll, I included JS code:

const injectedJS = `
  <script>
    var scrollEventHandler = function()
      {
        window.scroll(0, window.pageYOffset)
      }
    document.addEventListener('scroll', scrollEventHandler, false);
    document.getElementById('scrollbar').style.display = 'block';
    document.body.style.overflow = 'hidden';

  </script>
`;

We are currently using RN:

react-native-cli: 2.0.1

react-native: 0.53.3

Answer №1

To make adjustments for Android, switch scalesPageToFit={false} to scalesPageToFit={true}. This modification should resolve the issue.

// ....
const scalesPageToFit = Platform.OS === 'android';

// .... 
return 
    <WebView
      scalesPageToFit={scalesPageToFit}
      bounces={false}
      scrollEnabled={false}
// ....

Update: With recent versions of rn-webview, depending on the html content being used, it might be necessary to include additional CSS targeting content that is causing problems:

max-width: 100%;

overflow-x: hidden;

and/or

showsHorizontalScrollIndicator={false}

Answer №2

npm install react-native-render-html@foundry 

import HTML from 'react-native-render-html'

 <ScrollView style={{ flex: 1 }} showsVerticalScrollIndicator={false}>
      <HTML 
       contentWidth={Dimensions.get('window').width}
       onLinkPress={(e,href)=>onShouldStartLoadWithRequest(e,href)}
        html={'<p>insert your own HTML code here</p>' }
        tagsStyles={ { p: { textAlign: 'center', fontStyle: 'normal', color: 'black' 
        ,fontFamily:'Mukta-Regular',fontWeight:'100'} }} />
 </ScrollView>

Answer №3

Here's a handy trick to hide the scrollbar:

To remove the Horizontal scrollbar, use:

showsHorizontalScrollIndicator={false}

To eliminate the Vertical scrollbar, use:

showsVerticalScrollIndicator={false}

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

Issue with React Native WebView functionality on Android devices running version 10 and above

Issues with React Native WebView on Android 10 and above have been observed. While it functions correctly in development mode, it only displays a white screen in production mode. <WebView ref={webView} javaScriptEnabled injectedJavaScript={initi ...

Transform this color matching game into an image matching game using JavaScript and jQuery

I have a color matching game that I would like to enhance by matching background-images instead of just background-colors. However, I am facing difficulties in making this change. For instance, instead of matching the color red with the text "red," I wan ...

Customizing the css for a certain category

On my homepage, I display categories with unique labels and icons. Each category is assigned a specific color for its label/icon. Typically, setting unique colors in HTML is straightforward: <!-- ngRepeat: category in categories | orderBy:'displ ...

I am having trouble with node.js express not recognizing the path to my CSS files

My objective is to load information onto an HTML page using Node.js and Express. The issue I am facing is that when I try to open the main page (which displays all the books from the database), everything works smoothly - the CSS and JS files are located ...

Replace Picture as you Scroll

Is it possible to swap images based on scroll position? For instance: When scrolling 100px, display img1.jpg When scrolling 200px, display img2.jpg When scrolling 300px, display img3.jpg Here's an example of what I'm looking for. Any suggest ...

What is the best way to conceal just the scrollbar thumb in a Bootstrap modal?

When opening a modal in Bootstrap according to the documentation, it only hides the scrollbar thumb, like shown in the examples below: However, my modal hides the entire scrollbar. How can I modify it to hide only the scrollbar thumb? body { height: ...

JavaScript slice() method displaying the wrong number of cards when clicked

In my code, I have a section called .registryShowcase. This section is designed to display logos and initially shows 8 of them. Upon clicking a button, it should load the next set of 8 logos until there are no more left to load (at which point the button ...

Creating a border for a checkbox using CSS

I need help with styling a checkbox using jQuery $("#reg_checkbox").css("border","thin solid red"); The border displays correctly in Internet Explorer, but not in Mozilla. How can I ensure browser compatibility for this styling? ...

Mastering the art of reading rows in ASP.NET using Java Script

Within the code snippet below, you'll find an image located in the second column. Clicking on this second column should allow me to access the data stored in the first column. Let's say we have a table with 10 rows. If the user clicks on the ico ...

Tips for adjusting the position of a primefaces p:confirmDialog

I am facing an issue with adjusting the distance between my confirmDialog and the top of the page when it is displayed within an iframe. I attempted to modify my CSS settings, but unfortunately, it did not have the desired effect. <p:confirmDialog glob ...

Tips for utilizing flexbox to position a button to the right of an InputSelect

Struggling to position a button next to a dropdown? No matter what I attempt, the button keeps ending up above the dropdown. Ideally, I'd like it to appear alongside 'Select Organisation' as shown in the image below: https://i.sstatic.net/w ...

Implementing touch cancellation in Corona SDK: A comprehensive guide

I'm currently developing a game with Corona SDK, where I need to display multiple balls. Each ball object has a TouchListener implemented. You can see the code snippet below: local function ballTouchEvent(e) local touchedBall = e.target loca ...

Utilizing flexbox to align a horizontal menu

I have been working on creating a fixed menu using flexbox. Within my navigation bar, I have set up 2 divs. The first one is supposed to contain the logo, and the second one is for the menu (utilizing the display:flex property). However, I am facing an iss ...

Is JavaScript necessary for this task in PHP?

Hi everyone, I recently discovered a way to style a PHP echo in a more visually appealing manner. Instead of presenting an unattractive colored box right away, I was wondering if there is a way to only display the box when the user selects the calculation ...

How can I create an image link with a hover effect using HTML and CSS?

I've streamlined my code to focus on the issue at hand - I have an image that changes when hovered over, but how can I make it so that clicking on the image redirects you to a specific website? http://pastebin.com/h83yi3e3 Here is the code snippet: ...

Incorporating an external URL into a webpage with a method that allows for overflow

Let me explain my current predicament. I recently created a new navigation menu using a php framework different from the one used in the existing site. Both the new menu and the old site exist on the same domain. To integrate the new navigation, I attempte ...

The title in the bottom bar is not appearing correctly when using the library "com.roughike:bottom-bar:1.4.0.1"

Hello, I have a question regarding the bottom bar on my website. You can see it here: https://i.stack.imgur.com/gaayc.png The library I am using is com.roughike:bottom-bar:1.4.0.1. However, I am facing an issue: Some titles on the bottom bar are not bei ...

Get rid of any unnecessary class on the element

I'm currently working on an image slider that displays before and after images when the user drags left to right. The issue I'm facing is that the 'draggable' class is being added not only to these images but also to my hero images. It ...

Click event not triggering to update image

I'm currently working on a Codepen project where I want to use JavaScript to change the image of an element with the id "chrome". However, my code doesn't seem to be working as expected. Can someone help me troubleshoot and fix this issue? Your a ...

What steps can I take to resolve the JSONException error stating "No value" in my Android application?

Is anyone familiar with how to resolve my issue or where I should begin looking? I am completely lost. I have been working on a modified Android tutorial that involves MySql and Php. When I click the 'Register' button in my app, nothing happens ...