Skip to content

Instantly share code, notes, and snippets.

@alexander-yakushev
Last active October 6, 2015 00:57
Show Gist options
  • Save alexander-yakushev/2907442 to your computer and use it in GitHub Desktop.
Save alexander-yakushev/2907442 to your computer and use it in GitHub Desktop.
A simple demostration of neko.ui toolkit for building Android UI in Clojure
(ns test.leindroid.sample.main
(:use [neko.activity :only [defactivity set-content-view!]]
[neko.threading :only [on-ui]]
[neko.ui :only [make-ui]])
(:import android.widget.EditText
android.net.Uri
android.content.Intent))
(declare ^EditText edit
^android.app.Activity a)
(def button-attributes
{:layout-width 0
:layout-weight 1
:layout-height :fill})
(defn add-symbol [s]
(.setText edit (str (.getText edit) s)))
(defn delete-symbol []
(let [text (str (.getText edit))]
(.setText edit (.substring text 0 (- (count text) 1)))))
(defn call-number []
(let [intent (Intent. Intent/ACTION_DIAL)]
(.setData intent (Uri/parse (str "tel:" (.getText edit))))
(.startActivity a intent)))
(defn number-button [i]
[:button
(assoc button-attributes
:text (str i)
:on-click (fn [_] (add-symbol i)))])
(def stuff (comp vec concat))
(defactivity test.leindroid.sample.MainActivity
:def a
:on-create
(fn [this bundle]
(on-ui
(set-content-view!
a
(make-ui
(stuff
[:linear-layout {:orientation :vertical
:layout-width :fill
:layout-height :fill}
[:edit-text {:def `edit
:layout-width :fill}]]
(map (fn [i]
(stuff
[:linear-layout {:orientation :horizontal
:layout-width :fill
:layout-height 0
:layout-weight 1}]
(map (fn [j]
(let [n (+ (* i 3) j)]
(number-button n)))
(range 1 4))))
(range 3))
[[:linear-layout {:orientation :horizontal
:layout-width :fill
:layout-height 0
:layout-weight 1}
[:button (assoc button-attributes
:text (str \u2190)
:on-click (fn [_] (delete-symbol)))]
(number-button 0)
[:button (assoc button-attributes
:text "Call"
:on-click (fn [_] (call-number)))]]]))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment