Android Question custom views

Exspecto

Member
Licensed User
Longtime User
I've tried searching for this but either I'm not able to find it or the examples aren't making sense to me.

I am wanting to create a custom view that I can add dynamically to a tabhost. The view would consist of a label, an edittext (number style), and two buttons (one for + and one for -). The buttons would increment/decrement the number in the edittext view. I want to be able to initialize the view with the value for the label text and edittext.

I could just create each of these views in sequence while putting them in the right order into the tabhost panel, but I think it'd be easier if I could just make a view/class/etc. that contains all the separate views, including the functionality for each part of it. I've looked at some custom view samples, but I'm not seeing how they are created in such a way that I can say something like the following:

(assuming I've created my own custom standalone view type named "MyCustomView")

B4X:
Dim nInitialValueForEditText As Int
nInitialValueForEditText = 5

Dim custView as MyCustomView
custView.initialize("i'm the label", nInitialValueForEditText)

pnlTab.AddTab2("Main", lstMain)
lstMain.Panel.AddView(custView, 0, 0, 100dip, 50dip)

I'd like to be able to define a set size for the view just because it'd be easier, but that's probably not a good idea since it wouldn't be able to scale. How do I account for resizing of the custom view?

Can someone kindly point me to the right tutorial to read? Do I have to make a separate library in order to do what I'm wanting?
 

Exspecto

Member
Licensed User
Longtime User
I ended up making a class that creates a panel with the views I wanted, then returns that panel. I take the returned panel and add it to the scrollview:

B4X:
Dim oStat As StatView 'the class that creates and returns the panel of various views
oStat.Initialize("Label goes here", 20)

'calls "oStat.Panel" which returns the panel I created earlier
lstMain.Panel.AddView(oStat.Panel, 0, 150, 220dip, 40dip)

The class that creates the panel is as simple as can be:

B4X:
'Class module
Sub Class_Globals
   
   Private mainPanel As Panel
   Private statValue As Int
   Private lblStat As Label
   Private lblValue As Label
   Private btnPlus As Button
   Private btnMinus As Button
   
End Sub

Sub Initialize(sStatName As String, nValue As Int)
   
   mainPanel.Initialize("")
   lblStat.Initialize("")
   lblValue.Initialize("")
   
   lblStat.Gravity = Gravity.bottom
   lblValue.Gravity = Gravity.bottom

   btnMinus.Initialize("minus_button")
   btnPlus.Initialize("plus_button")
   btnMinus.Text = "-"
   btnPlus.Text = "+"
   
   lblStat.Text = sStatName
   setValue(nValue)
   
   mainPanel.AddView(lblStat, 0, 0, 80dip, 40dip)
   mainPanel.AddView(lblValue, lblStat.Left + lblStat.Width + 1, 0, 40dip, 40dip)
   mainPanel.AddView(btnPlus, lblValue.Left + lblValue.Width + 1, 0, 40dip, 40dip)
   mainPanel.AddView(btnMinus, btnPlus.Left + btnPlus.Width + 1, 0, 40dip, 40dip)

End Sub

Sub getPanel As Panel
   Return mainPanel
End Sub

Sub setValue(nVal As Int)
   
   statValue = nVal
   lblValue.Text = statValue
   
End Sub

Sub getValue As Int
   Return statValue
End Sub

Sub minus_button_Click
   setValue(statValue - 1)   
End Sub

Sub plus_button_Click
   setValue(statValue + 1)   
End Sub

All the examples I saw were a lot more complex. I'm sure what I have here is about as basic as it gets, but it's what I needed.
 
Upvote 0
Top