B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=8.3
@EndOfDesignText@
' Update 1.0.2 (04/01/2018)
#Event: ItemClick (Position As Int, Value as String)
#DesignerProperty: Key: TextColor, DisplayName: TextColor, FieldType: Color, DefaultValue: 0xFF000000,Description: Text color
#DesignerProperty: Key: TextSize, DisplayName: TextSize, FieldType: int, DefaultValue: 14,Description: Label Text Size 
#DesignerProperty: Key: HintColor, DisplayName: HintColor, FieldType: Color, DefaultValue: 0xFF888888,Description: Hint color, noselected opstion
#DesignerProperty: Key: BackgroundColor, DisplayName: Color background, FieldType: Color, DefaultValue: 0xFFFFFFFF, Description: You can use the built-in color picker to find the color values.
#DesignerProperty: Key: BackgroundColorList, DisplayName: ListColor background, FieldType: Color, DefaultValue: 0xFF000000, Description: You can use the built-in color picker to find the color values.
#DesignerProperty: Key: GrayDisplay, DisplayName: Gray Display, FieldType: boolean, DefaultValue: False, Description: Gray Display.
#DesignerProperty: Key: RoundCorner, DisplayName:RoundCorner, FieldType: boolean, DefaultValue: False, Description: RoundCorner.

Sub Class_Globals
	Public SelectedItem As String
	Public SelectedID As String
	Public SelectedIndex As Int
	Public GrayDisplay As Boolean
	Public BColor, TColor,HintColor, BColorList As Int
	Public RoundCorner As Boolean
	Public ScrollViewParent As ScrollView = Null
	
	Private EventName As String 'ignore
	Private CallBack As Object 'ignore
	Private mBase As Panel
	Public MyAct As Panel
	Private Lab As Label
	Private AppPanel As Panel
	Public Top,Left As Int
	Private ListPanel As ScrollView
	
	Private ListItem As List
	Private TextSize As Int
	Type Item (Text As String,TextHelp As String,ID As String, BackgroundColor As Int, TextColor As Int, Image As Bitmap, G As Int)
End Sub

Public Sub Initialize (vCallback As Object, vEventName As String)
	EventName = vEventName
	CallBack = vCallback
	ListItem.Initialize
	Lab.Initialize("Lab")
	SelectedItem=""
	SelectedID=""
	SelectedIndex=-1
	
	AppPanel.Initialize("AppPanel")
	ListPanel.Initialize(1000dip)
End Sub

Public Sub Invalidate
	mBase.Invalidate
End Sub

Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
	mBase = Base
	mBase.Tag=Me
	
	Left=0
	Top=0
	Try
		Dim Obj As View = mBase
		Log(GetType(Obj))
		Do While Not(GetType(Obj) = "android.widget.FrameLayout")
			Left=Left+Obj.Left
			Top=Top+Obj.Top
			Obj=Obj.Parent
		Loop
		MyAct=Obj
	Catch
		Dim r As Reflector
		r.Target = r.GetActivityBA
		MyAct=r.GetField("vg")
	End Try
	
	If Props.ContainsKey("BackgroundColor") Then BColor=Props.Get("BackgroundColor")
	If Props.ContainsKey("BackgroundColorList") Then BColorList=Props.Get("BackgroundColorList")
	If Props.ContainsKey("TextColor") Then TColor=Props.Get("TextColor")
	If Props.ContainsKey("TextSize") Then TextSize=Props.Get("TextSize")
	If Props.ContainsKey("HintColor") Then HintColor=Props.Get("HintColor")
	If Props.ContainsKey("GrayDisplay") Then GrayDisplay=Props.Get("GrayDisplay")
	If Props.ContainsKey("RoundCorner") Then RoundCorner=Props.Get("RoundCorner")
	
	Lab.TextColor=TColor
	Lab.Color=BColor
	Lab.Gravity=Gravity.CENTER
	Lab.TextSize=TextSize
	Lab.BringToFront
	SetIndex(SelectedIndex)
	mBase.AddView(Lab,0,0,mBase.Width,mBase.Height)
	
	Dim Ima As Panel
	Ima.Initialize("")
	LoadDrawableByName(Ima,"dropdown_ic_arrow_normal_holo_light")
	mBase.AddView(Ima,mBase.Width-40dip,(mBase.Height/2)-20dip,40dip,40dip)

