The idea behind tweaks is that widget styles and behaviors should be as composable as widget layouts themselves.

What are they

A tweak is something that changes a property of a widget. More precisely, it is defined by a function WidgetClass ⇒ Unit.


You can use the Tweak companion object to define tweaks:

import android.view.View
import android.widget.TextView
import macroid._
import macroid.FullDsl._

val bringToFront = Tweak[View] { view ⇒

def text(str: String) = Tweak[TextView](_.setText(str))


Tweaks can be composed simply by using the + operator. For example:

def textAndBringToFront(str: String) =
  text(str) + bringToFront

The execution order is the same as the composition order, so in this case first the caption will be set and then bringToFront will be called.


Tweaking is one of Macroid’s most powerful operations (here’s why). We’ve already seen this example:

textView <~ text("Hi")

In fact, we can chain a few tweaks in one operation:

textView <~ text("Hi") <~ show

As you may already know, tweaking returns a UI action:

val textView2: Ui[TextView] =
  textView <~ text("Hi") <~ show

Note that tweaking does not require the widgets to be created with Macroid bricks.

Standard tweaks

A number of standard tweaks are included. You can always find them in the scaladoc. Note that all these tweaks can be imported with import macroid.FullDsl._ or just import macroid.Tweaks._.

A quick rundown:

  • Basic tweaks
    • id — set the id
    • wire — wire to a slot (see Searching)
  • Visibility
    • hide
    • show
  • Enabling/disabling
    • enable
    • disable
  • Padding
    • padding — supports named arguments. Example:

      w[TextView] <~ padding(top = 8 dp, bottom = 8 dp)
  • Layout
    • layoutParams / lp — works with any layout. Example:

      w[Button] <~ lp[LinearLayout](MATCH_PARENT, WRAP_CONTENT, 1.0f)
    • vertical
    • horizontal
    • addViews — add views to a ViewGroup. Example:

      val views = List("foo", "bar").map(s ⇒ w[TextView] <~ text(s))
      linearLayout <~ addViews(views, removeOld = true)
  • Text/captions
    • text
    • hint
  • Handling events
    • On — works with (almost) any events. Expects a UI action that takes no arguments and returns the same type as the associated listener. Examples:

      w[Button] <~ <~ hide)
      w[Button] <~ On.longClick((textView <~ show) ~ Ui(true))
      ) <~ On.refresh[SwipeRefreshLayout](Ui(...))
    • FuncOn — the same as On, but takes arguments, as specified by the listener. Example:

      w[StaggeredGridView] <~
        FuncOn.itemClick[StaggeredGridView] { (_: AdapterView[_], _: View, index: Int, _: Long) ⇒

Extra tweaks

Some extra tweaks are included in the macroid.contrib package. You can always find them in the scaladoc.

Please feel free to file a pull request with any extra tweaks you find useful. The naming strategy can be observed below.

A quick rundown:

  • LpTweaks
    • matchParent — same as lp[ViewGroup](MATCH_PARENT, MATCH_PARENT)
    • wrapContent — same as lp[ViewGroup](WRAP_CONTENT, WRAP_CONTENT)
    • matchWidth — same as lp[ViewGroup](MATCH_PARENT, WRAP_CONTENT)
    • matchHeight — same as lp[ViewGroup](WRAP_CONTENT, MATCH_PARENT)
  • BgTweaks
    • res — sets a background from resources
    • color — sets background to a color
  • TextTweaks (for TextView)
    • color
    • bold, italic, boldItalic
    • serif, sans, mono, typeface
    • medium, large, size
  • ImageTweaks (for ImageView)
    • res — shows a drawable from resources
    • bitmap
    • adjustBounds — sets adjustViewBounds to true
  • ListTweaks (for ListView)
    • noDivider — hides the divider
    • adapter — sets list adapter (works with any AbsListView)
  • PagerTweaks (for ViewPager)
    • page — sets current page
    • adapter — sets pager adapter
  • SeekTweaks (for SeekBar)
    • seek — sets the cue