An MVP app example using Databinding
Add the following in the module level build.gradle file e.g. app/build.gradle,
-
Add the following inside android section:
dataBinding { enabled = true }
-
Along with other plugins add the following in the top:
apply plugin: 'kotlin-kapt'
-
Add the following in the dependencies:
kapt "com.android.databinding:compiler:$gradle_version"
-
Create a model class, e.g. SearchViewModel in this example.
-
In the xml file of the activity where data binding will be used, create a layout tag. Place data tag inside this layout tag. If we have multiple data models which we want to bind to the Ui, we can create different data variables. We use the name field to access parameters of Model class.
-
Inside the activity, where we implement data binding, create a binding variable of ActivityMainBinding.
-
Replace the defaule setContentView with the DataBindingUtil.setContentView:
e.g. binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
- This is a basic example showing use of data binding to bridge the communication between data and UI.
- This app takes user input and makes a query to the Github public API to search repositories. https://developer.github.com/v3/search/#search-repositories.
- Remember to add android.permission.INTERNET in the AndroidManifest file as any network call will need this permission.
- 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.
- In this example we briefly cover making network calls, parsing the response and then communicating the parsed data back to the UI thread.
- Use of Picasso Library has been included to highlight getting data from an URL.
- The response is displayed in List View using data binding.
- The app preserves data on runtime configuration changes like Screen rotations, keyboard changes.