End Sub

Private Sub LoadDrawableByName(Control As View, ImageName As String) As Boolean
	Dim R As Reflector
	Try
		R.Target = R.GetContext
		R.Target = R.RunMethod("getResources")
		R.Target = R.RunMethod("getSystem")
		Dim ID_Drawable As Int
		ID_Drawable = R.RunMethod4("getIdentifier", Array As Object(ImageName, "drawable", "android"), _
		                                            Array As String("java.lang.String", "java.lang.String", "java.lang.String"))
		R.Target = R.GetContext
		R.Target = R.RunMethod("getResources")
		Control.Background = R.RunMethod2("getDrawable", ID_Drawable, "java.lang.int")
		Return True
		
	Catch
		LogColor("ERROR.LoadDrawableByName:" & LastException.Message, Colors.Red)
		Return False
		
	End Try
End Sub

Public Sub GetBase As Panel
	Return mBase
End Sub

#Region Private 

private Sub Lab_Click
	Dim I As IME
	I.Initialize("")
	I.HideKeyboard
	
	DesignerList
End Sub

Private Sub AppPanel_Touch (Action As Int, X As Float, Y As Float)
	'AppPanel.Visible=False
	AppPanel.RemoveView
End Sub

Sub Corner(Colore As Int) As ColorDrawable
	Dim cdb As ColorDrawable
	cdb.Initialize(Colore, 20dip)
	Return cdb
End Sub

Sub RoundBorders(B0 As Bitmap, radius As Int) As Bitmap

	Dim B1,B2 As Bitmap
	Dim cv As Canvas
	Dim Rect1 As Rect
    
	B2.InitializeMutable(40dip,40dip)
	cv.Initialize2(B2)
	Dim Drawable1 As ColorDrawable
	Drawable1.Initialize(Colors.White,radius)
	Rect1.Initialize(0,0,B2.Width-1,B2.Height-1)
	cv.DrawDrawable(Drawable1,Rect1)

	B1.InitializeMutable(40dip,40dip)
	cv.Initialize2(B1)
	cv.DrawBitmap(B0,Null,Rect1)
      
	For r=0 To B2.Height-1
		For c=0 To B2.Width-1
			If B2.GetPixel(c,r)=0 Then
				cv.DrawPoint(c,r,0)
			End If
		Next
	Next
	cv.DrawCircle(20dip,20dip,20dip,Colors.Black,False,1dip)
	'P.SetBackgroundImage(B1)
	Return B1
End Sub

