B4J Question Change webview font

Bel

Member
Licensed User
Hi
In android we cannot change font,of course can change but above android 4 if it dont mistake
Can i change webview font without any problem and how?
 

stevel05

Expert
Licensed User
Longtime User
Use CSS in the page content, the same as with any webpage.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
This should do it. The Fontfile.ttf should be in your assets folder.

B4X:
    Dim HTML As String = $"<style>
@font-face {
    font-family: myfont;
    src: url(${WebViewAssetFile("Fontfile.ttf")});
    }
    </style>
    <body style="font: 16px myfont;">
    Lorem ipsum dolor sit amet, mel et movet debitis. Sit mazim definiebas in. Saepe sadipscing est in, eu pri consul sententiae, his vitae feugait et. Eam idque choro in, homero labore prompta an his.

Quis recteque adolescens an usu, libris voluptaria necessitatibus ut nec. Sea et vocent invidunt. An sed movet choro complectitur, pro ne omnium apeirian. Torquatos tincidunt scribentur ad duo. Ceteros perpetua postulant et his, est an nobis audiam rationibus, est odio regione accusamus ex. Pri ut probo fastidii oporteat, everti sententiae sadipscing ut qui.

Pro ea iusto regione, ad mazim legimus mei, oblique sanctus eum an. Dico reque percipitur eam ne, pro et case vidit viris. Has imperdiet similique in. Per maiorum pericula definitiones an, mea ne mazim causae maiestatis. Suas maiorum lucilius cum ad.

At nam vocibus sensibus democritum, pro ex cetero consulatu disputationi. Ut duis ubique neglegentur ius, copiosae insolens posidonium ea nec. Utroque veritus expetendis quo et. Et eam assum oblique laboramus. Nihil epicuri in nam.

Nobis hendrerit eos cu. Ea pri eleifend neglegentur accommodare, at cum dolore bonorum, noster suscipiantur usu ut. Et has doctus maiestatis deterruisset, eam sumo cibo conceptam et, epicurei intellegebat duo cu. Ei error nemore sed, no sed meliore oporteat accommodare, at usu tractatos imperdiet philosophia. Regione numquam id has.
</body>"$

Webview1.LoadHtml(HTML)
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I Should have included the WebViewAssetFile sub too, it's available in the forum. For completeness it is:
B4X:
Sub WebViewAssetFile (FileName As String) As String
   Dim jo As JavaObject
   jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File")
   If jo.GetField("virtualAssetsFolder") = Null Then
     Return "file:///android_asset/" & FileName.ToLowerCase
   Else
     Return "file://" & File.Combine(jo.GetField("virtualAssetsFolder"), _
       jo.RunMethod("getUnpackedVirtualAssetFile", Array As Object(FileName)))
   End If
End Sub
 
  • Like
Reactions: Bel
Upvote 0

Bel

Member
Licensed User
I Should have included the WebViewAssetFile sub too, it's available in the forum. For completeness it is:
B4X:
Sub WebViewAssetFile (FileName As String) As String
   Dim jo As JavaObject
   jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File")
   If jo.GetField("virtualAssetsFolder") = Null Then
     Return "file:///android_asset/" & FileName.ToLowerCase
   Else
     Return "file://" & File.Combine(jo.GetField("virtualAssetsFolder"), _
       jo.RunMethod("getUnpackedVirtualAssetFile", Array As Object(FileName)))
   End If
End Sub
ُThanks but it get error in jo.GetField("virtualAssetsFolder")
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Ooo sorry, that's b4a code.

It's not needed for B4j try this:

B4X:
        Dim HTML As String = $"<style>
@font-face {
    font-family: myfont;
    src: url(${File.GetUri(File.DirAssets,"Fontfile.ttf")});
    }
    </style>
    <body style="font: 16px myfont;"> Lorem ipsum dolor sit amet, mel et movet debitis. Sit mazim definiebas in. Saepe sadipscing est in, eu pri consul sententiae, his vitae feugait et. Eam idque choro in, homero labore prompta an his.

Quis recteque adolescens an usu, libris voluptaria necessitatibus ut nec. Sea et vocent invidunt. An sed movet choro complectitur, pro ne omnium apeirian. Torquatos tincidunt scribentur ad duo. Ceteros perpetua postulant et his, est an nobis audiam rationibus, est odio regione accusamus ex. Pri ut probo fastidii oporteat, everti sententiae sadipscing ut qui.

Pro ea iusto regione, ad mazim legimus mei, oblique sanctus eum an. Dico reque percipitur eam ne, pro et case vidit viris. Has imperdiet similique in. Per maiorum pericula definitiones an, mea ne mazim causae maiestatis. Suas maiorum lucilius cum ad.

At nam vocibus sensibus democritum, pro ex cetero consulatu disputationi. Ut duis ubique neglegentur ius, copiosae insolens posidonium ea nec. Utroque veritus expetendis quo et. Et eam assum oblique laboramus. Nihil epicuri in nam.

Nobis hendrerit eos cu. Ea pri eleifend neglegentur accommodare, at cum dolore bonorum, noster suscipiantur usu ut. Et has doctus maiestatis deterruisset, eam sumo cibo conceptam et, epicurei intellegebat duo cu. Ei error nemore sed, no sed meliore oporteat accommodare, at usu tractatos imperdiet philosophia. Regione numquam id has.
</body>"$

WebView1.LoadHtml(HTML)
 
Upvote 0
Top