|
1 | 1 | # Databinding-Kotlin-Android |
2 | 2 | An MVP app example using Databinding |
| 3 | + |
| 4 | + |
| 5 | +## Steps to set up data binding |
| 6 | +Add the following in the module level build.gradle file e.g. app/build.gradle, |
| 7 | + 1. Add the following inside android section: |
| 8 | + |
| 9 | + ``` |
| 10 | + dataBinding { |
| 11 | + enabled = true |
| 12 | + } |
| 13 | + ``` |
| 14 | + 2. Along with other plugins add the following in the top: |
| 15 | + ``` |
| 16 | + apply plugin: 'kotlin-kapt' |
| 17 | + |
| 18 | + ``` |
| 19 | + 3. Add the following in the dependencies: |
| 20 | + |
| 21 | + ``` |
| 22 | + kapt "com.android.databinding:compiler:$gradle_version" |
| 23 | +
|
| 24 | + ``` |
| 25 | + 4. Create a model class, e.g. SearchViewModel in this example. |
| 26 | + |
| 27 | + 5. In the xml file of the activity where data binding will be used, create a layout tag. |
| 28 | + Place data tag inside this layout tag. If we have multiple data models which we want to bind to the Ui, |
| 29 | + we can create different data variables. We use the name field to access parameters of Model class. |
| 30 | + |
| 31 | + 6. Inside the activity, where we implement data binding, create a binding variable of ActivityMainBinding. |
| 32 | + |
| 33 | + 7. Replace the defaule setContentView with the DataBindingUtil.setContentView: |
| 34 | + ``` |
| 35 | + e.g. |
| 36 | + binding = DataBindingUtil.setContentView(this, R.layout.activity_main) |
| 37 | + ``` |
| 38 | + |
| 39 | +## Features included in the example: |
| 40 | +
|
| 41 | +#### Basic Data Binding: |
| 42 | +
|
| 43 | +* This is a basic example showing use of data binding to bridge the communication between data and UI. |
| 44 | +
|
| 45 | +#### Network Communication |
| 46 | +* This app takes user input and makes a query to the Github public API to search repositories. |
| 47 | +https://developer.github.com/v3/search/#search-repositories. |
| 48 | +* Remember to add android.permission.INTERNET in the AndroidManifest file as any network call will need this permission. |
| 49 | +
|
| 50 | +#### JSON Data Parsing |
| 51 | +* After receiving response from the API it parses the Json data to search for Repository Name, Description, Star count and URL of User's Avatar. |
| 52 | +
|
| 53 | +#### Communication between UI Thread and Background Thread |
| 54 | +* In this example we briefly cover making network calls, parsing the response and then communicating the parsed data back to the UI thread. |
| 55 | +
|
| 56 | +#### Loading Image from an URL |
| 57 | +* Use of Picasso Library has been included to highlight getting data from an URL. |
| 58 | +
|
| 59 | +#### Data Binding with List View |
| 60 | +* The response is displayed in List View using data binding. |
| 61 | +
|
| 62 | +#### Preserving data on Runtime Configuration Changes |
| 63 | +
|
| 64 | +* The app preserves data on runtime configuration changes like Screen rotations, keyboard changes. |
0 commit comments