The CSS ID selector fails to load on Android WebView

I am facing an issue while trying to load HTML with CSS that includes an ID selector. The file is not loading to the webview as expected.

Here is the code snippet I attempted to load:

    <!DOCTYPE html>
     <html>
      <head>
        <style>
           #para1 {
              text-align: center;
              color: red;
                   }
              </style>
              </head>
            <body>
        <p id="para1">Hello World!</p>
        <p>This paragraph is not affected by the style.</p>
  </body>
  </html>

However, on using the following code below, it loads successfully in the webview:

         <html>
            <head>
              <style>
                h1 {color:red;}
                p {color:blue;}
             </style>
             </head>
              <body>
             <h1>A heading</h1>
             <p>A paragraph.</p>
          </body>
         </html>

I have configured the WebView with these properties:

    mWebView.setWebViewClient(new CustomWebViewClient());
    mWebView.setWebChromeClient(new CustomWebChromeClient());
    mWebView.getSettings().setSupportZoom(true); // ZoomControls
    mWebView.getSettings().setBuiltInZoomControls(true); // Multitouch
    mWebView.getSettings().setDisplayZoomControls(false);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    mWebView.getSettings().setUseWideViewPort(CONFIG.webviewEnableViewport());
    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.requestFocus(View.FOCUS_DOWN);
    mWebView.setHorizontalScrollBarEnabled(true);
    mWebView.setVerticalScrollBarEnabled(true);

Seeking assistance in resolving this issue. Any help would be appreciated.

Answer №1

The problem was successfully resolved by employing base64 encoding.

   String encodedContent = Base64.encodeToString(myContentString.getBytes(), 
   Base64.NO_PADDING);
   mWebView.loadData(encodedContent, "text/html", "base64");

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

HTML and JavaScript - Facing issues rendering HTML content during the conversion process from Markdown format

In this particular scenario, my goal is to transform the content inside #fileDisplayArea into markdown format. However, I am encountering an issue where the HTML code within the div element is not being rendered. <div id="fileDisplayArea"># Title ...

Is this an effective and appropriate method for establishing media queries?

<link id ="style" rel="stylesheet" type="text/css" title="other" href="regularStyles.css" /> <link rel="stylesheet" media= "all and (mid-width: 767px) and (max-width:2049px) and (min-height:767px) and (max-height: 1538px)" href="notePads.css" /& ...

Guide on using timestamps in PHP

I am struggling to incorporate the current date and time of the SO-NUMBER that the user is entering. The issue I'm encountering lies within the IF..ELSE loop, it only updates one column in the database, which is the "samplerecived" column even if I ...

Android - Email with image attachment showing 0KB file size when sent

After creating an app with a click action to send an email with an image attachment file, I initially thought the code was working correctly. However, upon further inspection, I discovered that the size of the attached image file was 0kb. When attempting t ...

Forming a ring around a character within a header one element

I am faced with the challenge of only circling a single letter within a word that is contained in an H1 tag. While creating a circle around text is easy, I specifically need to target just one letter: .large { font-size: 5em; } .circle { border-rad ...

The Fade In effect in jQuery causes my fixed header to overlap with images on the page

This is the javascript snippet using jquery <script type="text/javascript> $(document).ready(function () { $('#showhidetarget').hide(); $('a#showhidetrigger').click(function () { $('#showhidetarget&apo ...

Different ways to prevent invalid entries in an input field with type datetime-local

<input type="datetime-local" step="1"> Is there a way to prevent invalid date input? For example, entering a date like "11:11:1111" in the format "mm-dd-yyyy". How can this be addressed using Moment.js? ...

Safari and Chrome browsers (using the Webkit engine) do not support hiding the vertical scrollbar on if

I currently have an iframe embedded on www.example.com that is linked to support.example.com (which is a CNAME to a foreign domain). To ensure the contained webpage fits perfectly without needing any scrollbars, I automatically adjust the height of my ifr ...

The color of the text changes based on its alignment

Utilizing style sheets and attribute selectors, create styles that will change the color of text displayed on the page based on its alignment (e.g. left-aligned text in blue, right-aligned text in green). Additionally, text within h2 tags should have color ...

Enhance the appearance of div reflection with additional flair

Attempting to enhance a div with a reflection using the following CSS: CSS :: div.reflection { -webkit-transform: scaleY(-1); -moz-transform: scaleY(-1); -o-transform: scaleY(-1); -ms-transform: scaleY(-1); transform: scaleY(-1); ...

Unable to retrieve the parent element using jQuery

I am facing an issue with my html structure that is generated dynamically through a foreach loop. I have attempted to remove the entire <a> element by accessing it from its ACTIVE HYPERLINK. However, all my efforts seem to be in vain as I am unable t ...

What is the best way to enlarge a div to fill the entire screen?

I recently started using Flot to visually represent my data. I've been exploring ways to enhance the user experience, and one idea that came to mind was making the graph occupy the entire screen when a button is clicked. The current setup of the div e ...

Tips for displaying a loading screen during the loading of JSON data or object before transitioning to a new activity

I am looking to display a loading screen while fetching json data or object, and then switch to a new activity with all the json objects that have been loaded. Here is my data modal class: public class CategoryDataModal { private Stri ...

Issue with CSS floating elements on left or right causing a significant gap

I've encountered an issue with my CSS layout. I have specified "float:right; clear:right" for ads and "float:left" for blocks in the main text meant to flow like images. However, there is a significant gap in the main text flowbox. The main text only ...

Looking for assistance with parsing out four numerical values from an HTML scrape using Python

I currently have code that opens a URL and retrieves HTML data into htmlA Within htmlA, I am attempting to extract 4 specific pieces of information: A date Price 1 Price 2 A percentage The section of htmlA where these 4 pieces of information are located ...

Tips for verifying whether the time setting is automatic on an Android device

Currently, I am developing an android app using flutter. One of the functionalities I am looking to incorporate is the ability to check whether the time setting on the android device is set to automatic or not. In Java, we can achieve this by using the fol ...

Determine the highest video resolution capability of a mobile device when accessing a responsive website

Looking for a solution to display html5 videos on various browsers and platforms, with different size resolutions. A challenge arises when high resolution videos are accessed from devices with standard or low display resolutions, causing them not to play p ...

Navigating through onclick events in an Android fragment with Navigation functionality

I'm having trouble setting an onClick listener for my button in a fragment. I may be misunderstanding the context, but I just can't seem to figure it out. Please help me out below, I believe I've done everything correctly? I thought I had a ...

PHP unable to create new directory

I'm in the process of uploading a file, and here is my HTML code for it: <form action="upload_file.php" method="post" enctype="multipart/form-data"> Select a file: <input type="file" name="upload"> <input type="submit"> < ...

how to set focus on EditText in android

On my application, I have three screens with 3 editTexts on each screen. The issue I am facing is that whenever these activities are launched, the cursor automatically goes to the first edittext and the keyboard pops up. What I would like instead is for t ...