Posts

Android Architecture Components : Room Persistence Library

Image
Room: a SQLite object mapping library After many years and using countless 3rd party DAO libraries I am happy to finally introduce official Android solution to working with SQLite databases and it is called Room Persistence Library. Before we begin you can find code from this tutorial here . SQL Review Data is stored in tables Table's rows contain entities Table's columns contain entity's fields Rows can have id's (primary keys) and id's must be unique You can create relationships between rows in different tables by referring to other row's id (foreign key) Index primary key columns for performance Use SQL queries to find sort and filter the data from tables What is Room? Room is a library that uses annotations in your Java data models and your data access objects (DAO's) to generate database schema (table definitions) and SQLite queries. So you don't have to write all the SQLite queries from scratch and have logic for storing and r

Android Data Binding : Dynamic RecyclerView Adapter

Image
One of the most common list design patterns that I encounter as Android developer is a list with a variation of the following: Multiple row types.  Expandable/Collapsible rows or sections.  Load more/Scroll to load more.  Delete rows/Sections.  And all of these can be implemented in a generic manner with Android Data Binding library. You can write one adapter for all the RecycleView s in your app and use it to render all the common design patterns. Check out the source for the example in this tutorial here . Final result: Lets begin by defining our adapter: public class RecyclerViewBindingAdapter extends RecyclerView.Adapter<RecyclerViewBindingAdapter.BindingViewHolder> { private ObservableList<AdapterDataItem> data; public RecyclerViewBindingAdapter(ObservableList<AdapterDataItem> data) { this.data = data; data.addOnListChangedCallback(new ObservableListCallback()); } @Override public BindingViewHolder onCreateV

Android Data Binding : Tricks and Magic

Image
You can do a lot of awesome things while using the Android Data Binding library. Some will save you a lots of time and some will seem like magic. You can see the full source code for this tutorial here . Custom XML attributes You can create custom xml attributes and add them to existing UI elements. For example if you ever had to use a custom font files in your application you would always have to set the type face programmatically as the TextView or its sub classes did not have an xml attribute to set the custom type face file from xml. With data binding you can implement your own attributes quick and easy. First lets define our binding adapter.  public class AttributeBindings { @BindingAdapter({"bind:typeface"}) public static void setTypeFace(TextView view, TypeFaceType tft) { Log.i("AttributeBindings", "setting typeface:"+tft); Typeface typeface = null; switch (tft) { case NORMAL: typefa

Android Data Binding : How to Integrate Into Existing App

Image
Android Data Binding Library has been production ready for almost a year and you may be wondering if you can start using this great library in an existing project without introducing more bugs and without having to rewrite a lot of exiting code in your project. Yes you can, and its very easy to start! And why wouldn't you? Why wait to start a new project in few months or years and miss out on all the benefits of this library?! As the library offers significant time savings to developers and enforces good coding practices and as a result making your code cleaner and more maintainable. You can pull the the code for this tutorial here . It is easy to begin. If your project is minimum API level 7 or higher and is using Android Plugin for Gradle 1.5.0-alpha1 or higher then you can simply enable the Data Binding library by adding the following to the app module's build.gradle file android { .... dataBinding { enabled = true } } And that's it! Now you