How can I use horizontal tabs "\t" to align texts displayed on android phone screen

149 views
Skip to first unread message

Elvis Sogunro

unread,
May 4, 2021, 1:28:04 PM5/4/21
to DroidScript
Hi Everyone,

Does anyone know how I can make texts resulting from some calculations or loop operations to appear aligned on the phone screen.

These texts are created using statements like:

Optext = Optext + indxNo +"\t\t" + MyDte +"\t\t" + fName +"\t\t\t\t" + Age + "\n"

When Optext is written to a text file, everything comes out perfectly aligned, but when displayed via a list on the screen, the texts do not fall on exact tab positions, but follows tab spacings after previous texts, so if a previous text is shorter than the one above it, then the text following will be more indented to the left & vice versa.

Why does it work for written texts and not for the screen?
Any solution?

Thanks

Alan Hendry

unread,
May 7, 2021, 9:05:56 AM5/7/21
to DroidScript
Hi,
If you know HTML and CSS you could try specifying option html and controlling the width of each field in a <span>
If you are a Premium user then see https://symdstools.github.io/Docs/docs/MUI/CreateDataTable.htm 
Regards, ah
Message has been deleted

Jared

unread,
May 17, 2021, 9:36:22 PM5/17/21
to DroidScript
I'd just use spaces instead...

also try the "Log" option for app.CreateText(), may format it better. 

Elvis Sogunro

unread,
May 19, 2021, 3:08:10 AM5/19/21
to DroidScript

Thanks Jared. 
Spaces have the same effect, that's why tab positions works better on text files because it defines the exact starting position of the next item.
I'll check out the "log" option. Not sure I've come across it before.

Thanks 😊

Steve Garman

unread,
May 19, 2021, 8:43:11 AM5/19/21
to DroidScript
I'm not crazy about the effect of tabs for tabulating purposes with or without the log option
The following output shows the top 2 lines with some tabs. 
The bottom two have the tabs replaced by spaces
Unless I am making some inappropriate assumptions about tab-stops the output is not what I would expect to see

function OnStart()
{
   lay = app.CreateLayout("linear", " FillXY")

   var msg="a\tbbb\tccc\t\tddd eee fff"
   
   txt1 = app.AddText(lay, msg,0.9,-1,"left,monospace")
   txt2 = app.AddText(lay, msg,0.9,-1,"log,monospace")

   msg=msg.replace(/\t/g," ")
   
   txt3 = app.AddText(lay, msg,0.9,-1,"left,monospace")
   txt4 = app.AddText(lay, msg,0.9,-1,"log,monospace")


   app.AddLayout(lay)
}
Screenshot_20210519-133242.jpg

Elvis Sogunro

unread,
May 20, 2021, 2:58:54 AM5/20/21
to DroidScript
Thanks Steve.

This example doesn't bring out the fine points, because you're using values with fixed number of characters. 
Let's assume what is to be tabularised is first name and last names of students in a political school. I'll pick 3 Names at random.

What you're aiming to get is 

S/No         First Name               Last Name
  9              Steve                         Houston
10              Donald                      Trump
11              Michelle                    Obama

But what you're getting is given below, with the following observations:
1.  First Names of the 2nd & 3rd lines are pushed forward by one character space 
     because the serial numbers have increased by one digit each.

2. Because Donald is longer than Steve by one character,  the last name Trump eventually gets 
     pushed forward by two characters

3. Because Michelle is longer than Donald, the same sequence of extra padding or push occurs.

Final result

S/No         First Name               Last Name
 9              Steve                         Houston
10               Donald                       Trump
11               Michelle                       Obama

Please try this out with your code.

When you now have a list of hundreds or more names, what comes out I'd some form of wiggling
 some push out, some are drawn in based on the number of characters in their serial number and the first names.

I am yet to try the log & monospace options. Last time I used monospace, the words were looking somehow, 
but I'll plug them in and see the effects.

Many Thanks.

Steve Garman

unread,
May 20, 2021, 6:10:20 AM5/20/21
to DroidScript
/*
this code will emulate how tab stops
worked back in my day on typewriters
and teletypes

I don't know how it would stand up
to large  amounts of data
*/
function OnStart()
{
   // use sample data
   var data =
      "123456789 123456789 123456789\n\n"+
      "S/No\tFirst Name\tLast Name\n" +
      "9\tSteve\tHouston\n" +
      "10\tDonald\tTrump\n" +
      "11\tMichelle\tObama"
   var lay = app.CreateLayout("linear", "VCenter,FillXY")
   var s = expandTabs(data)
   var txt = app.AddText(lay, s, 0.9, -1, "monospace,left,multiline")
   app.AddLayout(lay)
}

function expandTabs(data)
{
   // set tab stops in characters from left
   var tabs = [6, 18, 32, 44, 56]
   var tabsMax = tabs.length - 1
      //split data into array of lines
   var linesIn = data.split("\n")
   var len = linesIn.length
   var linesOut = []
   var line

   //loop through lines
   for(var i = 0; i < len; i++)
   {
      var arr = linesIn[i].split("\t")
      var jlen = arr.length
      var pos = 0
      line = ""

      // loop through columns
      for(var j = 0; j < jlen; j++)
      {
         line += (arr.shift())
         pos = line.length
         var nxt = 0, pad = 0
         // find next tab stop
         while(tabs[nxt] < pos && nxt < tabsMax)
         {
            nxt++
         }
         var pad = tabs[nxt] - pos
         if(pad > 0) line += " ".repeat(pad)
      }
      // add line to output array
      linesOut.push(line)
   }
   // reassemble report and return it
   return(linesOut.join("\n"))
}
////

Elvis Sogunro

unread,
May 21, 2021, 4:19:36 AM5/21/21
to DroidScript

🎯🎯🎯😘

Bullseye! Brilliant, Excellent.

Thanks Steve. This is spot on. I just need to figure out how to integrate it into my application which actually received it's data from an excel file converted to text. The application sieves the incoming data to select those relevant to it.

Many many Thanks 😊

Steve Garman

unread,
May 21, 2021, 4:59:34 AM5/21/21
to DroidScript
Just a word of warning. My code as posted does not play nicely with \t\t with nothing in between

the line 
  line += (arr.shift())
actually adds nothing in that case

So if your data may contine touching tabs you might want to expand that to something like
  var linadd = arr.shift()
  if(linadd=="") linadd=" "
  line += linadd

Another thing worth mentioning is the code as written will work with any line separator in your data that contains \n
so \r\n is ok and so is \n\r but it won't cope with just \r
Reply all
Reply to author
Forward
0 new messages