Android: Implementing custom font (Arial) in WebView for External Website with CSS styling

Can I integrate a TTF font (like Arial) into my Android app so that it can be accessed by a externally loaded webpage's CSS?

For instance, imagine my website is and its CSS file contains the following:

@font-face { 
    font-family: "CustomArial"; 
    src: url('Arial.ttf'); 
}

h1 {  
    font-family: 'CustomArial', serif;
}

I've experimented with different variations for setting the font-face URL source, but none seem to work:

  • /mnt/sdcard/Arial.ttf
  • file://mnt/sdcard/Arial.ttf

I am aware that this approach works if the Arial.ttf file is in the same directory as the CSS file on the server. However, I want to use the Arial.ttf file located in my app's /assets folder or somewhere else on the SDCard. The reason being that the Arial.ttf file is 775kb, and users would need to download it before the page can be displayed.

Is there a way to achieve this? I understand that I could do this if the HTML loaded into the WebView was local and I used WebView.loadData(), but I specifically need to utilize WebView.loadUrl().

Any insights or assistance would be greatly appreciated!

Answer №1

1) Make sure to store the font file in the assets directory within your project, not on the SD card. Create a specific folder labeled assets within your project files.

2) Within your @font-face declaration, specify the source of the font using

src: url('file:///android_asset/Arial.ttf');

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

Is there a way to dynamically ascertain if my application is operating as a system or in a regular capacity

What is the best way to distinguish between a system app and a regular app? After examining the android PackageManager, I did not come across any clear distinction. Edit: Ultimately, I am looking for a solution that involves coding. if(system app) { // ...

Having trouble generating the signed APK for a Nativescript Android app

I am currently in the process of developing a Nativescript application for Android. I have been using the following command: $ tns build android [--compileSdk API Level] [--key-store-path File Path --key-store-password Password -key-store-alias Name --ke ...

Before the cloud function can return a promise, there are certain processing tasks that need to be completed. However, the promise is being

In one of my cloud functions, I pass an array of numbers and compare them with a collection in Firestore. If any matching numbers are found, I need to return an array containing those numbers. However, before carrying out the comparison, the function is re ...

The Kendo Dropdownlist mysteriously disappears behind the PDFViewer when using Safari

Currently, I am immersed in a project based on the MVC framework and utilizing the Kendo DropDownList. An problem has arisen, which is visualized in the image below. Specifically, the items of the Kendo DropDownList are becoming obscured by the PDFViewer ...

Adjust the width of content within a wrapper using HTML and CSS to automatically resize

I am facing an issue with a content div that has variable length content arranged horizontally. I want the width of this content div to adjust automatically based on the length of its content. Removing the arbitrary "20%" value from the width property is c ...

What is the best way to set the width of a fixed div based on its parent div's percentage width?

Having trouble setting a fixed div to be the same width as its parent element, which is itself size relative to another div. To clarify, here is a code snippet that demonstrates the issue: HTML <div class="grandparent"> <div class="parent" ...

Increase the background image size for a <li> element

I am working with an unordered list and have set it up to display a background image on the li element when the cursor hovers over it: <div class="sidebar"> <ul> <li>test</li> <li>test</li> ...

Utilizing "Content" for Responsive Image Design in Chrome Version 33.0.1750.117

I previously implemented a method for responsive images on my website, which was working fine until the latest Chrome update. Surprisingly, it still functions properly on other browsers. //< ![CDATA[ var queries = [ ...

bring in all the files located within the Directory

Is there a way to import all CSS files from a specific folder without having to import each file individually? Looking to import assets/css/* ? <link href="<?php echo base_url(); ?>assets/css/*" rel="stylesheet"/> <title&g ...

Display items inside containers using angularjs ng repeat and bootstrap styles, however, the layout is not being rendered correctly

I am struggling to display products in boxes on my ecommerce website, similar to how they appear on other platforms. Although I am utilizing AngularJS ng-repeat and bootstrap classes, the layout of the products is not coming out as intended. Take a look ...

We apologize for the inconvenience, but the application has encountered an unexpected error. Please attempt to

Currently, I am attempting to develop an android application for login using Eclipse. However, when I execute the code, I encounter the error mentioned above. After inspecting the logcat, it indicates an error in line 36 but I am uncertain how to resolve i ...

Introduction to the Android RecyclerView and ViewPager combo

I am facing an issue with my ViewPager that uses a RecyclerView for each page and shares ViewItem rows across pages. I have implemented a single RecyclerViewPool to share between them. However, the problem is that the ViewPager loads all RecyclerView insta ...

Enhance Your Button with CSS3 Animation

While experimenting with CSS3 animations, I encountered a challenge when trying to apply an animation to a button upon clicking. The desired effect was for the button to zoom in, spin, and then zoom out, giving the appearance of flying off the screen. Surp ...

Tackling the sticky header problem with in-browser anchoring and CSS dynamic height restrictions

I am experimenting with a unique combination of a Pure CSS in-page anchoring solution using scroll-behavior: smooth and scroll-margin-top settings, along with a sticky header for in-page navigation. By utilizing IntersectionObserver, I can identify when t ...

Using WebDriver Selenium to choose an element within a table following another element

The webpage features a user details table with a functionality to delete users. To remove a user, I must select the row based on the username and then click the "Delete" button. The structure of the HTML table is as follows: <table id="tblUsersGrid" ce ...

Decide on the appropriate Context variable to utilize with MediaPlayer.create in Android

Encountered a complex issue with MP3 streaming using the MediaPlayer in Android. I previously created an app with it, but now I am refactoring the code for various reasons. Within the Player activity, there is a call to a Service: mp3Service.playSong(get ...

Is the use of div:after content really affecting the width? I am having trouble getting my transition to work

Here is a simple code snippet that represents my actual code: #myDiv { background: black; color:white; float:left; min-width:45px; max-width:450px; -webkit-transition: all 1s ease-in-out; transition: all 1s ease-in-out; } #myDiv:hover:after { width ...

CSS: The height of floating divs is set to 0

I'm attempting to align 2 divs next to each other within another div, creating 2 columns of text with the outer div forming a border around both: HTML <div id="outer"> <div id="left"> ... <div id="right"> </div& ...

When I press the back button to return to a previous activity, my arraylist is reset

One of the challenges I am facing in my app is with multiple activities. In my main activity, I have a listview populated by an arraylist. Strangely, when I navigate to the second activity and then press the back button to return to the main activity, my a ...

Adding a custom class to a select2 dropdown: How can it be done?

I have customized select2 using CSS with its general classes and IDs. Currently, I am attempting to customize a specific class that will be assigned to select2 and then applied in the CSS. The problem lies not within the select itself, but within its dro ...