{"id":18687989,"url":"https://github.com/anitaa1990/databindingexample","last_synced_at":"2025-04-12T05:30:57.955Z","repository":{"id":117939863,"uuid":"131569459","full_name":"anitaa1990/DataBindingExample","owner":"anitaa1990","description":"A demo of how to implement Data Binding in Android app","archived":false,"fork":false,"pushed_at":"2019-03-20T10:32:41.000Z","size":219,"stargazers_count":11,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-26T00:51:21.984Z","etag":null,"topics":["android","android-development","data-binding","databindingutil","demo","loginmodel","loginpresenter","pojo"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anitaa1990.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-30T07:53:46.000Z","updated_at":"2021-09-09T23:38:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"206688cb-529b-434b-bd8c-5562b9fd838e","html_url":"https://github.com/anitaa1990/DataBindingExample","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anitaa1990%2FDataBindingExample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anitaa1990%2FDataBindingExample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anitaa1990%2FDataBindingExample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anitaa1990%2FDataBindingExample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anitaa1990","download_url":"https://codeload.github.com/anitaa1990/DataBindingExample/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248523603,"owners_count":21118531,"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","android-development","data-binding","databindingutil","demo","loginmodel","loginpresenter","pojo"],"created_at":"2024-11-07T10:35:09.145Z","updated_at":"2025-04-12T05:30:57.916Z","avatar_url":"https://github.com/anitaa1990.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DataBindingExample\nA demo of how to implement Data Binding in Android app\u003c/br\u003e\u003c/br\u003e\n\n\n\u003cimg src=\"https://github.com/anitaa1990/DataBindingExample/blob/master/demo.png\" width=\"200\" style=\"max-width:100%;\"\u003e\u003cbr/\u003e\u003cbr/\u003e\n\n\nEach Activity in the demo app shows a sample implementation of data binding.\u003cbr/\u003e\n1. \u003ch4\u003eReplace findViewById  with data binding\u003c/h4\u003e\n  Step 1: In layout file, make ```\u003clayout\u003e``` tag as most parent tag or root tag. After adding it, build system will process it for data binding.\u003cbr/\u003e\n  Step 2: After above step, binding class will be generated based on same name of layout file (e.g. activity_main’s binding class will be generated as ActivityMainBinding). Set setContentView using DataBindingUtil.\u003cbr/\u003e\n  That’s it.\u003cbr/\u003e\n  Example: ```DataBinding1Activity```\u003cbr/\u003e\u003cbr/\u003e\n  \n2. \u003ch4\u003eHow can we change binding class name?\u003c/h4\u003e\nBy default, binding class is generated based on the name of the layout file and placed in a data binding package under the module package. E.g. the layout file activity_main.xml will generate ActivityMainBinding. If the module package is com.an.demo, then it will be placed in com.an.demo.databinding directory.\u003cbr/\u003e\nWe can change that by adding this tag ```\u003cdata class=\"MainActivityBinding\"\u003e``` to the layout file.\u003cbr/\u003eNow generated binding class is named MainActivityBinding\u003cbr/\u003e\n  That’s it.\u003cbr/\u003e\n  Example: ```DataBinding2Activity```\u003cbr/\u003e\u003cbr/\u003e\n  \n  \n3. \u003ch4\u003eHow to bind data in layout file?\u003c/h4\u003e\nStep 1: Create a POJO class i.e. User\u003cbr/\u003e\nStep 2: Add \u003cdata\u003e element to the layout file. \u003cdata\u003e element is a way to binding data with UI element.\u003cbr/\u003e\nStep 3: Add \u003cvariable\u003e element to the layout file. \u003cvariable\u003e is a object of POJO which we bind with UI.\u003cbr/\u003e\nStep 4: add ```@{user.name}``` in text property of TextView. Generally, Expressions within the layout are written in the attribute properties using the ```@{}``` syntax\u003cbr/\u003e\nStep 4: Set ```binding.setUser(user);``` in the Activity class\u003cbr/\u003e\nThat’s it.\u003cbr/\u003e\nExample: ```DataBinding2Activity```\u003cbr/\u003e\u003cbr/\u003e  \n  \n \n4. \u003ch4\u003eEvent Handling using data binding\u003c/h4\u003e\n  Step 1: In layout file, make ```\u003clayout\u003e``` tag as most parent tag or root tag.\u003cbr/\u003e\n  Step 2: After above step, binding class will be generated based on same name of layout file (e.g. activity_main’s binding class will be generated as ActivityMainBinding). Set setContentView using DataBindingUtil.\u003cbr/\u003e\n  Step 3:  Create a POJO class i.e. LoginModel and add it as a variable to the layout file.\u003cbr/\u003e\n  Step 4: Create a presenter class i.e. LoginPresenter and add it as a variable to the layout file.\u003cbr/\u003e\n  Step 5: ```android:onClick=\"@{presenter::onClickButton2}\"``` is an example of event binding with Method reference\n  That’s it.\u003cbr/\u003e\n  Example: ```MainActivity```\u003cbr/\u003e\u003cbr/\u003e  \n  \n  \n5. \u003ch4\u003eCustom Data Binding Implementation\u003c/h4\u003e\n  Step 1: In layout file, make ```\u003clayout\u003e``` tag as most parent tag or root tag. Create a POJO class i.e. LoginModel and add it as a variable to the layout file. i.e. LoginModel. Create a presenter class and add it as a variable to the layout file. i.e. LoginPresenter\u003cbr/\u003e\n  Step 2: Create a separate adapter class for Binding i.e. DataBindingAdapter. Functionality of the custom binding class would be to display a Toast message. Create the binding method inside the customer adapter class.i.e.\u003cbr/\u003e     \n ``` @BindingAdapter(\"toast\")\n     public static void showToast(View view, String toast) {\n        if(toast != null \u0026\u0026 !toast.isEmpty()) {\n            Toast.makeText(view.getContext(), toast, Toast.LENGTH_SHORT).show();\n        }\n    } \n```\nAdd the custom attribute to your view in the layout file.i.e. \n``` app:toast=\"@{loginInfo.loginMessage}\" ```  \u003cbr/\u003e\nStep 3: Set ```binding.setModel(new LoginModel());``` in the Activity class.\u003cbr/\u003e\nStep 4: Set ``` binding.setPresenter(new LoginPresenter()); ``` in the Activity class.\u003cbr/\u003e\nThat’s it.\u003cbr/\u003e\nExample: ```DataBinding3Activity```\u003cbr/\u003e\u003cbr/\u003e \n  \n  \n6. \u003ch4\u003eImplement RecyclerView using Data Binding\u003c/h4\u003e\n  Step 1: In layout file, make ```\u003clayout\u003e``` tag as most parent tag or root tag. \u003cbr/\u003e\n  Step 2: Create a custom ```BindingViewHolder``` class for reusability purposes.\u003cbr/\u003e\n  Step 3: Create your recyclerview adapter class and item layout class. Set data \u0026 variable tag to the layout item class.\u003cbr/\u003e\n  Step 4: Set ```binding.recyclerView.setAdapter(adapter);``` to the Activity class \u003cbr/\u003e\n  That’s it.\u003cbr/\u003e\n  Example: ```DataBinding4Activity```\u003cbr/\u003e\u003cbr/\u003e\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanitaa1990%2Fdatabindingexample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanitaa1990%2Fdatabindingexample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanitaa1990%2Fdatabindingexample/lists"}