尿黄是什么原因| 离职是什么意思| 菠萝和什么不能一起吃| 食欲不振吃什么药| 寒号鸟是什么动物| 宫颈多发纳囊是什么病| 监督的近义词是什么| 棉涤是什么面料| 交泰殿是干什么的| 高同型半胱氨酸血症吃什么药| 包皮发炎红肿用什么药| 余字五行属什么| 感染幽门螺杆菌吃什么药| 为什么泡完脚后非常痒| 左肺下叶钙化灶是什么意思| TPS什么意思| 伤口吃什么消炎药| 气管炎吃什么药| 卖什么意思| 脚心疼痛是什么原因引起的| 女人吃什么排卵最快| 山梨糖醇是什么| 糖尿病人能吃什么| 牙龈出血缺什么| 水洗棉是什么面料| 身份证什么时候可以办| 手外科属于什么科| 真菌镜检阴性是什么意思| 跳梁小丑是什么生肖| 膝盖发软无力是什么原因| 鸡胸肉炒什么好吃| cto是什么职位| noxa20是什么药| ab型rh阳性是什么意思| 隔空打牛是什么意思| 鼻子红是什么原因| 医院什么时候下班| 朱元璋为什么不传位给朱棣| jb是什么意思| 慢性肠胃炎吃什么药| 痛风是什么病| 做牛排需要什么调料| 焦虑症吃什么药效果好| 狗可以吃什么| 七月初八是什么星座| 符咒是什么意思| 贫血看什么科| 什么族不吃猪肉| 口巴读什么| 睾丸炎有什么症状| 越南人说什么语言| 左肾肾盂分离什么意思| 发光免疫是检查什么的| 光顾是什么意思| 急性胆囊炎吃什么药| 3月27号是什么星座| 乌鸡炖什么好吃| 黄金茶属于什么茶| 昔人是什么意思| 耳鸣耳聋吃什么药| 脑白质病变是什么意思| 梦见钱包丢了是什么意思| 刘备的马叫什么名字| 重庆房价为什么这么低| 裂纹舌是什么原因引起的| 急性支气管炎吃什么药| 骨髓不造血是什么病| 什么的口罩| 宫腔占位什么意思| 绿色痰是什么原因| 豆腐皮炒什么好吃| 什么食物补锌| dhea是什么| 什么面好吃| cp组合什么意思| 单抗是什么药| 一个虫一个离念什么| 胎盘宫底后壁是什么意思| 百香果有什么好处功效| 橄榄绿是什么颜色| 突然头晕眼花站不稳是什么原因| 头发沙发是什么意思| 微创是什么| 六月底是什么星座| 有什么颜色| 小腿经常抽筋是什么原因| 杨玉环属什么生肖| 手脚发胀是什么前兆| 梦见蛇是什么意思| 男士去皱纹用什么好| 南红五行属什么| 胃疼可以吃什么药| 阑尾炎有什么症状表现| 脂肪肝喝什么茶最好| 缺点是什么意思| 试商是什么意思| 胃溃疡什么症状| diff什么意思| 花生和什么不能一起吃| 4月25号什么星座| 蝉吃什么东西| 冷宫是什么意思| 塞屁股的退烧药叫什么| 100分能上什么大学| 结节性甲状腺肿是什么意思| 11月29号什么星座| 载波是什么意思| 须发早白吃什么中成药| 山加乘念什么| 脑梗吃什么好| 啤酒酵母是什么| 1946年属什么生肖属相| 脸霜什么牌子的好| 经常拉稀是什么原因| 咳嗽吐白痰是什么病| 在五行中属什么| hot什么意思| 夫妻备孕检查挂什么科| 为什么屁股上会长痘| 龟吃什么| 体育精神是什么| 养儿防老下一句是什么| 扇贝不能和什么一起吃| 肚脐眼上面痛是什么原因引起的| generic是什么意思| 现在什么餐饮最火| 拔牙后不能吃什么食物| 蚊子为什么咬人| 今日属相是什么生肖| 甲五行属什么| 甲亢平时要注意什么| 脂肪肝中医叫什么名字| 出家需要什么条件| 内分泌代谢科是看什么病的| 佟丽娅是什么民族| 为什么刚吃完饭不能洗澡| be结局是什么意思| 蛋白粉适合什么人群吃| b2c模式是什么意思| 1946属什么生肖| 尿赤是什么意思| 丈二和尚摸不着头脑是什么意思| 揣测是什么意思| 眼发花是什么病的征兆| amber是什么意思| 血清铁低是什么原因| 什么水果治便秘| 男生说gg是什么意思| 出虚汗是什么原因引起的怎么调理| cct是什么意思| 什么的尾巴长不了歇后语| 大米发霉是什么样子| 抑郁症发作是什么感觉| 盐酸哌替啶是什么药| 胎位lsa是什么意思| 黑色的裤子配什么颜色的上衣| 焗油是什么意思| 什么可以代替人体润滑油| 男人结扎有什么好处| 为什么会阳痿| 孕妇过敏性鼻炎可以用什么药| 毛豆吃多了有什么坏处| 平诊是什么意思| 哮喘用什么药| 肾结石是什么原因| svc是什么意思| 钠是什么意思| 闻名的闻什么意思| 女孩叫兮兮是什么意思| imei是什么意思| 皮脂膜是什么| 怀孕有积液是什么原因| 高姿属于什么档次| kenzo是什么牌子| 小产吃什么好恢复营养| 三角形为什么具有稳定性| 见红是什么颜色| 三伏天是什么时候| dna倍体异常细胞是什么意思| 日出东方下一句是什么| 什么东西燃烧脂肪最快| 每天放屁多是什么原因| 心脏不好最忌讳吃什么| 儿童吃什么长个子最快| 偶是什么意思| 鸡的守护神是什么菩萨| 胎动是什么感觉| 妈妈a型爸爸b型孩子是什么血型| 什么的城市| 眼睛肿胀是什么原因| 党内警告处分有什么影响| 八股文是什么| 房颤有什么危害| 鼻子歪了是什么原因| 冰冻三尺非一日之寒什么意思| 吃韭菜有什么好处和坏处| 阴疽是什么病| 堃怎么读什么意思| 经常泡脚有什么好处| 肠胃不好吃什么| 白带增多是什么原因| 花椒吃多了对身体有什么影响| 睡眠障碍吃什么药最好| 血糖高可以喝什么粥| 闭合性跌打损伤是什么意思| 拔牙后能吃什么东西| 肛门下坠感是什么原因| 考试为什么要用2b铅笔| 寒露是什么意思| po是什么的缩写| 结果是什么意思| 海鲜中毒有什么反应| 过度紧张会有什么症状| 乙肝表面抗体定量偏高什么意思| evian是什么品牌| 拉条子是什么意思| 胎停了有什么症状| 尿酸高是什么原因| 心慌吃什么药| 后续是什么意思| 尿液阳性是什么意思| 女人跑马是什么意思| 面瘫是什么| 收放自如是什么意思| 晴雨伞是什么意思| 1978年属马五行缺什么| 为什么要军训| 女人心肌缺血吃什么药| 雕琢是什么意思| 桥本甲状腺炎吃什么药| 舌尖溃疡是什么原因| zd是什么意思| 61岁属什么| 10月出生的是什么星座| 什么是量子力学| 癸水的根是什么| 松露是什么| 门静脉增宽是什么意思| 你正在干什么用英语怎么说| 梅核气西医叫什么| 罗汉果可以和什么一起泡水喝| 指甲有竖纹是什么原因| 锁骨中间的窝叫什么| 运动减肥为什么体重不减反增| 吉代表什么生肖| 什么的交流| hpv阳性是什么病| 遥不可及是什么意思| 不什么其烦| 腋下是什么经络| 院士是什么级别| 草酸钙结晶是什么意思| 什么生肖最好| 为什么不能叫醒梦游的人| 寻的部首是什么| squirrel是什么意思| 强字五行属什么| 玳瑁色是什么颜色| 屋里有蝙蝠有什么预兆| 番石榴什么时候成熟| 肝火旺吃什么| 尿ph值高是什么意思| 幼儿急疹为什么不能碰水| 胎停是什么意思| 百度
Sitemap
Android Developers

