Tips for integrating Css/JQuery files into an external page on an Android WebView

Uncertain if my desired idea is feasible or not, but why not try now.

The structure of my MVC4 Master page is as follows:

<head runat="server">
    <meta charset="utf-8" />
    <title>RYTE HCMS</title>
    <meta name="viewport" content="width=device-width" />
    <link href="<%: Url.Content("~/favicon.ico") %>" rel="shortcut icon" type="image/x-icon" />
    <link rel="stylesheet" href="~/Content/jquery/jquery.mobile.theme-1.4.3.min.css" />
    <link rel="stylesheet" href="~/Content/jquery/jquery.mobile.icons.min.css" />
    <link href="~/Content/jquery-ui.datepicker.min.css" rel="stylesheet" />
    <link href="~/Content/jquery.mobile.structure-1.4.5.min.css" rel="stylesheet" />
    <%: Scripts.Render("~/bundles/modernizr") %>
    <style type="text/css">
        .wrap {
            white-space: normal !important;
        }
    </style>
</head>
<body>
    <div data-role="page" data-theme="a" data-url="master-page" id="master-page">
        <div data-role="header" data-position="fixed">
            <asp:ContentPlaceHolder ID="Header" runat="server">
                <a href="#leftpanel" data-panelid="leftpanel" data-icon="bars" data-role="link" style="height: 17px"></a>
                <h1>
                    <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
                </h1>
                <% Html.RenderPartial("LoginUserControl"); %>
            </asp:ContentPlaceHolder>
        </div>
        <div data-role="content">
            <asp:ContentPlaceHolder ID="MainContent" runat="server" />
        </div>
    </div>
    <script src="<%=Url.Content("~/Scripts/jquery-1.11.1.min.js")%>"></script>
    <script>
        $(document).on("mobileinit", function () {
            $.mobile.ajaxEnabled = false;
        });
    </script>
    <script src="<%=Url.Content("~/Scripts/jquery.mobile-1.4.5.min.js")%>"></script>
    <script type="text/javascript">
        $(document).on("pageinit", "#master-page", function () {
            $(document).on("swipeleft swiperight", "#master-page", function (e) {
                // We check if there is no open panel on the page because otherwise
                // a swipe to close the left panel would also open the right panel (and v.v.).
                // We do this by checking the data that the framework stores on the page element (panel: open).
                if ($.mobile.activePage.jqmData("panel") !== "open") {
                    if (e.type === "swipeleft") {
                        $("#right-panel").panel("open");
                    } else if (e.type === "swiperight") {
                        $("#leftpanel").panel("open");
                    }
                }
            });
        });
    </script>
    <asp:ContentPlaceHolder ID="ScriptsSection" runat="server" />
</body>

I aim to load and apply CSS/JQuery in the Android WebView. My goal is to store the files within the android mobile app and implement them as the page loads inside the webView.

If these files are not loaded in a specific order, the design will fall apart. This approach is essential to ensure that users do not perceive it as a web app. Currently, upon each refresh/reload, it takes around 1-2 seconds for jquery mobile and CSS to load.

Considering that MVC4 pages are compiled on a remote server, is this task achievable?

Answer №1

Alright, let me break it down for you. To implement the js/css code in your assets files, follow the steps mentioned below.

For a better understanding, refer to this sample project (I have demonstrated loading js code, but the process remains the same for css as well) here

