Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Data Binding from zero

Android Data Binding from zero

What is Android Data binding why and why not Data Binding. how to start working with it. Learn about Binding Adapter and Observables.

Seyyed davud hosseiny

June 21, 2018
Tweet

More Decks by Seyyed davud hosseiny

Other Decks in Programming

Transcript

  1. About me • دیوردنا سیون همانرب 4 لاس • تاهرپ

    رد راک هب لوغشم • ناسر مایپ یاه همانرب یور یراک هبرجت
  2. About me • دیوردنا سیون همانرب 4 لاس • تاهرپ

    رد راک هب لوغشم • ناسر مایپ یاه همانرب یور یراک هبرجت • نیلتاک راد تسود
  3. About me • دیوردنا سیون همانرب 4 لاس • تاهرپ

    رد راک هب لوغشم • ناسر مایپ یاه همانرب یور یراک هبرجت • نیلتاک راد تسود • https://github.com/DHosseiny
  4. About me • دیوردنا سیون همانرب 4 لاس • تاهرپ

    رد راک هب لوغشم • ناسر مایپ یاه همانرب یور یراک هبرجت • نیلتاک راد تسود • https://github.com/DHosseiny @ hosseinydavid
  5. What is Data Binding? • هب هداد عبانم لاصتا View

    رد اه xml layout • راکدوخ تیدپآ ییاناوت UI هداد رییغت اب
  6. What is Data Binding? • هب هداد عبانم لاصتا View

    رد اه xml layout • راکدوخ تیدپآ ییاناوت UI هداد رییغت اب • Say no to findViewById
  7. Why Data Binding? • رت هداس و رتزیمت دک •

    شهاک نودب performance • شهاک هب کمک Null Pointer Exception
  8. Why not Data Binding? • دنوش یم هداد ناشن لیاپماک

    نامز رد دک نتشون نامز یاج هب اهاطخ
  9. Why not Data Binding? • دنوش یم هداد ناشن لیاپماک

    نامز رد دک نتشون نامز یاج هب اهاطخ • IDE Support فیعض
  10. Why not Data Binding? • دنوش یم هداد ناشن لیاپماک

    نامز رد دک نتشون نامز یاج هب اهاطخ • IDE Support فیعض • IDE Support فیعض
  11. Why not Data Binding? • دنوش یم هداد ناشن لیاپماک

    نامز رد دک نتشون نامز یاج هب اهاطخ • IDE Support فیعض • IDE Support فیعض • IDE Support فیعض
  12. How to start? .1 build.gradle .2 Layout tag <FrameLayout android:layout_width="match_parent"

    android:layout_height="match_parent"> <TextView android:id="@+id/text_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout>
  13. How to start? .1 build.gradle .2 Layout tag <layout> <FrameLayout

    android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout> </layout>
  14. How to start? .1 build.gradle .2 Layout tag <layout> <data>

    <variable name="user" type="ir.logcat.databinding.User" /> </data> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout> </layout>
  15. How to start? .1 build.gradle .2 Layout tag <layout> <data>

    <variable name="user" type="ir.logcat.databinding.User" /> </data> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.name}" /> </FrameLayout> </layout>
  16. How to start? .1 build.gradle .2 Layout tag <layout> <data>

    <variable name="user" type="ir.logcat.databinding.User" /> </data> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.name}" /> </FrameLayout> </layout> @{}
  17. How to start? .1 build.gradle .2 Layout tag .3 inflate

    public class MainActivity extends AppCompatActivity { User user; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
  18. How to start? .1 build.gradle .2 Layout tag .3 inflate

    public class MainActivity extends AppCompatActivity { User user; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); } }
  19. How to start? .1 build.gradle .2 Layout tag .3 inflate

    public class MainActivity extends AppCompatActivity { User user; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); } }
  20. How to start? .1 build.gradle .2 Layout tag .3 inflate

    public class MainActivity extends AppCompatActivity { User user; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); } }
  21. How to start? .1 build.gradle .2 Layout tag .3 inflate

    public class MainActivity extends AppCompatActivity { User user; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); binding.setUser(user); } }
  22. How to start? .1 build.gradle .2 Layout tag .3 inflate

    public class MainActivity extends AppCompatActivity { User user; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); binding.setUser(user); binding.textName.setText("Android Developers"); } }
  23. How to start? .1 build.gradle .2 Layout tag .3 inflate

    public class MainActivity extends AppCompatActivity { User user; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); binding.setUser(user); binding.textName.setText("Android Developers"); } } Type Safe
  24. Inflation • DataBindingUtil.setContentView(activity, R.layout.activity_main); • ListItemBinding binding = ListItemBinding.inflate(inflater, parent,

    attachToParent); • ListItemBinding binding = ListItemBinding.bind(view); For Activity For Any layout
  25. Inflation • DataBindingUtil.setContentView(activity, R.layout.activity_main); • ListItemBinding binding = ListItemBinding.inflate(inflater, parent,

    attachToParent); • ListItemBinding binding = ListItemBinding.bind(view); For Activity For Any layout For inflated view
  26. Basics Java package ir.logcat.databinding; import android.view.View; private User user; Xml

    <layout> <data> <import type="android.view.View" /> <variable name="user" type="ir.parsdroidgroup.databinding.User" /> </data>
  27. Basics Java package ir.logcat.databinding; import android.view.View; private User user; @Override

    protected void onCreate(Bundle savedInstanceState) { ... } Xml <layout> <data> <import type="android.view.View" /> <variable name="user" type="ir.parsdroidgroup.databinding.User" /> </data>
  28. Basics Java package ir.logcat.databinding; import android.view.View; private User user; @Override

    protected void onCreate(Bundle savedInstanceState) { ... } Xml <layout> <data> <import type="android.view.View" /> <variable name="user" type="ir.parsdroidgroup.databinding.User" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout> </layout>
  29. Supported Operators and keywords • Mathematical + - / *

    % • String concatenation + • Logical && ||
  30. Supported Operators and keywords • Mathematical + - / *

    % • String concatenation + • Logical && || • Comparison == < > <= >=
  31. Supported Operators and keywords • Mathematical + - / *

    % • String concatenation + • Logical && || • Comparison == < > <= >= • instanceOf
  32. Supported Operators and keywords • Mathematical + - / *

    % • String concatenation + • Logical && || • Comparison == < > <= >= • instanceOf • null
  33. Supported Operators and keywords • Mathematical + - / *

    % • String concatenation + • Logical && || • Comparison == < > <= >= • instanceOf • null • Cast
  34. Supported Operators and keywords • Mathematical + - / *

    % • String concatenation + • Logical && || • Comparison == < > <= >= • instanceOf • null • Cast • Method calls
  35. Supported Operators and keywords • Mathematical + - / *

    % • String concatenation + • Logical && || • Comparison == < > <= >= • instanceOf • null • Cast • Method calls • Array access []
  36. Supported Operators and keywords • Mathematical + - / *

    % • String concatenation + • Logical && || • Comparison == < > <= >= • instanceOf • null • Cast • Method calls • Array access [] • Ternary operator ?:
  37. Supported Operators and keywords • Mathematical + - / *

    % • String concatenation + • Logical && || • Comparison == < > <= >= • instanceOf • null • Cast • Method calls • Array access [] • Ternary operator ?: • Elvis operator ??
  38. Supported Operators and keywords • Mathematical + - / *

    % • String concatenation + • Logical && || • Comparison == < > <= >= • instanceOf • null • Cast • Method calls • Array access [] • Ternary operator ?: • Elvis operator ?? • Lambdas and Method references
  39. Examples android:text="@{String.valueOf(index + 1)}“ android:visibility="@{age < 13 ? View.GONE :

    View.VISIBLE}“ android:transitionName='@{"image_" + id}' android:text="@{user.displayName ?? user.lastName}" android:text="@{list[index]}"
  40. Binding Adapters <imageView android:src="@{user.imageUrl}" /> @BindingAdapter("android:src") public static void loadImage(ImageView

    imageView, String url) { if (url == null) return; RequestOptions requestOptions = RequestOptions.circleCropTransform(); Glide.with(imageView.getContext()) .load(url) .apply(requestOptions) .into(imageView); }
  41. Binding Adapters <imageView android:src="@{user.imageUrl}" bind:placeholder="@{R.drawable.placeholder}" /> @BindingAdapter("android:src") public static void

    loadImage(ImageView imageView, String url) { if (url == null) return; RequestOptions requestOptions = RequestOptions.circleCropTransform(); Glide.with(imageView.getContext()) .load(url) .apply(requestOptions) .into(imageView); }
  42. Binding Adapters <imageView android:src="@{user.imageUrl}" bind:placeholder="@{R.drawable.placeholder}" /> @BindingAdapter(value = {"android:src", "bind:placeholder"},

    requireAll = false) public static void loadImage(ImageView imageView, String url, int placeHolder) { if (url == null) return; RequestOptions requestOptions = RequestOptions.circleCropTransform(); if (placeHolder != 0) { requestOptions = requestOptions.placeholder(placeHolder); } Glide.with(imageView.getContext()) .load(url) .apply(requestOptions) .into(imageView); }
  43. BaseObservable public class User { private String name; public User(String

    name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } }
  44. BaseObservable public class User extends BaseObservable { private String name;

    public User(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } }
  45. BaseObservable public class User extends BaseObservable { private String name;

    public User(String name) { this.name = name; } public void setName(String name) { this.name = name; notifyPropertyChanged(BR.name); } public String getName() { return this.name; } }
  46. BaseObservable public class User extends BaseObservable { private String name;

    public User(String name) { this.name = name; } public void setName(String name) { this.name = name; notifyPropertyChanged(BR.name); } @Bindable public String getName() { return this.name; } }
  47. ObservableFieldand it’s primitive friends ObservableField<T> ObservableBoolean ObservableByte ObservableChar ObservableShort ObservableInt

    ObservableLong ObservableFloat ObservableDouble ObservableParcelable<T> ObservableList<T> ObservableArrayList<T>
  48. ObservableFieldand it’s primitive types public class User { private String

    name; public User(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } }
  49. ObservableFieldand it’s primitive types public class User { private ObservableField<String>

    name; public User(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } }
  50. ObservableFieldand it’s primitive types public class User { private ObservableField<String>

    name; public User(String name) { this.name = name; } public void setName(String name) { this.name.set(name); } public String getName() { return this.name; } }
  51. ObservableFieldand it’s primitive types public class User { private ObservableField<String>

    name; public User(String name) { this.name = name; } public void setName(String name) { this.name.set(name); } public String getName() { return this.name.get(); } }
  52. ObservableFieldand it’s primitive types public class User { private ObservableField<String>

    name; public User(String name) { this.name = name; } public void setName(String name) { this.name.set(name); } public String getName() { return this.name.get(); } } <TextView android:text="@{user.name}" />