Articles on modern tools and resources to help you build experiences that people love, faster and easier, across every Android device.

Android view binding article header

New in Android Studio 3.6, view binding gives you the ability to replace findViewById with generated binding objects to simplify code, remove bugs, and avoid all the boilerplate of findViewById.

TL;DR

  • Enable view binding in build.gradle (no libraries dependencies)
  • View binding generates a binding object for every layout in your module (activity_awesome.xmlActivityAwesomeBinding.java)
  • Binding object contains one property for every view with an id in the layout — with the correct type and null-safety
  • Full support for both the Java programming language and Kotlin

Update build.gradle to enable view binding

You don’t need to include any extra libraries to enable view binding. It’s built into the Android Gradle Plugin starting with the versions shipped in Android Studio 3.6. To enable view binding, configure viewBinding in your module-level build.gradle file.

// Available in Android Gradle Plugin 3.6.0
android {
viewBinding {
enabled = true
}
}

In Android Studio 4.0, viewBinding has been moved into buildFeatures [release notes] and you should use:

// Android Studio 4.0
android {
buildFeatures {
viewBinding = true
}
}

Once enabled for a project, view binding will generate a binding class for all of your layouts automatically. You don’t have to make changes to your XML — it’ll automatically work with your existing layouts.

View binding works with your existing XML, and will generate a binding object for each layout in a module.