private Sub DesignerList
	Dim HeightListView As Int = 0
	Dim Space As Int
	
	If ListItem.size>0 Then
		If AppPanel.IsInitialized Then AppPanel.RemoveView
		MyAct.addview(AppPanel,0,0,100%x,100%Y)
				
		If RoundCorner Then	ListPanel.Color=Colors.Transparent Else ListPanel.Color=BColorList
		ListPanel.RemoveView
		ListPanel.Panel.RemoveAllViews
		ListPanel.Panel.Height=0

		For i=0 To ListItem.Size-1
			Dim Item As Item
			Item.Initialize
			
			Item = ListItem.Get(i)
			Dim P As Panel
			P.Initialize("PanelClick")
			If RoundCorner Then P.Background=Corner(Item.BackgroundColor) Else P.Color=Item.BackgroundColor
			
			P.Tag=I
			Try
				Space=40dip
				Dim Ima As ImageView
				Ima.Initialize("")
				Ima.Gravity=Gravity.FILL
				If RoundCorner Then
					Ima.SetBackgroundImage(RoundBorders(Item.Image,20dip))
				Else
					Ima.SetBackgroundImage(Item.Image)
				End If
				
				P.AddView(Ima,0dip,5dip,40dip,40dip)
			Catch
				Space=0dip
			End Try
			
			Dim La As Label
			La.Initialize("")
			La.Text=Item.text
			La.TextSize=15
			La.Color=Colors.Transparent
			La.TextColor=Item.TextColor
			La.Typeface=Typeface.DEFAULT_BOLD
			La.Gravity=Item.G
			
			P.AddView(La,Space,0dip,mBase.Width-Space,30dip)
			If Item.texthelp.Trim<>"" Then
				Dim La As Label
				La.Initialize("")
				La.Text=Item.TextHelp
				La.TextSize=12
				La.Color=Colors.Transparent
				La.TextColor=Item.TextColor
				La.Gravity=Item.G
				P.AddView(La,Space,25dip,mBase.Width-Space,30dip)
			Else
				La.Height=50dip
			End If
			ListPanel.Panel.AddView(P,-mBase.Width,HeightListView,mBase.Width-2DIP,49dip)
			P.SetLayoutAnimated(100+(HeightListView/3),1dip,HeightListView,mBase.Width-2DIP,49dip)
			HeightListView=HeightListView+50dip
		Next
		ListPanel.Panel.Height=HeightListView
		
		Dim LeftSpinner,TopSpinner,HeightSpinner,Yafter As Int
		
		If GrayDisplay Then AppPanel.Color=Colors.aRGB(200,0,0,0) Else AppPanel.Color=Colors.Transparent
		If ScrollViewParent.IsInitialized Then
			If Left+mBase.Width+ScrollViewParent.Left>100%x Then LeftSpinner=100%x-mBase.Width Else LeftSpinner=Left
		Else
			If Left+mBase.Width>100%x Then LeftSpinner=100%x-mBase.Width Else LeftSpinner=Left
		End If
		
		Yafter=100%y-Top-mBase.Height
		If ScrollViewParent.IsInitialized Then
			Yafter=Yafter+ScrollViewParent.ScrollPosition
			Top=Top-ScrollViewParent.ScrollPosition
		End If
		If (HeightListView>Top)And (HeightListView>Yafter) Then
			' Non va bene dappertutto
			If (Top>100%y-Top-mBase.Height) Then
				'sopra é più grande
				TopSpinner=0dip
				HeightSpinner=Top
			Else
				'sotto è più grande
				TopSpinner=Top+mBase.Height
				HeightSpinner=100%y-Top-mBase.Height
			End If
		Else if (HeightListView>Top) And (HeightListView<=Yafter) Then
			' Va bene di sotto
			TopSpinner=Top+mBase.Height
			HeightSpinner=HeightListView
		Else If (HeightListView<=Top)And (HeightListView>Yafter) Then
			' Va bene di sopra
			TopSpinner=Top+mBase.Height-HeightListView
			HeightSpinner=HeightListView
		Else
			' Haqspazio sopra e sotto
			TopSpinner=Top+mBase.Height
			HeightSpinner=HeightListView
		End If
		
		If ScrollViewParent.IsInitialized=False Then
			AppPanel.AddView(ListPanel,LeftSpinner,TopSpinner,mBase.Width,HeightSpinner)
		Else
			AppPanel.AddView(ListPanel,LeftSpinner,TopSpinner,mBase.Width,HeightSpinner)
		End If
		'AppPanel.Visible=True
	End If
End Sub

Private Sub PanelClick_Click
	Dim P As Panel = Sender
	Dim Position As Int = P.Tag
	
	SelectedIndex=Position
	SelectedID=GetID(Position)
	SelectedItem=GetItem(Position)
	Lab.TextColor=TColor
	Lab.Text=SelectedItem
	Lab.BringToFront
	If SubExists(CallBack,EventName & "_ItemClick") Then CallSub3(CallBack,EventName & "_ItemClick",Position,GetItem(Position))
	
	'AppPanel.Visible=False
	AppPanel.RemoveView
End Sub
 
 #End Region
 
 #Region Method
  
Public Sub Add(Text As String, TextHelp As String,ID As String, BackgroundColor As Int, TextColor As Int, Gravities As Int,Image As Bitmap)
	Dim Item As Item
	
	Item.Initialize
	Item.Text=Text
	Item.TextHelp=TextHelp
	Item.ID=ID
	Item.BackgroundColor=BackgroundColor
	Item.TextColor=TextColor
	Item.G=Gravities
	'Log(Image)
	If Image.IsInitialized Then
		Item.Image=Image
	Else
		Dim Bmp As Bitmap
		Bmp=Null
		Item.Image=Bmp
	End If
	ListItem.Add(Item)
	
End Sub

Public Sub AddAll(L As List, TextHelp As String, BackgroundColor As Int, TextColor As Int, Gravities As Int)
	Dim Bmp As Bitmap = Null
	
	For i=0 To L.Size-1
		Dim Item As Item
		Item.Text=L.Get(i)
		Item.TextHelp=TextHelp
		Item.ID=i
		Item.BackgroundColor=BackgroundColor
		Item.TextColor=TextColor
		Item.G=Gravities
		Item.Image=Bmp
		ListItem.Add(Item)
	Next
