Skip to content

Commit a02b64c

Browse files
committed
data-binding listview
1 parent d9d162f commit a02b64c

File tree

39 files changed

+980
-0
lines changed

39 files changed

+980
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ captures/
3434

3535
# IntelliJ
3636
*.iml
37+
.idea/*
3738
.idea/workspace.xml
3839
.idea/tasks.xml
3940
.idea/gradle.xml
@@ -63,3 +64,8 @@ fastlane/Preview.html
6364
fastlane/screenshots
6465
fastlane/test_output
6566
fastlane/readme.md
67+
app/
68+
databinding-example/libs/
69+
gradle/
70+
gradlew.bat
71+
libs/

.idea/modules.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,64 @@
11
# Databinding-Kotlin-Android
22
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.

build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
ext.kotlin_version = '1.2.41'
5+
ext.gradle_version = '3.1.2'
6+
repositories {
7+
google()
8+
jcenter()
9+
}
10+
dependencies {
11+
classpath "com.android.tools.build:gradle:$gradle_version"
12+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
13+
14+
// NOTE: Do not place your application dependencies here; they belong
15+
// in the individual module build.gradle files
16+
}
17+
}
18+
19+
allprojects {
20+
repositories {
21+
google()
22+
jcenter()
23+
}
24+
}
25+
26+
task clean(type: Delete) {
27+
delete rootProject.buildDir
28+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
apply plugin: 'kotlin-kapt'
5+
6+
android {
7+
compileSdkVersion 27
8+
9+
defaultConfig {
10+
applicationId "com.github.databinding.databinding_listview_example"
11+
minSdkVersion 21
12+
targetSdkVersion 27
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
17+
18+
}
19+
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
27+
dataBinding {
28+
enabled = true
29+
}
30+
}
31+
32+
dependencies {
33+
implementation fileTree(dir: 'libs', include: ['*.jar'])
34+
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
35+
implementation 'com.android.support:appcompat-v7:27.1.1'
36+
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
37+
testImplementation 'junit:junit:4.12'
38+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
39+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
40+
41+
// required for data binding
42+
kapt "com.android.databinding:compiler:$gradle_version"
43+
44+
// added for loading image from url
45+
implementation 'com.squareup.picasso:picasso:2.5.2'
46+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.databinding.listviewexample
2+
3+
import android.support.test.InstrumentationRegistry
4+
import android.support.test.runner.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getTargetContext()
22+
assertEquals("com.github.databinding.databinding_listview_example", appContext.packageName)
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.github.databinding.listviewexample">
4+
5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
<application
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:roundIcon="@mipmap/ic_launcher_round"
11+
android:supportsRtl="true"
12+
android:theme="@style/AppTheme">
13+
<activity android:name="com.github.databinding.listviewexample.MainActivity">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN"/>
16+
17+
<category android:name="android.intent.category.LAUNCHER"/>
18+
</intent-filter>
19+
</activity>
20+
</application>
21+
22+
</manifest>

0 commit comments

Comments
 (0)