Home   Archive   Permalink



R3 Android Tutorial

I added a primer about using Saphirion's R3 GUI on Android, to the tutorial at:
    
http://business-programming.com/business_programming.html#section-18
    
Basic syntax differences between R2 VID and Saphirion R3-GUI are covered. Several of the little demo apps in the tutorial are converted to R3 (calculator, tile game, minimal cash register, parts database, etc.), and there's a bit about setting up the Android working environment with tools like Jota (code editor), file explorer, etc.
    
It's not in depth, but enough to help make the jump to R3 GUI and Android easy for anyone who's worked with R2.

posted by:   Nick       25-Aug-2013/10:08:38-7:00



For those who are not familiar with Rebol version 3 (R3), here are some demo scipts, with GUIs. These programs run on Android and desktop OSs, using the exact same code. These little scripts are easy to execute and modify for your own use. They should give some insight into how immediately useful and productive R3 is - but this just scratches the surface. Rebol is a deep and powerful tool. Networking, file management, list processing, text parsing, and other powerful functions are built in and easy to use. There is nothing else like it to productively create cross platform apps between desktop and mobile.
    
The Saphirion builds of R3 are at http://development.saphirion.com/experimental/builds/. The GUI library is at http://development.saphirion.com/resources/r3-gui.r3 (download and put this file in the same folder as any scripts you write). Just download and install/associate the tiny R3 interpreter with .r3 scripts (plain text files saved with an ".r3" extension in the file name) and click any saved .r3 scripts with a file manager. On Android, my preferred file manager is ES File explorer, and my favorite text editor for writing scripts is Jota. Give it a try - it only takes a minute to start writing Android apps that can also be run, exactly as-is, on your desktop PC:
    
    
R E B O L []
load-gui
view [text "Hello World!"]
    
    
R E B O L [title: "Tiny Note Editor"]
do %r3-gui.r3 ; download this file manually or just use load-gui as above
view [
     a1: area
     button "Save" on-action [write %notes.txt get-face a1]
     button "Load" on-action [set-face a1 to-string read %notes.txt]
]
    
    
R E B O L [title: "Data Entry to CSV File"]
do %r3-gui.r3
view [
     text "First Name:"
     f1: field
     text "Last Name:"
     f2: field
     button "Submit" on-action [
         write/append %cntcts.txt rejoin [
             mold get-face f1 " " mold get-face f2 newline
         ]
         request "" "Saved"
     ]
     a1: area
     button "Load" on-action [set-face a1 to-string read %cntcts.txt]
]
    
    
R E B O L [title: "Text File Reader (how to use a text list file selector)"]
do %r3-gui.r3
view [
     a1: area
     button "Load" on-action [
         files: read %./
         view/modal [
             text "File Name:"
             t2: text-list files on-action [
                 set-face a1 to-string read(to-file pick files get-face t2)
                 unview
             ]
         ]
     ]
]
    
    
R E B O L [title: "List-View (Grid) Example"]
do %r3-gui.r3
view [
     text-table ["1" 200 "2" 100 "3"][
         ["asdf" "a" "4"]
         ["sdfg" "b" "3"]
         ["dfgh" "c" "2"]
         ["fghj" "d" "1"]
     ]
]
    
    
R E B O L [title: "Calculator"]
do %r3-gui.r3
stylize [
     btn: button [
         facets: [init-size: 50x50]
         actors: [on-action:[set-face f join get-face f get-face face]]
     ]
]
view [
     hgroup [
         f: field return
         btn "1" btn "2" btn "3" btn " + " return
         btn "4" btn "5" btn "6" btn " - " return
         btn "7" btn "8" btn "9" btn " * " return
         btn "0" btn "." btn " / " btn "=" on-action [
             attempt [set-face f form do get-face f]
         ]
     ]
]
    
    
R E B O L [title: "Sliding Tile Puzzle"]
do %r3-gui.r3
stylize [
     p: button [
         facets: [init-size: 60x60 max-size: 60x60]
         actors: [
             on-action: [
                 t: face/gob/offset
                 face/gob/offset: x/gob/offset
                 x/gob/offset: t
             ]
         ]
     ]
]
view/options [
     hgroup [
         p "8" p "7" p "6" return
         p "5" p "4" p "3" return
         p "2" p "1" x: box 60x60 white
     ]
] [bg-color: white]
    
    
R E B O L [title: "Math Test"]
do %r3-gui.r3
random/seed now
x: does [rejoin [random 10 " + " random 20]]
view [
     f1: field (x)
     text "Answer:"
     f2: field on-action [
         either (get-face f2) = (form do get-face f1) [
             request "Yes!" "Yes!"][request "No!" "No!"
         ]
         set-face f1 x
         set-face f2 ""
         focus f2
     ]
]
    
    
R E B O L [title: "Minimal Cash Register"]
do %r3-gui.r3
stylize [fld: field [init-size: 80]]    
view [
     hgroup [
         text "Cashier:" cashier: fld
         text "Item:"     item: fld
         text "Price:"     price: fld on-action [
             if error? try [to-money get-face price] [
                 request "Error" "Price error"
                 return none
             ]
             set-face a rejoin [
                 get-face a mold get-face item tab get-face price newline
             ]
             set-face item copy "" set-face price copy ""
             sum: 0
             foreach [item price] load get-face a [
                 sum: sum + to-money price
             ]
             set-face subtotal form sum
             set-face tax form sum * .06
             set-face total form sum * 1.06
             focus item
         ]
         return
         a: area 600x300
         return
         text "Subtotal:" subtotal: fld
         text "Tax:"        tax: fld
         text "Total:"     total: fld
         button "Save" on-action [
             items: replace/all (mold load get-face a) newline " "
             write/append %sales.txt rejoin [
                 items newline get-face cashier newline now/date newline
             ]
             set-face item copy "" set-face price copy ""
             set-face a copy ""    set-face subtotal copy ""
             set-face tax copy "" set-face total copy ""
         ]
     ]
]
    
    
R E B O L [title: "Requestors"]
do %r3-gui.r3
x: request/ask "Question" "Do you like this?."
either x = false [print "No!"] [print "Yes!"]
x: request/custom "" "Do you like this?" ["Yay" "Boo"]
either x = false [print "Boo!"] [print "Yay!"]


