Android Question Changing the size of Listview text?

Danamo

Member
Licensed User
I'm using
List1.SingleLineLayout.ItemHeight and .Label.TextSize
to change the size of the text in a Listview. This works okay the first time the Listview is populated (in Activity_Create) but I want my App to have the option to change the font size.

When I use the SingleLineLayout in a sub to change the font size, it only changes some of the lines in the Listview, instead of every line. I've tried clearing and repopulating the list but it still does the same thing.

What am I doing wrong? Thanks for any advice!
 

Knoppi

Active Member
Licensed User
Longtime User
Do you have a mix of AddSingleLine and AddTwoLines ?
With SingleLineLayout you change only the SingleLines.

here a small example:
B4X:
Sub ConfigListview( lv As ListView)
    lv.SingleLineLayout.ItemHeight = 40dip
    lv.SingleLineLayout.Label.TextSize = 18
    lv.SingleLineLayout.Label.TextColor = Colors.Black
    lv.SingleLineLayout.Label.Gravity = Gravity.BOTTOM
    lv.SingleLineLayout.Label.Typeface = Typeface.CreateNew( Typeface.DEFAULT, Typeface.STYLE_BOLD_ITALIC)
       
    lv.TwoLinesAndBitmap.ItemHeight = 60dip
    lv.TwoLinesAndBitmap.Label.TextSize = 20
    lv.TwoLinesAndBitmap.Label.TextColor = Colors.Black
    lv.TwoLinesAndBitmap.SecondLabel.TextSize = 14
    lv.TwoLinesAndBitmap.SecondLabel.TextColor = Colors.DarkGray
    lv.TwoLinesAndBitmap.SecondLabel.Gravity = Gravity.TOP
    lv.TwoLinesAndBitmap.SecondLabel.Height = 30dip
End Sub
 
Last edited:
Upvote 0

Danamo

Member
Licensed User
No, Knoppi, I'm not mixing AddSingle and AddTwo. My app only has SingleLines.

Here is the code from my app, that I am using to change the font size. (List1 is a ListView)

List1.SingleLineLayout.ItemHeight = 20dip
List1.SingleLineLayout.Label.TextSize = 14
List1.SingleLineLayout.Label.Typeface = Typeface.CreateNew( Typeface.DEFAULT, Typeface.STYLE_BOLD_ITALIC)


I added the "Label.Typeface = " part from your example since I didn't have that in my code, but it didn't make a difference. About one out of every 14 lines in the ListView gets changed to the new size, but all the other lines remain the same. Am I missing something?


Do you have a mix of AddSingleLine and AddTwoLines ?
With SingleLineLayout you change only the SingleLines.

here a small example:
B4X:
Sub ConfigListview( lv As ListView)
    lv.SingleLineLayout.ItemHeight = 40dip
    lv.SingleLineLayout.Label.TextSize = 18
    lv.SingleLineLayout.Label.TextColor = Colors.Black
    lv.SingleLineLayout.Label.Gravity = Gravity.BOTTOM
    lv.SingleLineLayout.Label.Typeface = Typeface.CreateNew( Typeface.DEFAULT, Typeface.STYLE_BOLD_ITALIC)
     
    lv.TwoLinesAndBitmap.ItemHeight = 60dip
    lv.TwoLinesAndBitmap.Label.TextSize = 20
    lv.TwoLinesAndBitmap.Label.TextColor = Colors.Black
    lv.TwoLinesAndBitmap.SecondLabel.TextSize = 14
    lv.TwoLinesAndBitmap.SecondLabel.TextColor = Colors.DarkGray
    lv.TwoLinesAndBitmap.SecondLabel.Gravity = Gravity.TOP
    lv.TwoLinesAndBitmap.SecondLabel.Height = 30dip
End Sub
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
Sorry, I can not check this at the moment because my pc with b4a does not work.

With Typeface you can change the style of the font ( BOLD, ITALIC ).
The Code-sniplett is only a example what is possible.
 
Upvote 0

Danamo

Member
Licensed User
You must change the styles before adding any item.

If the number of items is not too large then it will be simpler to use CustomListView.

