without seeing what you have done and how it i hard to give advices.
Maybe you set the horizontal anchor to left
'SetLayout
Panel1.SetLayout(0%x,0%y,100%x,100%y)
ImageView1.SetLayout(50%x,60%y,100dip,100dip)
Don't use setLayout but Left and Top, you are positioning the image view in the panel.
Still having the same result. Question: When an imageview is added and no image is specified in the designer, I still get a white box showing on the screen when I run the app. The box is in the same position as when I tried setting the imageview with the setLayout. Now I have :Ahhh, ok. Let me try that.
Thanks,
schemer
ImageView1.Left = 50%x
ImageView1.Top = 50%y
thePanel.SetLeftAndRight(0dip, 100%x)
thePanel.SetTopAndBottom(0dip, 100%y)
myImageView.HorizontalCenter = thePanel.Width / 2
myImageView.VerticalCenter = thePanel.Height / 2
If you are using the designer scripts then you should have thing like this:
B4X:thePanel.SetLeftAndRight(0dip, 100%x) thePanel.SetTopAndBottom(0dip, 100%y) myImageView.HorizontalCenter = thePanel.Width / 2 myImageView.VerticalCenter = thePanel.Height / 2
Assuming of course you have the ImageView inside the panel.
The code I posted is to be used with the designer scripts.
The best way is to use the designer, keeps your code tidy, all the "layout" stuff is on the designer and the "logic" on the IDE, however, if you still want to create your layouts by code then the code I posted has to be modified, so yes, it is also possible.
If you are using the designer scripts then you should have thing like this:
B4X:thePanel.SetLeftAndRight(0dip, 100%x) thePanel.SetTopAndBottom(0dip, 100%y) myImageView.HorizontalCenter = thePanel.Width / 2 myImageView.VerticalCenter = thePanel.Height / 2
Assuming of course you have the ImageView inside the panel.
Sub AdjustImageView(oImgView As ImageView, oBitmap As Bitmap)
Dim Delta, Height, Width As Int
If oBitmap.Width / oBitmap.Height > oImgView.Width / oImgView.Height Then
Height = oBitmap.Height / oBitmap.Width * oImgView.Width
Delta = (oImgView.Height - Height) / 2
oImgView.Height = Height
oImgView.Top = oImgView.Top + Delta
Else
Width = oBitmap.Width / oBitmap.Height * oImgView.Height
Delta = (oImgView.Width - Width) / 2
oImgView.Width = Width
oImgView.Left = oImgView.Left + Delta
End If
oImgView.Gravity = Gravity.FILL
oImgView.Bitmap = oBitmap
End Sub
Sub BuildCustomListView(result As DBResult)
'This is the CustomList View Control
clvSchemes.Clear
For Each records() As Object In result.Rows
'Creating the Panel and other views to be placed on the Panel. Finally this Panel is added to the CustomListView
Dim oPanel As Panel
oPanel.Initialize("")
oPanel.Color = Colors.White
'This MSCardView will hold the Image Title and the Image. This CardView will be added to the above Panel
Dim oMsCardView As MSCardView
oMsCardView.Initialize("")
oMsCardView.MaxElevation = 4dip
oMsCardView.Elevation = 4dip
oMsCardView.Radius = 12
'The label used to display the ImageTitle on the MSCardView
Dim oLblSchemeName As Label
oLblSchemeName.Initialize("")
oLblSchemeName.Text = records(result.Columns.Get("Title"))
oLblSchemeName.Gravity = Gravity.CENTER_HORIZONTAL
oLblSchemeName.TextSize = 18
oLblSchemeName.TextColor = Colors.Gray
oLblSchemeName.Typeface = Typeface.DEFAULT_BOLD
'This is to read image Data from Table and convert it to a Bitmap and the Bitmap will be displayed on the ImageView
Dim Buffer() As Byte
Dim oBitmap As Bitmap
Buffer = records(result.Columns.Get("Picture_Small"))
If Buffer = Null Then
Else
oBitmap = reqManager.BytesToImage(Buffer)
End If
Dim oImgView As ImageView
oImgView.Initialize("")
oImgView.Bitmap = oBitmap
oImgView.Gravity = Gravity.FILL
' Adding MSCardView to the Panel. This should be done first before setting the height and other properties of MsCardView
oPanel.AddView(oMsCardView ,0%x , 05dip, 98%x , 225dip )
'Setting up MsCardView Properties. Please note that this can be done only after adding MsCardView to the Panel as given above
oMsCardView.Height = 245dip
oMsCardView.Panel.AddView(oLblSchemeName ,0%x , 10dip, 100%x , 25dip )
oMsCardView.Panel.AddView(oImgView ,02%x , 30dip, 330dip, 200dip)
' Now Add Panel to the CustomListView
clvSchemes.Add(oPanel, 255dip, records(result.Columns.Get("ID")) )
'Adjust the ImageView based on the Picture's display ratio
AdjustImageView(oImgView, oBitmap, oPanel)
Next
End Sub
Sub AdjustImageView(oImgView As ImageView, oBitmap As Bitmap, oPanel As Panel)
Dim Delta, Height, Width As Int
If oBitmap.Width / oBitmap.Height > oImgView.Width / oImgView.Height Then
Height = oBitmap.Height / oBitmap.Width * oImgView.Width
Delta = (oImgView.Height - Height) / 2
oImgView.Height = Height
oImgView.Top = oImgView.Top + Delta
Else
Width = oBitmap.Width / oBitmap.Height * oImgView.Height
Delta = (oImgView.Width - Width) / 2
oImgView.Width = Width
oImgView.Left = oImgView.Left + Delta
End If
oImgView.Gravity = Gravity.FILL
'oImgView.Left = ( oPanel.Width - Width)/2 'Not Working so commented out
'oImgView.Top = (oPanel.Height - Height)/2 'Not Working so commented out
oImgView.Bitmap = oBitmap
End Sub
Ohanian,Thank you for the help. Unfortunately Bit.OR(Gravity.FILL , Gravity.CENTER) did not resolve the issue.try this :
B4X:oImgView.Gravity = Bit.OR(Gravity.FILL , Gravity.CENTER)
Shall try to post as a project to test it here.Can you post a small project showing the problem so we could test it.
Here is a sample project that demonstrates the issue.Can you post a small project showing the problem so we could test it.
#Region Project Attributes
#ApplicationLabel: ImgView Centered
#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.
Private clvSchemes As CustomListView
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("Main")
PrepareClv
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub PrepareClv
Dim i As Int : i=1
Dim cImageName As String
clvSchemes.Clear
For i=1 To 6
'Image File names. For Eg Picture1.png, Picture2.png, Picture3.png
cImageName="Picture" & i & ".png"
'Creating the Panel and other views to be placed on the Panel. Finally this Panel is added to the CustomListView
Dim oPanel As Panel
oPanel.Initialize("")
oPanel.Color = Colors.White
'The label used to display the Image Title
Dim oLblTitle As Label
oLblTitle.Initialize("")
oLblTitle.Text = cImageName
oLblTitle.Gravity = Gravity.CENTER_HORIZONTAL
oLblTitle.TextSize = 18
oLblTitle.TextColor = Colors.Gray
oLblTitle.Typeface = Typeface.DEFAULT_BOLD
' Create Bitmap
Dim oBitmap As Bitmap
oBitmap.Initialize(File.DirAssets, cImageName)
Dim oImgView As ImageView
oImgView.Initialize("")
oImgView.Bitmap = oBitmap
'oImgView.Gravity = Gravity.FILL
oPanel.AddView(oLblTitle ,0%x , 10dip, 100%x , 25dip )
oPanel.AddView(oImgView ,02%x , 30dip, 330dip, 200dip)
'Adjust ImageView based on the the Original Pictures aspect Ratio, so that the image is not stretched or disorted
AdjustImageView(oImgView, oBitmap, oPanel)
' Now Add Panel to the CustomListView
clvSchemes.Add(oPanel, 255dip, i )
Next
End Sub
Sub AdjustImageView(oImgView As ImageView, oBitmap As Bitmap, oPanel As Panel)
Dim Delta, Height, Width As Int
If oBitmap.Width / oBitmap.Height > oImgView.Width / oImgView.Height Then
Height = oBitmap.Height / oBitmap.Width * oImgView.Width
Delta = (oImgView.Height - Height) / 2
oImgView.Height = Height
oImgView.Top = oImgView.Top + Delta
Else
Width = oBitmap.Width / oBitmap.Height * oImgView.Height
Delta = (oImgView.Width - Width) / 2
oImgView.Width = Width
oImgView.Left = oImgView.Left + Delta
End If
'oImgView.Gravity = Gravity.FILL
oImgView.Gravity = Bit.Or(Gravity.FILL , Gravity.CENTER)
'oImgView.Left = ( oPanel.Width - Width)/2
'oImgView.Top = (oPanel.Height - Height)/2
oImgView.Bitmap = oBitmap
End Sub