posted by:   Nick       2-Sep-2013/11:24:40-7:00



Here's a little Contacts program that demonstrates how to update a text-table grid with text typed into field widgets, how to save and load to/from files, etc. It also demonstrates a simple request-file function, used during the save and load process.
    
The R3-GUI text-table has a number of useful features built in. Click the headers to sort by column. Use CTRL-H to delete any selected row. Right click any cell to manually edit. Use shift-arrow keys to move a selected row up or down in the table display. Very useful :)

posted by:   Nick       4-Sep-2013/16:03:50-7:00



R E B O L [title: "Contacts"]
do %r3-gui.r3
request-file: does [
     append files: copy ["../" "(New...)"] read %./
     view/modal [
         text "File Name:"
         t2: text-list files on-action [
             set 'file to-file pick files get-face t2
             unview
         ]
     ]
     if file = "(New...)" [
         view/modal [
             text "New File Name:"
             f: field on-action [set 'file to-file get-face f unview]
         ]
     ]
     if file = "../" [cd %../ request-file]
     if dir? file [cd :file request-file]
     file
]
add-contact: does [
     d: get-face/field t 'data
     append/only d copy/deep reduce [
         get-face f1 get-face f2 get-face f3
     ]
     set-face/field/no-show t d 'data
     set-face t/names/scr 100%
]
view [
     t: text-table ["Name" 200 "Phone" 100 "Address" 300] []
     hgroup [text 200 "Name:" text 100 "Phone:" text 300 "Address:"]
     hgroup [
         f1: field 200 f2: field 100 f3: field 300 on-action [add-contact]
     ]
     hgroup [
         button "Add New Contact" on-action [add-contact]
         button "Save" on-action [
             save-file: request-file
             if save-file <> none [
                 save save-file get-face/field t 'data
                 alert "Saved"
             ]
         ]
         button "Load" on-action [
             attempt [set-face/field/no-show t load request-file 'data]
             set-face t/names/scr 100%
         ]
     ]
]


posted by:   Nick       4-Sep-2013/16:04:32-7:00



I hate to say it, but I am trying your procedure for running r3 on android and it is not working. I have the basic three-line "hello android" program where the second line is "do %r3-gui.r3" and the result I get is "script error: %r3-gui.r3 has no value." I do have the r3-gui.r3 in the same folder as the script. I have stared at the code and the contents of the folder that holds my scripts and I am just not seeing anything I have done wrong with this three-line program.    
    
That "jota" editor you recommended is nice, by the way.

posted by:   Steven White       5-Sep-2013/17:18:33-7:00



Hi Steven,
    
Which version of the Saphirion R3 interpreter do you have installed? If the code is copied exactly (with the percent sign preceding the file name), and your version is up to date, that error should not occur. Be sure to remove the spaces in the the "R E B O L" header, as it appears in the examples above.
    
Try making a "rebol" sub-folder in your "/sdcard/" directory and move your scripts there. Execute this line in the interpreter when you get the error, please let me know if it sheds any light on the error:
    
>> ? system/version list-dir
    
I get a SYSTEM/VERSION value of 2.101.0.13.1 on my Android device.

posted by:   Nick       6-Sep-2013/8:43:39-7:00



Caught again!
    
I had a similar problem in the past where I was using a "dash" instead of a "hyphen" because those two characters looked so much alike on my tablet soft keyboard. So I was aware of that problem, but it turned out that the symbol I was thought was a "percent" symbol on my keyboard actually was a "care-of" symbol, sort of like "c/0" but in little letters so that it looked a lot like a percent sign. I scanned the keyboard AGAIN and by George if I didn't find the actual percent sign elsewhere. So I used it, and it worked. It might be time to visit the drug store and upgrade my reading glasses.
    
Thank you.

posted by:   Steven White       6-Sep-2013/12:14:12-7:00