Android Question using local(cutom) fonts in list view and web view

vvg

Member
Licensed User
Longtime User
Sir, I am developing my first app with b4a on android mobile. I am using regional language (custom) font in the application. My problem is that i can not use typeface in web view or list view. So what to do if i want to use custom font in web view and list view?
I have good experience in vb6 development. But no knowledge about html or css etc. So kindly guide me in detail and please provide the relevant code with example.
 

ilan

Expert
Licensed User
Longtime User
hi

to use a custom font in a listview you could try something like this:

B4X:
Sub FillListView
    Dim MyFont As Typeface
    MyFont = Typeface.LoadFromAssets("MyFont.ttf")

    listview1.SingleLineLayout.ItemHeight = 100dip
    listview1.SingleLineLayout.Label.TextSize = 20
    listview1.SingleLineLayout.Label.TextColor = Colors.Blue
    listview1.SingleLineLayout.Label.Gravity = Gravity.CENTER
    listview1.SingleLineLayout.Label.Typeface = MyFont
   
    For i = 1 To 10
        listview1.AddSingleLine("blablabla...")  
    Next
End Sub

or you could use the CustomListView Class: https://www.b4x.com/android/forum/t...xible-list-based-on-scrollview.19567/#content

about the webview, i am not sure i understood what you mean. in a webview you should see the text of the loaded website, dont you?
 
  • Like
Reactions: vvg
Upvote 0

ilan

Expert
Licensed User
Longtime User
You can use CSBuilder to customize the text font and other attributes:

Yeah right forgot about that.

B4X:
For i = 1 To 100
 ListView1.AddSingleLine(cs.Initialize.Color(Rnd(0xFF000000, -1)).Alignment("ALIGN_CENTER").Append($"Item #${i}"$).PopAll)
Next
 
Upvote 0

vvg

Member
Licensed User
Longtime User
hi

to use a custom font in a listview you could try something like this:

B4X:
Sub FillListView
    Dim MyFont As Typeface
    MyFont = Typeface.LoadFromAssets("MyFont.ttf")

    listview1.SingleLineLayout.ItemHeight = 100dip
    listview1.SingleLineLayout.Label.TextSize = 20
    listview1.SingleLineLayout.Label.TextColor = Colors.Blue
    listview1.SingleLineLayout.Label.Gravity = Gravity.CENTER
    listview1.SingleLineLayout.Label.Typeface = MyFont
  
    For i = 1 To 10
        listview1.AddSingleLine("blablabla...") 
    Next
End Sub

or you could use the CustomListView Class: https://www.b4x.com/android/forum/t...xible-list-based-on-scrollview.19567/#content

about the webview, i am not sure i understood what you mean. in a webview you should see the text of the loaded website, dont you?
 
Upvote 0

vvg

Member
Licensed User
Longtime User
thank you very much ilan for your help...It worked. I will try about web view also.

Second thing, I tried for a small donation as you suggested but couldn't as i am from india and i use only debit card and these cards are not usefull for international transactions. I tried but no use.
So thanx again for your help
 
Upvote 0

vvg

Member
Licensed User
Longtime User
Thank you Erel for your response. I will try. For list view, ilan gave hint about code and it worked. I will try your suggestion of cs builder also.
 
Upvote 0

vvg

Member
Licensed User
Longtime User
Dear ilan,
Your tip worked for list view but in webview, I could not display the same regional font. I m new for html, css, etc. stuff. Here I m giving the code. Please correct if there is anything wrong.


Following code that I m using to populate records in webview. It works fine if I used normal english font.
Now I want to use regional language font named Shivaji01.ttf.
But it does not reflect the proper font effect.

Here is the code.....

Sub ExecuteHtml(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, Clickable As Boolean) As String
'

Private cur As Cursor
Dim ColName As String
If StringArgs <> Null Then
cur = SQL.ExecQuery2(Query, StringArgs)
Else
cur = SQL.ExecQuery(Query)
End If
Log("ExecuteHtml: " & Query)
If Limit > 0 Then Limit = Min(Limit, cur.RowCount) Else Limit = cur.RowCount
Private sb As StringBuilder
sb.Initialize
sb.Append("<html><body>").Append(CRLF)
sb.Append("<style>").Append(CRLF)
sb.Append("p.ex1 {").Append(CRLF)
sb.Append("font: bold 15px shivaji01, sans-serif;").Append(CRLF)
sb.Append("}").Append(CRLF)
sb.Append("</style>").Append(CRLF)
sb.Append("<table><tr>").Append(CRLF)
sb.Append("<p class='ex1'>Aibar</p>").Append(CRLF)
For i = 0 To cur.ColumnCount - 1
'sb.Append("<th>").Append(cur.GetColumnName(i)).Append("</th>")
If i=0 Then
ColName="C.No."
else if i=1 Then
ColName="M. No."
else if i=2 Then
ColName="Member Name"
else if i=3 Then
ColName="Milk"
else if i=4 Then
ColName="Ltr"
else if i=5 Then
ColName="Fat"
else if i=6 Then
ColName="Rate Rs."
else if i=7 Then
ColName="Amt. Rs."
End If
sb.Append("<th>").Append(ColName).Append("</th>")
Next
'sb.Append("<p style='font-family:shivaji01; 'size:160%;'>jaya kipsa ithU laaok ]jaagar</p>")

sb.Append("</tr>").Append(CRLF)
For row = 0 To Limit - 1
cur.Position = row
If row Mod 2 = 0 Then
sb.Append("<tr>")
Else
sb.Append("<tr class='odd'>")
End If
For i = 0 To cur.ColumnCount - 1
sb.Append("<td>")
If Clickable Then
sb.Append("<a href='http://").Append(i).Append(".")
sb.Append(row)
sb.Append(".com'>").Append(cur.GetString2(i)).Append("</a>")
Else
sb.Append(cur.GetString2(i))
End If
sb.Append("</td>")
Next
sb.Append("</tr>").Append(CRLF)
Next
cur.Close
sb.Append("</table></body></html>")
Return sb.ToString
End Sub

Important:
This code runs well on desktop browser as I used the font Shivaji01.ttf from windows font folder.
But I can not understand from where and how to use Shivaji01.ttf font file in android.
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
please use Code Tags when you post a Code.

B4X:
'...

instead of using StringBuilder use csBuilder.
with csBuilder it should work.

look at the code i posted above or search in the forum for csBuilder there are lots of examples.
 
Upvote 0
Top