clojure-android / neko

The Clojure/Android Toolkit
Other
297 stars 36 forks source link

Request for guidance for custom element #50

Open p14n opened 9 years ago

p14n commented 9 years ago

Hi there,

I'm looking to create a custom drawing as part of a component. In the Android API it seems I'd subclass view and implement onDraw to draw directly on the canvas. I'm not sure in neko if it's possible to do this without writing my custom component in Java - it seems custom elements need a classname.

Any pointer much appreciated, Cheers, Dean

p14n commented 9 years ago

I think I found the answer - a :custom-constructor for a View.

alexander-yakushev commented 9 years ago

Indeed, I used :custom-constructor attribute to create a proxy where I override onDraw. So did it work for you?

p14n commented 9 years ago

Yes it did - thanks

(defn draw-circle [^Canvas canvas]
  (let [paint (Paint. Paint/ANTI_ALIAS_FLAG)]
    (.drawCircle canvas 0 0 20 paint)))

(defn main-layout [activity] [:linear-layout {:orientation :horizontal}
                      [:edit-text {:hint "Event name"}]
                      (make-ui activity [:view {:custom-constructor
                       (fn [ctx]
                         (proxy [android.view.View] [ctx]
                           (onDraw [^Canvas canvas]
                             (draw-circle canvas)
                             )))}])])
alexander-yakushev commented 9 years ago

OK, so I see you called make-ui yourself. It's fine to do that, but you won't be able to find that custom-view by ID if you later add :id attribute to it. So in fact you could just do:

(defn main-layout [activity]
  [:linear-layout {:orientation :horizontal}
   [:edit-text {:hint "Event name"}]
   [:view {:custom-constructor
           (fn [ctx]
             (proxy [android.view.View] [ctx]
               (onDraw [^Canvas canvas]
                 (draw-circle canvas)
                 )))}]])