Android Question Right and left align in one line

PumaCyan

Member
Licensed User
i am using print module to thermal printer using this source : Bluetooth ESC/POS Printer Class
how to make the results partly left aligned and partially right aligned in one line?

print1.jpg


left aligned red box
right aligned green box
 
Solution
This will work if you have a fixed-width font ...
B4X:
Sub Button1_Click
    Dim sample As String
    sample = formatLine("Low cost item", 1)
    Log(sample & " : Line length = " & sample.Length)
    sample = formatLine("Medium cost item", 10)
    Log(sample & " : Line length = " & sample.Length)
    sample = formatLine(" High cost item", 100)
    Log(sample & " : Line length = " & sample.Length)
End Sub

Private Sub formatLine(item As String, price As Float) As String
    Dim cost As String = NumberFormat2(price, 1, 3, 3, False)
    Dim lineLength As Int = item.Length + cost.Length
    Do While (lineLength < 40)                            ' Choose your line length HERE
        item = item & " "
        lineLength = item.Length +...

PumaCyan

Member
Licensed User
We can´t see how you are filling the output now. We can not guess it.
You need to pad the output of the last "column" with spaces on the left based on the maximum chars in that column.
so can I just use the TAB function instead ?
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
No, you cannot just use Tab because 10.00 and 100.00 would be left-aligned. You need to pad shorter amounts with leading spaces.-
 
Upvote 0

Spright

Active Member
If you used HTML instead of plaintext or layouts, there are tricks to do it in CSS.
But looking closely at the hyphens and at the font you can see It's a static width font that sets a maximum width of characters inside the red square, and continues with spaces and hypens before it spaces things just right in the green area. I think that is what happening here.
 
Upvote 0

PumaCyan

Member
Licensed User
If you used HTML instead of plaintext or layouts, there are tricks to do it in CSS.
But looking closely at the hyphens and at the font you can see It's a static width font that sets a maximum width of characters inside the red square, and continues with spaces and hypens before it spaces things just right in the green area. I think that is what happening here.

yes.. you are absolutely right
in the red area I deliberately limit it so it doesn't exceed the width of the paper and pushes the text in the green area
and it's a shame I haven't gotten deep into CSS / html yet, maybe next time
 
Upvote 0

Spright

Active Member
yes.. you are absolutely right
in the red area I deliberately limit it so it doesn't exceed the width of the paper and pushes the text in the green area
and it's a shame I haven't gotten deep into CSS / html yet, maybe next time
In kiosks you print using HTML and CSS quiet often, so it's doable, It all depends on what technology you use of course. But as you use a fixed font you will get the same perfect result.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
From the look of your image, the printer is using a non proportional font. fixed width font.
Alignment is basically padding the string with spaces to the appropriate column or truncating.

It looks like:
Code - 12 chars
spacer - 3 char
Text - 14 chars
spacer - 3 chars
price - 10 chars?
 
Upvote 0

PumaCyan

Member
Licensed User
From the look of your image, the printer is using a non proportional font. fixed width font.
Alignment is basically padding the string with spaces to the appropriate column or truncating.

It looks like:
Code - 12 chars
spacer - 3 char
Text - 14 chars
spacer - 3 chars
price - 10 chars?

sorry late reply...
any ideas or suggestions?
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
This will work if you have a fixed-width font ...
B4X:
Sub Button1_Click
    Dim sample As String
    sample = formatLine("Low cost item", 1)
    Log(sample & " : Line length = " & sample.Length)
    sample = formatLine("Medium cost item", 10)
    Log(sample & " : Line length = " & sample.Length)
    sample = formatLine(" High cost item", 100)
    Log(sample & " : Line length = " & sample.Length)
End Sub

Private Sub formatLine(item As String, price As Float) As String
    Dim cost As String = NumberFormat2(price, 1, 3, 3, False)
    Dim lineLength As Int = item.Length + cost.Length
    Do While (lineLength < 40)                            ' Choose your line length HERE
        item = item & " "
        lineLength = item.Length + cost.Length
    Loop
    Return item & cost
