Skip to content

Commit 5eaf97d

Browse files
committed
First example with data binding
1 parent eb169db commit 5eaf97d

27 files changed

Lines changed: 547 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 23
5+
buildToolsVersion "23.0.2"
6+
7+
defaultConfig {
8+
applicationId "com.vogella.android.testapp"
9+
minSdkVersion 23
10+
targetSdkVersion 23
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
21+
dataBinding {
22+
enabled = true;
23+
}
24+
25+
}
26+
27+
dependencies {
28+
compile fileTree(dir: 'libs', include: ['*.jar'])
29+
testCompile 'junit:junit:4.12'
30+
compile 'com.android.support:appcompat-v7:23.1.1'
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/vogella/Android/Sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.vogella.android.testapp;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.vogella.android.testapp">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
<activity android:name=".MainActivity">
12+
<intent-filter>
13+
<action android:name="android.intent.action.MAIN" />
14+
15+
<category android:name="android.intent.category.LAUNCHER" />
16+
</intent-filter>
17+
</activity>
18+
</application>
19+
20+
</manifest>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.vogella.android.testapp;
2+
3+
import android.app.Activity;
4+
import android.databinding.DataBindingUtil;
5+
import android.os.Bundle;
6+
7+
import com.vogella.android.testapp.databinding.ActivityMainBinding;
8+
9+
public class MainActivity extends Activity {
10+
11+
@Override
12+
protected void onCreate(Bundle savedInstanceState) {
13+
super.onCreate(savedInstanceState);
14+
String textFieldValue ="";
15+
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
16+
MainActivityHandlers handler = new MainActivityHandlers();
17+
binding.setHandler(handler);
18+
}
19+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.vogella.android.testapp;
2+
3+
import android.content.Context;
4+
import android.databinding.BaseObservable;
5+
import android.databinding.Bindable;
6+
import android.databinding.BindingAdapter;
7+
import android.databinding.ObservableField;
8+
import android.text.Editable;
9+
import android.text.TextWatcher;
10+
import android.view.View;
11+
import android.widget.EditText;
12+
import android.widget.Toast;
13+
14+
import java.util.Objects;
15+
16+
public class MainActivityHandlers extends BaseObservable {
17+
public ObservableField<String> textFieldValue = new ObservableField<>();
18+
private Context context;
19+
20+
public void onClick(View view) {
21+
context = view.getContext();
22+
Toast.makeText(context, textFieldValue.get(), Toast.LENGTH_SHORT).show();
23+
}
24+
25+
@Bindable
26+
public String getName() {
27+
return textFieldValue.get();
28+
}
29+
30+
public void setName(String name) {
31+
textFieldValue.set(name);
32+
notifyPropertyChanged(com.vogella.android.testapp.BR.name);
33+
}
34+
35+
public final TextWatcher textWatcher = new TextWatcher() {
36+
@Override
37+
public void afterTextChanged(Editable s) {
38+
if(!s.toString().equalsIgnoreCase(getName()))
39+
setName(s.toString());
40+
}
41+
42+
@Override
43+
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
44+
45+
@Override
46+
public void onTextChanged(CharSequence s, int start, int before, int count) {}
47+
};
48+
49+
// // Add this to avoid that the setText method is called in case the model changes
50+
// @BindingAdapter("binding")
51+
// public static void bindEditText(EditText editText, CharSequence value) {
52+
// if (!editText.getText().toString().equals(value.toString())) {
53+
// editText.setText(value);
54+
// }
55+
// }
56+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:bind="http://schemas.android.com/tools">
4+
<data>
5+
<variable
6+
name="handler"
7+
type="com.vogella.android.testapp.MainActivityHandlers" />
8+
</data>
9+
10+
<RelativeLayout
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent"
13+
android:paddingBottom="@dimen/activity_vertical_margin"
14+
android:paddingLeft="@dimen/activity_horizontal_margin"
15+
android:paddingRight="@dimen/activity_horizontal_margin"
16+
android:paddingTop="@dimen/activity_vertical_margin">
17+
18+
<EditText
19+
android:id="@+id/main_input"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:layout_alignParentTop="true"
23+
android:layout_alignParentStart="true"
24+
android:layout_alignParentEnd="true"
25+
android:addTextChangedListener="@{handler.textWatcher}"
26+
/>
27+
28+
<Button
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:layout_alignLeft="@+id/main_input"
32+
android:layout_below="@+id/main_input"
33+
android:layout_marginTop="31dp"
34+
android:onClick="@{handler.onClick}"
35+
android:text="Start" />
36+
37+
</RelativeLayout>
38+
</layout>
3.34 KB
Loading

0 commit comments

Comments
 (0)