Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Action bar

satchit8 edited this page Jul 12, 2016 · 11 revisions

Neko provides tools to create Android action bar (UI element that was introduced in Android Honeycomb). Relevant functions can be found in neko.action-bar namespace.

Create simple action bar

The primary function for making an action bar is setup-action-bar. It takes an activity and attribute map to configure the desired look and functionality. You usually want to call this function from activity’s onCreate method. For example:

(defactivity org.test.neko.MainActivity
  :key :main

  (onCreate [this bundle]
    ...
    (on-ui
     (setup-action-bar this
       {:title "Custom title"
        :icon android.R$drawable/btn_star_big_on
        :display-options [:show-home :show-title :home-as-up]
        :subtitle "Custom subtitle"}))))

And here is how it looks like.

Supported attributes

  • :title, :subtitle - strings to set title and subtitle of the action bar;
  • :display-options - parameters to configure how action bar should look like and behave. Can be either a single keyword or a vector of keywords from: :home-as-up, :show-home, :show-custom, :show-title, :use-logo;
  • :icon - either a Drawable or resource ID;
  • :navigation-mode - can be either :standard, :list or :tabs; :tabs - if :tabs were selected as navigation mode, this attributes should be a value of tab definitions.

Action bar tabs

Creation of tabs can also be done with Clojure data structures using :tab widget. It has the following attributes:

  • :text - title of the tab, should be a string;
  • :tab-listener - an ActionBar.TabListenerlistener object that reacts to tab-related actions.

Now, rather than creating listener object manually, you can use tab-listener function that takes key-value pairs of :on-tab-selected, :on-tab-unselected and :on-tab-reselected. Each of this functions take two arguments - the tab and the FragmentTransaction object.

(setup-action-bar this
  {...
   :navigation-mode :tabs
   :tabs [
          [:tab {:text "Alpha"
                 :tab-listener (tab-listener
                                :on-tab-selected (fn [tab ft]
                                                   (toast a "alpha")))}]
          [:tab {:text "Beta"
                 :tab-listener (tab-listener
                                :on-tab-selected (fn [tab ft]
                                                   (toast a "beta")))}]]})

There is even simpler method if the only thing you want from tabs is to change the underlying fragment being shown. You can provide :tab-listener with a fragment and the listener will be automatically created that handles showing/hiding of this fragment. See Fragments for more information on fragment creation.

{...
 :tabs [
        [:tab {:text "Alpha"
               :tab-listener (simple-fragment this
                              [:text-view {:text "Default text"
                                           :text-size [30 :dp]}])}]
        [:tab {:text "Beta"
               :tab-listener (simple-fragment this
                              [:linear-layout {:orientation :vertical}
                               [:button {:text "Default button"}]])}]]}