Android Question Set Image to Button that image source from url

Ajws

Member
Licensed User
Hi all,

help me...

how can i set image to button, image from online source (URL) not offline source..


Thank you
 

Ajws

Member
Licensed User
i still get error, this is my code..help...

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    DownloadImage("https://www.b4x.com/images3/android.png", Button1)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub DownloadImage(Link As String, iv As Button)
    Dim job As HttpJob
    job.Initialize("", Me) 'note that the name parameter is no longer needed.
    job.Download(Link)
    Wait For JobDone(job As HttpJob)
    If job.Success Then
    '    iv.SetImage (job.GetBitmap) 'replace with iv.Bitmap = job.GetBitmap in B4A / B4i
        iv.Bitmap = job.GetBitmap
    End If
    job.Release
End Sub
This would be a very good start ... see example #5 of 1st post.

https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/
 
Upvote 0

Ajws

Member
Licensed User
i still get error, this is my code..help...

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    DownloadImage("https://www.b4x.com/images3/android.png", Button1)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub DownloadImage(Link As String, iv As Button)
    Dim job As HttpJob
    job.Initialize("", Me) 'note that the name parameter is no longer needed.
    job.Download(Link)
    Wait For JobDone(job As HttpJob)
    If job.Success Then
    '    iv.SetImage (job.GetBitmap) 'replace with iv.Bitmap = job.GetBitmap in B4A / B4i
        iv.Bitmap = job.GetBitmap
    End If
    job.Release
End Sub
This would be a very good start ... see example #5 of 1st post.

https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
always post any error messages to help us solve your problem.

Do you have a layout containing a Button which you forgot to load ?
If not create a new layout "Layout1" and add a Button to it. Then ....

also the correct code should be .. myButton.SetBackgroundImage (job.GetBitmap)

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    DownloadImage("https://www.b4x.com/images3/android.png", Button1)
End Sub

Sub DownloadImage(Link As String, myButton As Button)
    Dim job As HttpJob
    job.Initialize("", Me) 'note that the name parameter is no longer needed.
    job.Download(Link)
    Wait For JobDone(job As HttpJob)
    If job.Success Then
        myButton.SetBackgroundImage (job.GetBitmap)
    End If
    job.Release
End Sub
 
Last edited:
Upvote 0

Ajws

Member
Licensed User
always post any error messages to help us solve your problem.

Do you have a layout containing a Button which you forgot to load ?
If not create a new layout "Layout1" and add a Button to it. Then ....

also the correct code should be .. myButton.SetBackgroundImage (job.GetBitmap)

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    DownloadImage("https://www.b4x.com/images3/android.png", Button1)
End Sub

Sub DownloadImage(Link As String, myButton As Button)
    Dim job As HttpJob
    job.Initialize("", Me) 'note that the name parameter is no longer needed.
    job.Download(Link)
    Wait For JobDone(job As HttpJob)
    If job.Success Then
        myButton.SetBackgroundImage (job.GetBitmap)
    End If
    job.Release
End Sub

ok mangojack it solved my problem..

thank you very much...

Done...
 
Upvote 0
Top