End Sub
 
public Sub Size As Int
	Return ListItem.Size
End Sub

Public Sub Clear
	ListItem.Clear
	DesignerList
	SelectedIndex=-1
	SelectedID=""
	SelectedItem=""
	Lab.Textcolor=HintColor
	Lab.Text="Seleccionar Categoria"
	Lab.BringToFront
End Sub

Public Sub RemoveIndex(Position As Int)
	If Position>-1 And Position<ListItem.Size Then ListItem.RemoveAt(Position)
End Sub

Public Sub RemoveID(ID As String)
	Dim Position As Int = -1
	
	For i=0 To ListItem.Size-1
		Dim Item As Item = ListItem.Get(i)
		If Item.ID=ID Then Position=i
	Next
	If Position>-1 Then ListItem.RemoveAt(Position)
End Sub

Public Sub GetItem(Position As Int) As String
	Dim Ret As String = ""
	If Position>-1 And Position<ListItem.Size Then
		Dim Item As Item = ListItem.Get(Position)
		Ret=Item.text
	End If
	Return Ret
End Sub

Public Sub GetHelpText(Position As Int) As String
	Dim Ret As String = ""
	If Position>-1 And Position<ListItem.Size Then
		Dim Item As Item = ListItem.Get(Position)
		Ret=Item.TextHelp
	End If
	Return Ret
End Sub

Public Sub GetItemFromID(ID As String) As String
	Dim Ret As String = ""
	
	For i=0 To ListItem.Size-1
		Dim Item As Item = ListItem.Get(i)
		If Item.ID=ID Then Ret=Item.text
	Next
	Return Ret
End Sub

Public Sub GetID(Position As Int) As String
	Dim Ret As String = ""
	If Position>-1 And Position<ListItem.Size Then
		Dim Item As Item = ListItem.Get(Position)
		Ret=Item.ID
	End If
	Return Ret
End Sub

Public Sub SetLabelTextSize(TextSizeLabel As Int)
	TextSize=TextSizeLabel
	Lab.Textsize=TextSizeLabel
End Sub

Public Sub SetIndex(Position As Int)
	If Position>-1 And Position<ListItem.Size Then
		'Dim Item As Item = ListItem.Get(Position)
		SelectedIndex=Position
		SelectedID=GetID(Position)
		SelectedItem=GetItem(Position)
		Lab.TextColor=TColor
		Lab.Text=SelectedItem
		Lab.BringToFront
	else if Position=-1 Then
		SelectedIndex=Position
		SelectedID=""
		SelectedItem=""
		Lab.Textcolor=HintColor
		Lab.Text="Seleccionar Categoria"   '''**** TEXTO INICIAL  DEL SPPINER
		Lab.BringToFront
	End If
End Sub

Public Sub SetID(ID As String)
	For i=0 To ListItem.Size-1
		Dim Item As Item = ListItem.Get(i)
		If Item.ID=ID Then
			SelectedIndex=i
			SelectedID=Item.id
			SelectedItem=Item.text
			Lab.TextColor=TColor
			Lab.Text=SelectedItem
			Lab.BringToFront
		End If
	Next
End Sub

Public Sub setValue(Value As String)
	For i=0 To ListItem.Size-1
		Dim Item As Item = ListItem.Get(i)
		If Item.Text=Value Then
			SelectedIndex=i
			SelectedID=Item.id
			SelectedItem=Item.text
			Lab.TextColor=TColor
			Lab.Text=SelectedItem
			Lab.BringToFront
		End If
	Next
End Sub

Public Sub SetBackgroundDrawable(Draw As ColorDrawable)
	mBase.Background=Draw
End Sub

Public Sub SetBackgroundImage(Bitmap As Bitmap)
	mBase.SetBackgroundImage(Bitmap)
End Sub
 
Public Sub Close
	AppPanel.RemoveView
End Sub
  
Public Sub Oper
	DesignerList
End Sub

Public Sub setEnabled(B As Boolean)
	Lab.Enabled=B
End Sub

Public Sub getEnabled As Boolean
	Return Lab.Enabled
End Sub
 
#End Region
