Android Tutorial Load HTML to Label

Discussion in 'Tutorials & Examples' started by Brandsum, Jun 9, 2019.

  1. Brandsum

    Brandsum Active Member Licensed User

    (Here is the iOS version)
    This is how you can set HTML data to Label (works only in release mode),

    Dependencies: StringUtils, jSoup, LabelExtras

    Here is the code,
    Code:
    Dim extraHeight As Int = 0 'global variable (height of images)
    Dim htmlParser As Html 'global variable
    Dim jSoup As jSoup 'global variable
    Dim htmlLabel As Label 'global variable
    Dim strUtils As StringUtils 'global variable

    Dim HtmlString = "YOUT HTML STRING"
    Dim HtmlImgList As List = jSoup.selectorElementAttr(HtmlString,"img","src"'list all the images to be downloaded

    'HtmlViewImgDownloader is a replica of Erel's ImageDownloader class. But it just stores the downloaded image to cache folder of app
    Dim rs As ResumableSub = CallSub2(HtmlViewImgDownloader,"Download",HtmlImgList)
    Wait For (rs) Complete (result As Boolean)

    htmlLabel.Initialize(
    "")
    htmlLabel.Text = htmlParser.FromHtml2(HtmlString,
    "ImageGetter","TagGetter")
    htmlLabel.TextSize = 
    16
    htmlLabel.width = 
    100%x
    htmlLabel.height = strUtils.MeasureMultilineTextHeight(htmlLabel,htmlLabel.Text.Trim) + extraHeight


    'It handles image tags while parsing your html string
    Sub ImageGetter_GetDrawable(Source As StringAs Object
        Source = 
    Regex.Split("/",Source)(Regex.Split("/",Source).Length-1'get image name from source string
        If File.Exists(File.DirDefaultExternal &"/cache", Source) Then
            
    Dim Bitmap1 As Bitmap
            Bitmap1=
    LoadBitmap(File.DirDefaultExternal &"/cache", Source)
        
            
    Dim BitmapDrawable1 As BitmapDrawable
            BitmapDrawable1.Initialize(Bitmap1)
      
            
    Dim l As LabelExtras
            l.SetDrawableBounds(BitmapDrawable1, 
    00100%x100%x * (Bitmap1.Height/Bitmap1.Width))
            extraHeight = extraHeight + 
    100%x * (Bitmap1.Height/Bitmap1.Width)
            
    Return BitmapDrawable1
        
    Else
            
    Return Null
        
    End If
    End Sub
    Here is the image downloader service module code save it as "HtmlViewImgDownloader",
    Code:
    #Region  Service Attributes
        
    #StartAtBoot: False
    #End Region

    Sub Process_Globals
        
    Private ongoingTasks As Map
    End Sub

    Sub Service_Create
        ongoingTasks.Initialize
    End Sub

    Sub Service_Start (StartingIntent As Intent)
        
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    End Sub

    Sub Service_Destroy

    End Sub

    Sub Download (ImageSource As ListAs ResumableSub
        
    For i = 0 To ImageSource.Size - 1
            
    Dim link As String = ImageSource.get(i)
            
    If ongoingTasks.ContainsKey(link) = False Then
                ongoingTasks.Put(link, 
    "")
                
    Dim imgName As String = Regex.Split("/",link)(Regex.Split("/",link).Length-1)
                
    If File.Exists(File.DirDefaultExternal & "/cache",imgName) And File.Size(File.DirDefaultExternal & "/cache",imgName)>0 Then
                    
    Try
                        
    ' check if image file is corrupted or not
                        Dim x As Bitmap 'ignore
                        x = LoadBitmapResize(File.DirDefaultExternal& "/cache",imgName,50dip,50dip,True)
                        ongoingTasks.Remove(link)
                    
    Catch
                        
    Dim j As HttpJob
                        j.Initialize(link, Me)
                        j.Download(link)
                        
    wait For (j) JobDone(j As HttpJob)
                        
    If j.Success Then
                            
    Dim bmp As B4XBitmap = j.GetBitmap
                            
    Dim os As OutputStream
                            os = 
    File.OpenOutput(File.DirDefaultExternal & "/cache",imgName,False)
                            
    If imgName.ToLowerCase.EndsWith(".png"Then
                                bmp.WriteToStream(os,
    100,"PNG")
                            
    Else
                                bmp.WriteToStream(os,
    100,"JPEG")
                            
    End If
                            os.Close
                            ongoingTasks.Remove(j.JobName)
                        
    End If
                        j.Release
                    
    End Try
                
    Else
                    
    Dim j As HttpJob
                    j.Initialize(link, Me)
                    j.Download(link)
                    
    wait For (j) JobDone(j As HttpJob)
                    
    If j.Success Then
                        
    Dim bmp As B4XBitmap = j.GetBitmap
                        
    Dim os As OutputStream
                        os = 
    File.OpenOutput(File.DirDefaultExternal & "/cache",imgName,False)
                        
    If imgName.ToLowerCase.EndsWith(".png"Then
                            bmp.WriteToStream(os,
    100,"PNG")
                        
    Else
                            bmp.WriteToStream(os,
    100,"JPEG")
                        
    End If
                        os.Close
                        ongoingTasks.Remove(j.JobName)
                    
    End If
                    j.Release
                
    End If
            
    End If
        
    Next
        
    Return True
    End Sub

    Sub ActivityIsPaused
        ongoingTasks.Clear
    End Sub
    Happy Coding :)
     
    Last edited: Jun 9, 2019
    Almora, Kiffi and JohnC like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice