Android Question Image on a button

Martin Domian

Member
Licensed User
Longtime User
I want to display a small image in the top right corner of a button which indicates the status of the device. The device behaves differently when clicked on this button.
Now I wanted to put an image view on the button and bring it to the foreground. But that only works if the status of the button is Enabled = false. If I set it to True, the image is not visible.

Does anyone have an idea?
 

Alexander Stolte

Expert
Licensed User
Longtime User
Have a look at this lib. maybe it is a solution for you:
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Don't forget that a button has an elevation greater than zero so an eventual image should have a greater valute for that property
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
I want to display a small image in the top right corner of a button which indicates the status of the device. The device behaves differently when clicked on this button.
Now I wanted to put an image view on the button and bring it to the foreground. But that only works if the status of the button is Enabled = false. If I set it to True, the image is not visible.

Does anyone have an idea?
Try this:
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
    Private xui as XUI
End Sub

Sub Globals
    Private  button1 As Button
    Private imgStatus As Bitmap
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")

    ' Load the status image
    
    imgStatus = LoadBitmap(File.DirAssets, "setting.png")

    UpdateButtonStatus(button1, imgStatus)
            
End Sub

Sub UpdateButtonStatus(btn As Button, img As Bitmap)
    ' Create a Canvas to draw on the button
    Dim cvs As Canvas
    cvs.Initialize(btn)
    
    ' Calculate the position for the status image (top right corner)
    Dim imgSize As Int = 24dip ' Size of the status image
    Dim left As Int = btn.Width - imgSize - 5dip ' 5dip padding from the right edge
    Dim top As Int = 5dip ' 5dip padding from the top edge
    
    ' Draw the status image
    
    Dim Rect As Rect
    Rect.Initialize(left, top, left + imgSize, top + imgSize)
    cvs.DrawBitmap(img, Null, Rect)
    Activity.Invalidate
End Sub
Private Sub Activity_LongClick
    button1.Enabled= Not(button1.Enabled)
    ' Update the button to show the status icon
    If button1.Enabled =True Then
        UpdateButtonStatus(button1, imgStatus)
    Else
        UpdateButtonStatus(button1, imgStatus)
    End If
End Sub

Private Sub Button1_Click
    xui.MsgboxAsync("clicked","clicked")
End Sub
 

Attachments

  • setting.png
    setting.png
    16.5 KB · Views: 67
Upvote 1

Martin Domian

Member
Licensed User
Longtime User
Try this:
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
    Private xui as XUI
End Sub

Sub Globals
    Private  button1 As Button
    Private imgStatus As Bitmap
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")

    ' Load the status image
   
    imgStatus = LoadBitmap(File.DirAssets, "setting.png")

    UpdateButtonStatus(button1, imgStatus)
           
End Sub

Sub UpdateButtonStatus(btn As Button, img As Bitmap)
    ' Create a Canvas to draw on the button
    Dim cvs As Canvas
    cvs.Initialize(btn)
   
    ' Calculate the position for the status image (top right corner)
    Dim imgSize As Int = 24dip ' Size of the status image
    Dim left As Int = btn.Width - imgSize - 5dip ' 5dip padding from the right edge
    Dim top As Int = 5dip ' 5dip padding from the top edge
   
    ' Draw the status image
   
    Dim Rect As Rect
    Rect.Initialize(left, top, left + imgSize, top + imgSize)
    cvs.DrawBitmap(img, Null, Rect)
    Activity.Invalidate
End Sub
Private Sub Activity_LongClick
    button1.Enabled= Not(button1.Enabled)
    ' Update the button to show the status icon
    If button1.Enabled =True Then
        UpdateButtonStatus(button1, imgStatus)
    Else
        UpdateButtonStatus(button1, imgStatus)
    End If
End Sub

Private Sub Button1_Click
    xui.MsgboxAsync("clicked","clicked")
End Sub
Thanks, that could work, i will try
 
Upvote 0

Martin Domian

Member
Licensed User
Longtime User
Try this:
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
    Private xui as XUI
End Sub

Sub Globals
    Private  button1 As Button
    Private imgStatus As Bitmap
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")

    ' Load the status image
   
    imgStatus = LoadBitmap(File.DirAssets, "setting.png")

    UpdateButtonStatus(button1, imgStatus)
           
End Sub

Sub UpdateButtonStatus(btn As Button, img As Bitmap)
    ' Create a Canvas to draw on the button
    Dim cvs As Canvas
    cvs.Initialize(btn)
   
    ' Calculate the position for the status image (top right corner)
    Dim imgSize As Int = 24dip ' Size of the status image
    Dim left As Int = btn.Width - imgSize - 5dip ' 5dip padding from the right edge
    Dim top As Int = 5dip ' 5dip padding from the top edge
   
    ' Draw the status image
   
    Dim Rect As Rect
    Rect.Initialize(left, top, left + imgSize, top + imgSize)
    cvs.DrawBitmap(img, Null, Rect)
    Activity.Invalidate
End Sub
Private Sub Activity_LongClick
    button1.Enabled= Not(button1.Enabled)
    ' Update the button to show the status icon
    If button1.Enabled =True Then
        UpdateButtonStatus(button1, imgStatus)
    Else
        UpdateButtonStatus(button1, imgStatus)
    End If
End Sub

Private Sub Button1_Click
    xui.MsgboxAsync("clicked","clicked")
End Sub

Very cool!
Copy Paste and it works!
Great!

THANX YOU
 
Upvote 0
Top