{"id":3445,"url":"https://github.com/anvil-ui/anvil","last_synced_at":"2025-05-16T05:05:02.187Z","repository":{"id":25853523,"uuid":"29293245","full_name":"anvil-ui/anvil","owner":"anvil-ui","description":"Minimal UI library for Android inspired by React","archived":false,"fork":false,"pushed_at":"2021-05-29T00:26:13.000Z","size":1159,"stargazers_count":1446,"open_issues_count":40,"forks_count":90,"subscribers_count":56,"default_branch":"master","last_synced_at":"2024-10-29T19:59:53.624Z","etag":null,"topics":["android","anvil","java","kotlin","react","xml-layout"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anvil-ui.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-15T10:55:34.000Z","updated_at":"2024-09-24T17:56:17.000Z","dependencies_parsed_at":"2022-08-24T14:15:25.161Z","dependency_job_id":null,"html_url":"https://github.com/anvil-ui/anvil","commit_stats":null,"previous_names":["zserge/anvil"],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anvil-ui%2Fanvil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anvil-ui%2Fanvil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anvil-ui%2Fanvil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anvil-ui%2Fanvil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anvil-ui","download_url":"https://codeload.github.com/anvil-ui/anvil/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254471061,"owners_count":22076585,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","anvil","java","kotlin","react","xml-layout"],"created_at":"2024-01-05T20:16:41.746Z","updated_at":"2025-05-16T05:05:02.171Z","avatar_url":"https://github.com/anvil-ui.png","language":"Java","readme":"\n# Anvil - reactive views for Android\n\n[![Join the chat at https://gitter.im/zserge/anvil](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/zserge/anvil?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge) [![Build Status](https://travis-ci.org/zserge/anvil.svg?branch=master)](https://travis-ci.org/zserge/anvil)\n[![Android Weekly](https://img.shields.io/badge/Android%20Weekly-%23193-brightgreen.svg)](http://androidweekly.net/issues/issue-193)\n\n\u003cbr/\u003e\n\n\u003cdiv\u003e\n\u003cimg align=\"left\" src=\"https://raw.githubusercontent.com/zserge/anvil/master/logo/ic_launcher.png\" alt=\"logo\" width=\"96px\" height=\"96px\" /\u003e\n\u003cp\u003e\nAnvil is a small Java library for creating reactive user interfaces. Originally inspired by \u003ca href=\"https://facebook.github.io/react/\"\u003eReact\u003c/a\u003e, it suits well as a view layer for \u003ca href=\"https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel\"\u003eMVVM\u003c/a\u003e or \u003ca href=\"http://redux.js.org/\"\u003eRedux\u003c/a\u003e design patterns.\n\u003c/p\u003e\n\u003c/div\u003e\n\n\u003cbr/\u003e\n\n## Features\n\n* Super small (4 hand-written classes + 1 generated class)\n* Easy to learn (top-level API is only 5 functions)\n* Fast (uses no reflection¹)\n* Efficient (views are updated lazily, if the data change didn't affect the view - it remains untouched)\n* Easy to read declarative syntax\n* Java 8 and Kotlin-friendly, but supports Java 6 as well\n* XML layouts are supported, too\n\n¹Reflection is still used to inflate views once (standard XML inflater does the\nsame thing, so no performance loss here).\n\n## Installation\n\n``` gradle\n// build.gradle\nrepositories {\n    jcenter()\n}\ndependencies {\n    compile 'co.trikita:anvil-sdk15:0.5.0'\n}\n```\nAnvil comes in multiple builds for different minimal SDK versions:\n\n* anvil-sdk15 (ICS, 99.7% of devices)\n* anvil-sdk19 (Kitkat, 94.3% of devices)\n* anvil-sdk21 (Lollipop, 82.3% of devices)\n\nAPI levels 16, 17, 18, 22 or 23 are not added because they had very few\nUI-related methods added.\n\n## Examples\n\nNormally you would write your layouts in XMLs, then get your views by their\nIDs and set their listeners, finally you would observe your data and modify\nview properties as your data changes.\n\nAnvil simplifies most of this boring routine.\n\nFirst, add a static import that makes it much easier to write your view:\n\n``` java\nimport static trikita.anvil.DSL.*;\n```\n\nNext, declare your layout, assign event listeners and bind data:\n\n``` Java\npublic int ticktock = 0;\npublic void onCreate(Bundle b) {\n    super.onCreate(b);\n    setContentView(new RenderableView(this) {\n        @Override\n        public void view() {\n            linearLayout(() -\u003e {\n                size(MATCH, MATCH);\n                padding(dip(8));\n                orientation(LinearLayout.VERTICAL);\n\n                textView(() -\u003e {\n                    size(MATCH, WRAP);\n                    text(\"Tick-tock: \" + ticktock);\n                });\n\n                button(() -\u003e {\n                    size(MATCH, WRAP);\n                    text(\"Close\");\n                    // Finish current activity when the button is clicked\n                    onClick(v -\u003e finish());\n                });\n            });\n        }\n    });\n}\n```\n\nHere we've created a very simple reactive view and added it to our Activity.\nWe've declared our layout (a LinearLayout with a TextView inside). We've defined\nstyles (width, height, orientation and padding). We've set OnClickListener to \nthe button. We've also bound a variable \"ticktock\" to the text property inside a TextView.\n\nNext, let's update your views as your data changes:\n\n``` java\nticktock++;\nAnvil.render();\n```\n\nAt this point your layout will be updated and the TextView will contain text\n\"Tick-Tock: 1\" instead of \"Tick-Tock: 0\". However the only actual view method\nbeing called would be setText().\n\nYou should know that there is no need to call Anvil.render() inside your event\nlisteners, it's already triggered after each UI listener call:\n\n``` java\npublic void view() {\n    linearLayout(() -\u003e {\n        textView(() -\u003e {\n            text(\"Clicks: \" + numClicks);\n        });\n        button(() -\u003e {\n            text(\"Click me\");\n            onClick(v -\u003e {\n                numClicks++; // text view will be updated automatically\n            });\n        });\n    });\n}\n```\n\n## Made with Anvil\n\n[![Talalarmo](https://raw.githubusercontent.com/trikita/talalarmo/master/src/main/res/drawable-xhdpi/ic_launcher.png)](https://github.com/trikita/talalarmo)\n[![Slide](https://raw.githubusercontent.com/trikita/slide/master/src/main/res/mipmap-xxhdpi/ic_launcher.png)](https://github.com/trikita/slide)\n[![Spot](https://i.imgur.com/Al3sh3Q.png)](https://play.google.com/store/apps/details?id=trikita.spot)\n[![Quilt](https://i.imgur.com/rqI02l0.png)](https://play.google.com/store/apps/details?id=trikita.quilt)\n\nYou may find more Anvil examples for Java 6, Java 8 and Kotlin at\n\n- [Anvil simple demos in Kotlin](https://github.com/zserge/anvil-kotlin-demos)\n- [Anvil more advanced examples in Java or Kotlin](https://github.com/zserge/anvil-examples)\n\n## How it works\n\nNo magic. When a renderable object is being constructed there are 3 types of\noperations: push view, modify some attribute of the current view, and \npop view. If you're familiar with\n[incremental DOM](http://google.github.io/incremental-dom/#about) - Anvil\nfollows the same approach.\n\nPushing a view adds it as a child to the parent view from the top of\nthe stack. Attribute modification simply sets the given property to\nthe current view on top of the stack. Pop unwinds the stack.\n\nWhen you mount this layout (assuming the `name` is \"John\"):\n\n``` java\nlinearLayout(() -\u003e {\n    textView(() -\u003e {\n        text(\"Hello \" + name);\n    });\n});\n```\n\nIt does the following sequence of actions:\n\n```\nPush LinearLayout (adding it to the root view)\nPush TextView (adding it to the linear layout)\nAttr text \"Hello John\" (calling setText of the TextView)\nPop\nPop\n```\n\nThe only trick is that these actions are cached into a so called \"virtual layout\" -\na tree-like structure matching the actual layout of views and their properties.\n\nSo when you call `Anvil.render()` next time it compares the sequence of\nactions with the cache and skips property values if they haven't change. Which means on the next\n`Anvil.render()` call _the views will remain untouched_. This caching technique\nmakes render a very quick operation (having a layout of 100 views, 10\nattributes each you can do about 4000 render cycles per second!).\n\nNow, if you modify the `name` from \"John\" to \"Jane\" and call Anvil.render() it will do the following:\n\n```\nPush LinearLayout (noop)\nPush TextView (noop)\nAttr text \"Hello Jane\" (comparing with \"Hello John\" from the pervious render,\n  noticing the difference and calling setText(\"Hello Jane\") to the TextView)\nPop\nPop\n```\n\nSo if you modify one of the variables \"bound\" to some of the attributes - the\ncache will be missed and attribute will be updated.\n\nFor all event listeners a \"proxy\" is generated, which delegates its\nmethod calls to your actual event listener, but calls `Anvil.render()` after each\nmethod. This is useful, because most of your data models are modified when\nthe user interacts with the UI, so you write less code without calling\n`Anvil.render()` from every listener. Remember, no-op renders are very fast.\n\n## Supported languages and API levels\n\nAnvil is written in Java 7, but its API is designed to use lambdas as well, so\nin modern times it's recommended to use Anvil with Java8/Retrolambda or Kotlin.\n\nSyntax is a bit different for each language, but it's very intuitive anyway.\n\nJava 6 without lambdas:\n\n``` java\npublic void view() {\n    o (linearLayout(),\n      orientation(LinearLayout.VERTICAL),\n\n        o (textView(),\n          text(\"...\")),\n        \n        o (button(),\n          text(\"...\"),\n          onClick(myListener)));\n}\n```\n\nJava 8 + RetroLambda:\n\n``` java\npublic void view() {\n    linearLayout(() -\u003e {\n        orientation(LinearLayout.VERTICAL);\n        textView(() -\u003e {\n            text(\"....\");\n        });\n        button(() -\u003e {\n            text(\".....\");\n            onClick(v -\u003e {\n                ....\n            });\n        });\n    });\n}\n```\n\nKotlin:\n\n``` kotlin\npublic override fun view() {\n    linearLayout {\n        orientation(LinearLayout.VERTICAL)\n        textView {\n            text(\"....\")\n        }\n        button {\n            text(\"....\")\n            onClick { v -\u003e\n                ...\n            }\n        }\n    }\n}\n```\n\nAnvil library contains only a few classes to work with the virtual layout, but most\nof the DSL (domain-specific language describing how to create views/layouts and set\ntheir attributes) is generated from `android.jar`.\n\n## API\n\nHere's a list of classes and methods you need to know to work with Anvil like a pro:\n\n* `Anvil.Renderable` - functional interface that one should implement to\n    describe layout structure, style and data bindings.\n\n* `Anvil.mount(View, Anvil.Renderable)` - mounts renderable layout into a\n    View or a ViewGroup\n\n* `Anvil.unmount(View)` - unmounts renderable layout from the View\n    removing all its child views if it's a ViewGroup\n\n* `Anvil.render()` - starts a new render cycle updating all mounted views\n\n* `Anvil.currentView(): View` - returns the view which is currently being\n    rendered. Useful in some very rare cases and only inside the Renderable's\n    method `view()` to get access to the real view and modifying it manually.\n\n* `RenderableView` - a most typical implementation of Anvil.Renderable.\n    Extending this class and overriding its method `view()` allows to create\n    self-contained reactive components that are mounted and unmounted\n    automatically.\n\n* `RenderableAdapter` - extending this class and overriding its `getCount()`,\n    `getItem(int)` and `view(int)` allows to create lists where each item is a\n    standalone reactive renderable object.\n\n* `RenderableAdapter.withItems(list, cb(index, item))` - a shortcut to create\n    simple adapters for the given list of items. `cb` is a lambda that describes\n    the layout and bindings of a certain list item at the given index.\n\n## DSL\n\nThe bottom part of the iceberg is Anvil DSL.\n\nDSL consists of a few handwritten property setters, but most of it is\ngenerated from java classes in the android SDK.\n\nSee a full list of the DSL methods for each API level [here](https://github.com/zserge/anvil/blob/master/DSL.md).\n\n### Property setters\n\nThe setters are named as the view methods from Android SDK, but without the\n\"set\" prefix. E.g. \"text(s)\" instead of \"setText(s)\", \"backgroundDrawable(d)\"\ninstead of \"setBackgroundDrawable(d)\" and so on.\n\n### Event listeners\n\nEvent listeners also have names from the Android SDK, but without the \"set\"\nprefix and the \"Listener\" suffix, e.g. \"onClick\" instead of \"setOnClickListener\".\n\n### Handwritten bindings\n\nFor LayoutParams the bindings can't be generated easily, so it was faster to write them manually:\n\n* `size(width, height)` - set width and height. Special constants like `WRAP`, `FILL`\n  and `MATCH` are available.\n* `dip(x)` - returns the value in pixels for the dimension in density-independent\n  pixels. Often used with size, padding or margin properties.\n* `margin(m)`, `margin(h, v)`, `margin(l, t, r, b)` - set view margin for all 4\n  sides, for horizontal/vertical dimensions or for each side individually.\n* `weight(w)` - modifies view layout weight.\n* `layoutGravity(g)` - modifies layout gravity of a view. Common constants like\n  `START`, `END`, `CENTER`, `CENTER_VERTICAL`, etc. are available.\n* `align(verb, anchor)`, `align(verb)` - base functions for relative layout params.\n* `above(id)`, `alignBaseline(id)`, `alignBottom(id)`, `alignEnd(id)`, `alignLeft(id)`, `alignParentBottom()`,\n  `alignParentEnd()`, `alignParentLeft()`, `alignParentRight()`, `alignParentStart()`, `alignParentTop()`,\n  `alignRight(id)`, `alignTop(id)`, `below(id)`, `centerHorizontal()`, `centerVertical()`, `centerInParent()`,\n  `toEndOf(id)`, `toLeftOf(id)`, `toRightOf(id)`, `toStartOf(id)` - all possible settings for relative layout params\n\nA few bindings have been  written for other use cases which we find useful:\n\n* `init(Runnable)` - executes a runnable once, useful to initialize custom views (see also `Anvil.currentView()`).\n* `R()` - returns a `Resources` object associated with the current view. Useful for\n  multiple screen support (sizes, dpi, orientation etc).\n* `isPortrait()` - returns true if a screen is portrait-oriented. Useful for\n    tweaking layouts for different orientations.\n* `typeface(font)` - loads font from assets by its file name and sets the typeface to\n    a TextView\n* `padding(p)`, `padding(h, v)`, `padding(l, t, r, b)` - set view padding for all 4\n    sides, for horizontal/vertical dimensions or for each side individually.\n* `visibility(flag)` - sets the visibility property to `View.VISIBLE` or `View.GONE` depending on\n    the `flag` boolean value\n* `shadowLayer(radius, dx, dy, color)` - sets shadow layer of a TextView\n* `onTextChanged(textWatcher)` - binds a text watcher to an `EditText`. No\n    `Anvil.render()` is called in this case, because you're likely to get an infinite\n    recursion.\n* `text(StringBuilder)` - binds a string builder to the edit text, so when you\n    change its contents - the edit text is changed, and if you type something\n    manually - the string builder gets modified. So far it's the only two-way\n    data binding, becayse TextWatcher is a complicated beast.\n* `onItemSelected(lambda)` - accepts a functional interface to handle a `Spinner`\n    events. `onNothingSelected()` method is omitted, because it's rarely used anyway.\n\nIf a binding you need is not in the list - please, check [issue #27](https://github.com/zserge/anvil/issues/27) and report it there.\n\nA special case for animations is added:\n\n* `anim(trigger, Animator)` - starts animation when `trigger` is true, cancels it\n    when the `trigger` becomes false.\n\nFinally, a few low-level DSL functions are there, which you would no need unless you want to write your own property setters or custom view builders:\n\n* `v(class, attrs...)` - pushes view, applies attributes, doesn't pop the view.\n* `o()`, `x()` - names that look like bullets, actually pop the view. These are used in Java 6 syntax.\n* `v(class, renderable)` - pushes the view, applies the renderable to fulfil\n    attributes and child views, pops the view. This is used in Java 8 and Kotlin\n    syntax.\n* `attr(func, value)` - checks the cache for the given value of the given\n    property setter function. Often used to create your own property setter\n    binding.\n\n## XML layouts\n\nIf you're migrating an existing project to Anvil or if you prefer to keep your\nview layouts declared in XML - you can do so:\n\n``` kotlin\npublic override fun view() {\n    // A designer gave us an XML with some fancy layout:\n    // a viewgroup with a button and a progress bar in it\n    xml(R.layout.my_layout) {\n        backgroundColor(Settings.bgColor) // will modify root layout view color\n\n        withId(R.id.my_button) {\n            // button state may depend on some variable\n            enabled(isMyButtonEnabled)\n                // button click listener can be attached\n                onClick { v -\u003e\n                    ...\n                }\n        }\n\n        withId(R.id.my_progress_bar) {\n            visible(isMyProgressBarShown)\n            progress(someProgressValue)\n        }\n    }\n}\n```\n\nHere `xml()` creates a view node, much like `frameLayout()` or `textView()`,\nexcept for it uses an XML file to inflate the views, not the direct view class\nconstructor. Much like view \"builders\", `xml()` takes a renderable lambda as a\nparameter and uses that to modify the created view.\n\nAny attribute setters will affect the root view from the XML layout.\n\nIf you want to modify attributes of the child views from the XML - you should\nuse `withId()` to assign a renderable to a view with the given ID. You may not\nfollow the hierarchy of the child views, e.g. all your `withId()` calls may be\nnested as the views are nested in the XML, or may be direct children of the\n`xml()` renderable. `xml()` calls can be nested, too.\n\n`xml()` also allows you to create views that can not be inflated from Java code\nbut only from XML (such as hostozintal progress bars) or any other views where\nAttributeSet must be given or the views that rely on the `onFinishInflate`\nmethod to be called.\n\n`withId()` call is not limited to XML views, it can be used for views declared\nin code that have IDs. So if you have a custom viewgroup that creates its child\nviews automatically and assigns some IDs to them - you can still modify their\nproperties using `withId()`. Also, if any views are created inside the\n`withId()` or `xml()` - they will be appeneded to the view group:\n\n``` java\nv(MyComponent.java, () -\u003e {\n    withId(R.id.child_view, () -\u003e {\n        // R.id.child_view was implicitely created in MyComponent's constructor\n        // but we still can modify it here\n    });\n\n    textView(() -\u003e {\n        // This textView will be appeneded to MyComponent layout\n    });\n});\n```\n\n## License\n\nCode is distributed under MIT license, feel free to use it in your proprietary\nprojects as well.\n\n","funding_links":[],"categories":["Java","Libraries"],"sub_categories":["Data binding"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanvil-ui%2Fanvil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanvil-ui%2Fanvil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanvil-ui%2Fanvil/lists"}