I am setting the styles first, using .SingleLineLayout...
Here's my code:

B4X:
'ListView1 as ListView is defined and initialized by the Designer in Process_Globals
'dataList As List is defined in Process_Globals, and initialized and populated in Activity_Create
'All works fine and the ListView shows the item lines in the LV's original default font style and size, etc.

'In a Sub, I'm doing this to change the font of the LV...

Dim i As integer
ListView1.Clear
ListView1.SingleLineLayout.Label.Typeface = Typeface.CreateNew( Typeface.DEFAULT,  _ Typeface.STYLE_BOLD_ITALIC)
ListView1.SingleLineLayout.ItemHeight = 25dip
ListView1.SingleLineLayout.Label.TextSize = 20
For i= To dataList.Size - 1
    ListView1.AddSingleLine(dataList.Get(i))
Next

The result I am getting is that most of the lines in the ListView remain unchanged, except that every 14th or so line is changed to the new typeface, height, and text size.

No doubt as a beginner I doing or missing something really stupid or obvious here? I appreciate your expert help!!!
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
First thing, you don't have to declare the variable for that FOR...NEXT loop
B4X:
Dim i As integer
Second, why are you repeating that code?, you can define the typeface, font size etc just once (you could do it right after loading your layout), the Sub where you have the code should only have this:
B4X:
ListView1.Clear
For i= To dataList.Size - 1
    ListView1.AddSingleLine(dataList.Get(i))
Next
 
Upvote 0

Danamo

Member
Licensed User
First thing, you don't have to declare the variable for that FOR...NEXT loop
B4X:
Dim i As integer
Second, why are you repeating that code?, you can define the typeface, font size etc just once (you could do it right after loading your layout), the Sub where you have the code should only have this:

Okay, thanks. I didn't realize that the For command declared the counter variable so I didn't have to do that first. Duh to me!

As for repeating the code, that's not my intent. I want to be able to CHANGE the font size later on if desired. (Like if the app user desires a larger or smaller font than the default originally specified. My list has a lot of lines so I start out with a small font size to fit more into it on the screen - and less scrolling - but I want the option to show the data in a larger font size too.)

Are you saying that once a ListView is created and populated then it's font parameters cannot be changed at all after that? So if I wanted to see the same list data items in a larger font or different style then I'd have to create another ListView and populate it with the data, and hide the original ListView or something?

Maybe I need to use a Custom ListView instead? But my list has thousands of items and I've read elsewhere in this forum that Custom ListViews are much slower when handling large numbers of items?
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
I'm sorry, but why not declaring the variable i in that FOR...NEXT, if he wants to ?

B4X:
Private i as integer
ListView1.Clear
For i=0 To dataList.Size - 1
    ListView1.AddSingleLine(dataList.Get(i))
Next

Or you can iterate the List, using For Each

B4X:
    Dim dataList As List
    For Each data as String In dataList
        ListView1.AddSingleLine(data)
    Next
 
Last edited:
Upvote 0

NJDude

Expert
Licensed User
Longtime User
@Danamo

If you have thousands of items then keep using the ListView, but, in order to avoid what you reported on your first post you will have to re-load the listview, the issue you experienced is due to the way listviews work, look at the attached code sample, I'm creating a listview and loading 1000 items, clicking on the button changes the font, height etc and ALL the lines look fine, pay attention to what I did.

I hope that helps.
 

Attachments

  • ListViewSample.zip
    20.2 KB · Views: 515
Upvote 0

Danamo

Member
Licensed User
@Danamo

If you have thousands of items then keep using the ListView, but, in order to avoid what you reported on your first post you will have to re-load the listview, the issue you experienced is due to the way listviews work, look at the attached code sample, I'm creating a listview and loading 1000 items, clicking on the button changes the font, height etc and ALL the lines look fine, pay attention to what I did.

I hope that helps.

Yes, that's very helpful!!!

I see, in your code you are removing all the activity's views, reloading the layout, defining the new listview font parameters, and then repopulating the listview items.

Thank you very much for taking the time to create the sample code and providing me with a solution to my question.
 
Upvote 0
Top