You can use the binding class whenever you inflate layouts such as Fragment, Activity, or even a RecyclerView Adapter (or ViewHolder).

Use view binding in an Activity

If you have a layout called activity_awesome.xml, which contains a button and two text views, view binding generates a small class called ActivityAwesomeBinding that contains a property for every view with an ID in the layout.

You don’t have to call findViewById when using view binding — instead just use the properties provided to reference any view in the layout with an id.

The root element of the layout is always stored in a property called root which is generated automatically for you. In an Activity’s onCreate method you pass root to setContentView to tell the Activity to use the layout from the binding object.

Easy Mistake: Calling setContentView(…) with the layout resource id instead of the inflated binding object is an easy mistake to make. This causes the layout to be inflated twice and listeners to be installed on the wrong layout object.

Solution: When using view binding in an Activity, you should always pass the layout from the binding object with setContentView(binding.root).

Safe code using binding objects

findViewById is the source of many user-facing bugs in Android. It’s easy to pass an id that’s not in the current layout — producing null and a crash. And, since it doesn’t have any type-safety built in it’s easy to ship code that calls findViewById<TextView>(R.id.image). View binding replaces findViewById with a concise, safe alternative.

View bindings are…

  • Type-safe because properties are always correctly typed based on the views in the layout. So if you put a TextView in the layout, view binding will expose a TextView property.
  • Null-safe for layouts defined in multiple configurations. View binding will detect if a view is only present in some configurations and create a @Nullable property.

And since the generated binding classes are regular Java classes with Kotlin-friendly annotations, you can use view binding from both the Java programming language and Kotlin.

What code does it generate?

View binding generates a Java class that replaces the need for findViewById in your code. It will generate one binding object for every XML layout in your module while mapping names so activity_awesome.xml maps toActivityAwesomeBinding.java.

When editing an XML layout in Android Studio, code generation will be optimized to only update the binding object related to that XML file, and it will do so in memory to make things fast. This means that changes to the binding object are available immediately in the editor and you don’t have to wait for a full rebuild.

Android Studio is optimized to update the binding objects immediately when editing XML layouts.

Let’s step through the generated code for the example XML layout from earlier in this post to learn what view binding generates.

Properties generated by view binding are correctly typed and null-safe.

View binding will generate one correctly-typed property for each view with a specified id. It will also generate a property called rootView that’s exposed via a getter getRoot. View binding doesn’t do any logic– it just exposes your views in a binding object so you can wire them up without error-prone calls to findViewById. This keeps the generated file simple (and avoids slowing down builds).

If you’re using Kotlin, this class is optimized for interoperability. Since all properties are annotated with @Nullable or @NonNull Kotlin knows how to expose them as null-safe types. To learn more about interop between the languages, check out the documentation for calling Java from Kotlin.

View binding generates an inflate method, which is the primary way to make new binding objects.

In ActivityAwesomeBinding.java, view binding generates a public inflate method. The one argument version passes null as the parent view and doesn’t attach to parent. View binding also exposes a three argument version of inflate that lets you pass the parent and attachToParent parameters when needed.

The call to bind is where the magic happens. It will take the inflated layout and bind all of the properties, with some error checking added to generate readable error messages.

Simplified version of the generated public bind method.

The bind method is the most complex code in the generated binding object, with a call to findViewById for each view to bind. And here you can see the magic happen – since the compiler can check the types and potential nullability of each property directly from the XML layouts it can safely call findViewById.

Note, the actual generated code for the bind method is longer and uses a labeled break to optimize bytecode. Check out this post by Jake Wharton to learn more about the optimizations applied.