End Sub
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
This will work if you have a fixed-width font ...
B4X:
Sub Button1_Click
    Dim sample As String
    sample = formatLine("Low cost item", 1)
    Log(sample & " : Line length = " & sample.Length)
    sample = formatLine("Medium cost item", 10)
    Log(sample & " : Line length = " & sample.Length)
    sample = formatLine(" High cost item", 100)
    Log(sample & " : Line length = " & sample.Length)
End Sub

Private Sub formatLine(item As String, price As Float) As String
    Dim cost As String = NumberFormat2(price, 1, 3, 3, False)
    Dim lineLength As Int = item.Length + cost.Length
    Do While (lineLength < 40)                            ' Choose your line length HERE
        item = item & " "
        lineLength = item.Length + cost.Length
    Loop
    Return item & cost
End Sub
let me add a format (B4XFormatter)
B4X:
Private Sub Button1_Click
    Dim sample As String
    sample = formatLine("Low cost item", 100)
    Log(sample & " : Line length = " & sample.Length)
    sample = formatLine("Medium cost item", 10000)
    Log(sample & " : Line length = " & sample.Length)
    sample = formatLine("High cost item", 100000)
    Log(sample & " : Line length = " & sample.Length)
    sample = formatLine("High cost item", 1000000)
    Log(sample & " : Line length = " & sample.Length)
End Sub

Private Sub formatLine(item As String, price As Float) As String
    Dim cost As String = SetFormmatter(price, 12, 0)
    Dim lineLength As Int = item.Length + cost.Length
    Do While (lineLength < 40)                            ' Choose your line length HERE
        item = item & " "
        lineLength = item.Length + cost.Length
    Loop
    Return item & cost
End Sub

Public Sub SetFormmatter(Value As Float, Integers As Int, Fractions As Int) As String
    Dim Formatter As B4XFormatter
    Formatter.Initialize
    Formatter.GetDefaultFormat.GroupingCharacter = "."
    Formatter.GetDefaultFormat.DecimalPoint = ","
    Formatter.GetDefaultFormat.MinimumIntegers     = Integers
    Formatter.GetDefaultFormat.MaximumFractions = Fractions
    Formatter.GetDefaultFormat.MinimumFractions = Fractions
    Formatter.GetDefaultFormat.Prefix = "$"
    Formatter.GetDefaultFormat.IntegerPaddingChar = " "
    Return Formatter.Format(Value)
End Sub
1690362053199.png
 
Upvote 0
Solution

PumaCyan

Member
Licensed User
let me add a format (B4XFormatter)
B4X:
Private Sub Button1_Click
    Dim sample As String
    sample = formatLine("Low cost item", 100)
    Log(sample & " : Line length = " & sample.Length)
    sample = formatLine("Medium cost item", 10000)
    Log(sample & " : Line length = " & sample.Length)
    sample = formatLine("High cost item", 100000)
    Log(sample & " : Line length = " & sample.Length)
    sample = formatLine("High cost item", 1000000)
    Log(sample & " : Line length = " & sample.Length)
End Sub

Private Sub formatLine(item As String, price As Float) As String
    Dim cost As String = SetFormmatter(price, 12, 0)
    Dim lineLength As Int = item.Length + cost.Length
    Do While (lineLength < 40)                            ' Choose your line length HERE
        item = item & " "
        lineLength = item.Length + cost.Length
    Loop
    Return item & cost
End Sub

Public Sub SetFormmatter(Value As Float, Integers As Int, Fractions As Int) As String
    Dim Formatter As B4XFormatter
    Formatter.Initialize
    Formatter.GetDefaultFormat.GroupingCharacter = "."
    Formatter.GetDefaultFormat.DecimalPoint = ","
    Formatter.GetDefaultFormat.MinimumIntegers     = Integers
    Formatter.GetDefaultFormat.MaximumFractions = Fractions
    Formatter.GetDefaultFormat.MinimumFractions = Fractions
    Formatter.GetDefaultFormat.Prefix = "$"
    Formatter.GetDefaultFormat.IntegerPaddingChar = " "
    Return Formatter.Format(Value)
End Sub
View attachment 144050

thanks a lot for the help
it works as i expected
 
Upvote 0
Top