Cursussen/Courses Codesnippets     Top 
B4A-lists - Category dialog


1. Layout
To add a category you can use a dialog. Go to the designer window and make a new file. Call it category_dialog. In this layout you can place 1 EditText view and give it the name “edt1”. Leave some room for the dialog title bar.


2. Initialize
To use this layout you have to write some code.
In the Class_Globals method add the variable declarations for the B4Xdialog:
In the B4Xpage_Created add an initialization for the dialog and set some properties:
' in Class_Globals
Public catdialog As B4XDialog
Private edt1 As EditText

Private Sub B4XPage_Created (Root1 As B4XView)
	Root = Root1
	Root.LoadLayout("category_layout")
	B4XPages.SetTitle(Me,"Lists_textfiles_notes - Category")
	catdialog.Initialize(Root)
	catdialog.PutAtTop = True
	catdialog.BackgroundColor = Colors.LightGray
	catdialog.BodyTextColor = Colors.Blue
End Sub


3. Setup
In the btn2_Click subroutine we call the dialog and process the result of the dialog.
First we implement some more settings for the dialog.
private Sub setup_dialog(strlayout As String,lbltext As String) As Panel 
	Dim pnl As B4XView = xui.CreatePanel("")
	pnl.SetLayoutAnimated(0, 0, 0, 340dip, 150dip)
	pnl.LoadLayout(strlayout)
	Dim lbl As Label
	lbl.Initialize("")
	lbl.Text = lbltext
	lbl.TextSize = 20
	lbl.TextColor = Colors.Black
	lbl.Color = Colors.LightGray
	pnl.AddView(lbl,0,0,340dip,50dip)
	Return pnl
End Sub

Sub btn2_Click
	Dim pnl1 As Panel = setup_dialog("category_dialog","Add category")
	Wait For (catdialog.ShowCustom(pnl1, "OK", "", "CANCEL")) Complete (Result As Int)
	If Result = xui.dialogResponse_Positive Then
		If edt1.Text <> "" Then
			clv2.AddTextItem(edt1.text,edt1.text)
			If clv2.Size > 0 Then
				save_list
			End If
		End If
	End If
End Sub



4. Add
When you click on the Add button a dialog appears where you can type in a category name. If you click on OK the category will be added to the list. When you click on CANCEL no category will be added.


5. Change or delete
To change a category you need the following code in the subroutine clv2_ItemClick:
The dialog now has 3 buttons: SAVE, DELETE and CANCEL.
With the SAVE button you save the changes you have made to the EditText inputfield. If you make the field empty nothing will change!
With the DELETE button you will delete the item from the list and with the CANCEL button you will return to the list.
Sub clv2_ItemClick (Index As Int, Value As Object)
	Dim pnl1 As Panel = setup_dialog("category_dialog","Edit/Delete category")
	Dim rs1 As ResumableSub =  catdialog.ShowCustom(pnl1, "SAVE", "DELETE", "CANCEL")
	edt1.Text = clv2.GetValue(Index)
	Wait For (rs1) Complete (Result As Int)
	If Result = xui.dialogResponse_Positive Then
		If edt1.Text <> "" Then
			clv2.RemoveAt(Index)
			clv2.InsertAtTextItem(Index,edt1.text,edt1.text)
		End If
	else if Result = xui.dialogResponse_Negative Then
			clv2.RemoveAt(Index)
	End If
	If clv2.Size > 0 Then
		save_list
	End If
	'ToastMessageShow("item at position " & Index & " has value " & Value,True)
End Sub