Android Question How to place any Text to a button at CustomlistView

AndroidMadhu

Active Member
Licensed User
Hello,
I am using CustomlistView and placed button dynamically.
I mam using below code to create the button at Customlistview. Now I wanted to place a text [For example : Click or Expand or Any text].
How do I place a text on the button at customListview.
Below is my code for creating buttons dynamically
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")
    'CLV1.DefaultTextBackgroundColor=Colors.Gray
    For i = 0 To 100
        CLV1.Add(createItem("Item #" & i,CLV1.AsView.Width,200dip),i)   
    Next

End Sub

private Sub createItem(Text As String,width As Int,height As Int) As B4XView
    Dim p As B4XView=xui.CreatePanel("")
    Dim btnxpan As Button
    btnxpan.Initialize("")
    p.SetLayoutAnimated(0,0,0,100%x,10%x)
    p.Color=Colors.White
    btnxpan.Color=Colors.Gray
    btnxpan.TextColor=Colors.Black
    btnxpan.Width=70%x
    p.AddView(btnxpan,90%x,10dip,10dip,10dip)
    Return p
End Sub
 

emexes

Expert
Licensed User
Perhaps I am going blind, but my first question was: why are you passing the width and height parameters to createItem, and then seemingly not using them?

Then I realised the Text parameter doesn't get used either, and that's probably the reason for your "how do I place text on the button" question.

Adding btnxpan.Text = Text to the createItem routine will probably fix the text issue.

I think you may have dodged a bullet with the non-use of the height and width parameters, by sizing the button relative to the Activity.
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
Adding btnxpan.Text = Text to the createItem routine will probably fix the text issue.
I have used this..... but no luck.... I used like below
B4X:
btnxpan.Text="Click"
Is that correct approach?
 
Upvote 0

emexes

Expert
Licensed User
I have used this..... but no luck.... I used like below
B4X:
btnxpan.Text="Click"
Is that correct approach?
Yes, but... I'm surprised that it hasn't worked. Post the code including that line that did not work, and hopefully we can work out why.

Also, is anything appearing in your CLV? Perhaps the issue isn't that we can't see the text, perhaps it's that we can't see the buttons or their containing panels.
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
I am uploading the full code below
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")
    CLV1.DefaultTextBackgroundColor=Colors.Gray
    CLV1.PressedColor=Colors.White
    For i = 0 To 100
        CLV1.Add(createItem("Item #" & i,CLV1.AsView.Width,200dip),i)
    Next
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

private Sub createItem(Text As String,width As Int,height As Int) As B4XView
    Dim p As B4XView=xui.CreatePanel("")
    Dim btnxpan As Button
    btnxpan.Initialize("")
    p.SetLayoutAnimated(0,0,0,100%x,10%x)
    p.Color=Colors.White
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Text=Text
    lbl.TextColor=Colors.Black
    p.AddView(lbl,12dip,10dip,100dip,100dip)
    btnxpan.Color=Colors.LightGray
    btnxpan.TextColor=Colors.Black
    btnxpan.Text="Click"
    p.AddView(btnxpan,CLV1.AsView.Width-55dip,10dip,50dip,height-4dip)
    Return p
End Sub
 
Upvote 0

emexes

Expert
Licensed User
I'm away ("on holiday") for a few days and there's no wifi at this place, and without a router in the middle between my laptop and the Android device I've got here, I'm having trouble getting stuff to compile and run. So it might take me a while to get back to you with a definitive answer (if anyone else helps you in the meantime, I won't be offended :)

But some stuff that immediately jumped out at me was:

1/ first thing to do when something isn't working is to go back to bare basics... can you temporarily get rid of the animation, see if that makes a difference?

2/ you're sneaking in a reference to a global variable in the createItem sub, with "CLV.AsView.Width-55dip"... yeah it should work, but it has a banana-peel-waiting-to-trip-us-up feel about it, and parameter "width" is the same thing and probably what you intended to use in the first place, so let's just use that

3/ in p.AddView, if the panel Top is 10dip and the panel Height is height-4dip, then won't that put the panel Bottom 6dip past your intended view height? (admittedly I could easily have confused something up here, perhaps this calculation is correct)
 
Upvote 0

emexes

Expert
Licensed User
No. The correct approach is the create a layout file for the cell layout.
My natural inclination is to defer to Erel, because he's already been down a lot of these dark alleys and has seen traps and troubles that I am yet to discover, but... if you did still want to do it your way for some reason (perhaps you like traps and troubles ;-) then you could try something like:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private CLV1 As CustomListView
  
    Dim Xui As XUI    'I am surprised that this is possible
  
End Sub

Sub CreateLabelButton(Width As Int, Height As Int, BackColor As Int, LabelText As String, ButtonText As String) As B4XView
  
    Dim Pnl1 As B4XView = Xui.CreatePanel("")
    Pnl1.Width = Width
    Pnl1.Height = Height
    Pnl1.Color = BackColor
  
    Dim Lbl1 As Label
    Lbl1.Initialize("")
    Lbl1.Text = LabelText
    Lbl1.TextColor = Colors.Blue
    Lbl1.Color = Colors.Gray
    Lbl1.Gravity = Gravity.CENTER
    Pnl1.AddView(Lbl1, 10dip, 10dip, Width / 2 - 20dip, Height - 20dip)
  
    Dim Btn1 As Button
    Btn1.Initialize("")
    Btn1.Text = ButtonText
    Btn1.Height = Pnl1.Height - 20dip
    Pnl1.AddView(Btn1, Width / 2 + 10dip, 10dip, Width / 2 - 20dip, Height - 20dip)
  
    Return Pnl1
  
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("Layout1")
  
    'CLV1.DefaultTextBackgroundColor=Colors.Gray
  
    Dim LabButWidth As Int = CLV1.AsView.Width
    Dim LabButHeight As Int = LabButWidth / 3    'aspect ratio 3:1
  
    For I = 1 To 100
        Dim LabButBackColor As Int = Colors.RGB(Rnd(1, 4) * 51, Rnd(1, 4) * 51, 0)
      
        Dim LabBut As B4XView = CreateLabelButton(LabButWidth, LabButHeight, LabButBackColor, "Label " & I, "Button " & I)
        CLV1.Add(LabBut, I)
    Next

End Sub

which produced this test CLV:

Screenshot_20190408-232934[1].png
 
Last edited:
Upvote 0
Top