On each binding class, view binding exposes three public static functions to create a binding an object, here’s a quick guide for when to use each:

  • inflate(inflater) – Use this in an Activity onCreate where there is no parent view to pass to the binding object.
  • inflate(inflater, parent, attachToParent) – Use this in a Fragment or a RecyclerView Adapter (or ViewHolder) where you need to pass the parent ViewGroup to the binding object.
  • bind(rootView) – Use this when you’ve already inflated the view and you just want to use view binding to avoid findViewById. This is useful for fitting view binding into your existing infrastructure and when refactoring code to use ViewBinding.

What about included layouts

One binding object will be generated for each layout.xml in a module. This is true even when another layout <include>s this this layout.

Example of include tag in view binding. Note that the <include> tag has an id!

In the case of included layouts, view binding will create a reference to the included layout’s binding object.

Note that the <include> tag has an id: android:id="@+id/includes". This is required for view binding to generate a property (just like a normal view).

Include tags must have an id to generate a binding property.

View binding will generate a reference to the IncludedButtonsBinding object in ActivityAwesomeBinding.

Using view binding and data binding

View binding is only a replacement for findViewById. If you also want to automatically bind views in XML you can use the data binding library. Both libraries can be applied to the same module and they’ll work together.

When both are enabled, layouts that use a <layout> tag will use data binding to generate binding objects. All other layouts will use view binding to generate binding objects.

You can use data binding and view binding in the same module.

We developed view binding in addition to data binding because many developers provided feedback that they wanted a lighter weight solution to replace findViewById without the rest of the data binding library – and view binding provides that solution.

View binding and Kotlin synthetics or ButterKnife

One of the most common questions asked about view binding is, “Should I use view binding instead of Kotlin synthetics or ButterKnife?” Both of these libraries are used successfully by many apps and solve the same problem.

For most apps we recommend trying out view binding instead of these libraries because view binding provides safer, more concise view lookup.

Zoom image will be displayed
View binding is always null safe, only references views from the current layout, supports java and Kotlin, and is concise.
Comparing features of View binding, ButterKnife, and Kotlin synthetics.

While ButterKnife validates nullable/non-null at runtime, the compiler does not check that you’ve correctly matched what’s in your layouts

We recommend trying out view binding for safe, concise, view lookup.

Learn more

To learn more about view binding check out the official documentation.

And we’d love to hear your experiences with #ViewBinding library on twitter!

--

--

Android Developers
Android Developers

Published in Android Developers

Articles on modern tools and resources to help you build experiences that people love, faster and easier, across every Android device.

Responses (29)

什么是阻生智齿 听调不听宣什么意思 诺如病毒吃什么药好得快一点 sp是什么面料成分 白头发多吃什么食物能变黑
吃盐吃多了有什么危害 着床出血什么时候出现 丹五行属性是什么 1978年属什么的 蜘蛛的血液是什么颜色
一个三点水一个除念什么 爱是什么偏旁 农历八月初三是什么星座 烤鱼一般用什么鱼 农历五月二十四是什么日子
懿代表什么意思 本虚标实是什么意思 宫颈纳囊是什么意思 88年属龙的是什么命 gary是什么意思
瘙痒是什么意思qingzhougame.com 人参果什么季节成熟hcv8jop6ns4r.cn 过门是什么意思hcv7jop9ns8r.cn 鬼压床是什么原因造成的hcv9jop0ns3r.cn 例假颜色发黑是什么原因hcv7jop6ns9r.cn
脚板肿是什么原因引起的hcv9jop4ns3r.cn 沙棘不能和什么一起吃hcv9jop5ns8r.cn 蚕蛹过敏什么办法最快hcv8jop3ns5r.cn 子宫内膜异位症有什么症状表现imcecn.com 缺硒有什么症状hcv8jop9ns1r.cn
脚背抽筋是什么原因引起的zhongyiyatai.com 女左上眼皮跳是什么预兆naasee.com 羊肉炖什么好吃又营养xianpinbao.com 心脏为什么会突然刺痛hcv8jop5ns3r.cn 熊猫是什么科hcv8jop6ns5r.cn
喝葡萄糖有什么功效与作用hcv8jop3ns6r.cn 半夏是什么hcv9jop6ns5r.cn 脸颊两边长痘痘是什么原因引起的hcv8jop8ns9r.cn ecl是什么意思kuyehao.com 痤疮是什么意思hcv9jop5ns4r.cn
百度