Here's a general overview:

  • Set up your webview with a custom WebViewClient (like MailWebViewClient in the example). No need for a custom webview as shown in the example
    wv.setWebViewClient(webViewClient); 
  • In your webview client, customize the onPageFinished() method and load your javascript code within a string (src)
    view.loadUrl("javascript: " + src); 
  • That's pretty much all you need to do. It should work without any issues

    The sample project demonstrates a script that resizes tables and images to fit the webview screen (a common need for android). Additionally, there is some linkify code included to identify and highlight links. I recommend not using the provided js code directly as it has some bugs, but it serves as a good illustration of the concept.

    Answer №2

    Ensure all CSS and JS files are placed in the assets directory.

    Then, attempt to display the web view using this code:

    webView.loadDataWithBaseURL("file:///android_asset/", <YOUR_HTML_CODE> ,"text/html", "UTF-8", null);
    

    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

    Creating dynamic keys to insert objects

    In my coding project, I am dealing with an empty array, a value, and an object. Since there are multiple objects involved, I want to organize them into categories. Here is an example of what I envision: ARRAY KEY OBJECT OBJECT KEY OBJECT ...

    Receiving a 401 error code stating "unauthorized" when attempting to invoke a method using AJAX in .NET

    I am working on a .NET project within the framework 4.5.1 When I incorporate ajax as shown below, the debugger successfully hits the page_load event: $.ajax({ "async": true, "crossDomain": true, type: "POST", url: "Advance ...

    I am struggling to set up angular-localstorage4

    I have been following the instructions in the provided link: angular-localstorage4 When attempting to import WebStorageModule and LocalStorageService from angular-localstorage, I encounter an error in the console even though the compilation is successful. ...

    There is no varying factor between the platforms when utilizing ionic 3

    My initial attempt at trying ionic 3 has been met with some challenges. When I launch the application using either the ionic lab or ionic serve command, the platforms displayed in the browser show the same views for every device (iOS, Android, Windows). H ...

    CSS Styling for a Minimalist Web Design

    I have been working on creating my own layout for a few hours now, but I am still encountering some issues. My goal is to achieve something like this: Specifically, I want the body of the page to be flexible, with a maximum width of 900px. However, I am s ...

    What strategies can flex-shrink use to triumph over padding in the battle for space

    I am facing an issue with a flex-box tag cloud where the sizing is controlled from the outside. I need the tags inside to be collapsible all the way down to zero if necessary, but currently they only collapse up to 2 times their padding. https://i.sstatic ...

    Enhancing server error troubleshooting with Next.js: improved stack trace visibility?

    When a server error happens on Next.js, the call stack only provides information about the specific component where the error is located without offering any further guidance. For instance, in /pages/index.js, I have a component named Test. Within this co ...

    Tips for resolving issues with storing data in a text box that is constantly being added to

    Can someone please assist me with this issue I'm facing? I am using isset to check if the index is defined, but it stores 0 instead of saving the value of the textbox. How should I tackle this problem? If I don't check if the index is defined and ...

    Does the addClass() method run asynchronously?

    When trying to remove the class transition (and therefore, the CSS transition property), moving the div to 200px immediately, reapplying the transition css property, and then animating the div to the right over 1 second, it appears to be frozen instead. I ...

    Introducing an alternative solution for handling tasks linked to the completion of CSS3 animations

    In order to incorporate smooth CSS3 animations into my website, I have implemented the animate.css library created by Dan Eden. One particular scenario involves a loading screen that features a fade-out animation. It is crucial for this animation to comple ...

    Select a single option from the group to include in the array

    I'm currently developing a new soccer betting application. My goal is to allow users to choose the result of a match - whether it's a win, loss, or draw - and then save that selection in a list of chosen bets. https://i.stack.imgur.com/hO3uV.png ...

    What is the jQuery syntax for targeting a specific element within an object?

    Is there a way to access a sub element from $(this)? For instance, how can I specifically select a span element inside the this object? ...

    Utilize GroupBy in JavaScript to categorize JSON data and display it within an optgroup

    I seem to be a bit disoriented. I have received this JSON data: [{ "id": "210", "name": "Name 1", "category": "Category 1" }, { "id": "187", "name": "Name 2", "category": "Category 1" }, { "id": "186", "name": "Name 3", ...

    Enable the GPS feature on rooted devices

    The topic of controlling GPS programmatically on Android has been widely debated, with the common consensus being that it can't be done due to security and privacy concerns. However, is there a potential workaround for rooted devices to enable GPS by ...

    The Bootstrap navigation row smoothly adjusts its size, yet only partially highlights active or highlighted links

    When viewing on a smaller screen, one of the links in this navigation (nav-pills, nav-justified) spans two lines, causing the other links to appear larger as well. While this may seem beneficial, active or highlighted links only partially fill the box, res ...

    Determine the success of an SQL query in Node.js

    I've created a basic API using nodejs to connect my Flutter app with a SQL Server database, but I have a question. How can I verify if the query was successful in order to return different results? I'm attempting an update after a successful in ...

    What could be causing React Router to fail in navigating to a nested route?

    In my App.js file, I am implementing front-end routing using react-router-dom version 6.11.2: import "./App.css"; import { Route, RouterProvider, createBrowserRouter, createRoutesFromElements, } from "react-router-dom"; // Othe ...

    Having issues with AJAX and .change() function not functioning correctly?

    I am working on two drop-down menus. The first menu displays the provinces in the country, and when a province is selected, the second menu should show the districts within that province. The following code is for displaying the provinces: $(document).re ...

    Steps for retrieving a file after uploading it with express and multer

    I successfully uploaded the file to my backend filesystem using multer My server runs on node while the client is based on react. However, I am facing issues with downloading and displaying the saved file on the client side using react. Every time I att ...

    Can one look through a div to the one beneath it by moving the cursor?

    Hey there! I have a unique question for you. I've been trying to achieve a specific effect where two divs are stacked on top of each other, and the content of the lower div is only revealed in a certain area around the cursor. Is it possible to make o ...