android-data-binding-command
radzio
29
Visit GitHub RepoDesign Patterns

Android Data Binding Command

Command design pattern implementation for Android Data Binding

Demo

gif2

How to start?

Step 1. Add it in your root build.gradle at the end of repositories:

	allprojects {
		repositories {
			...
			maven { url "https://jitpack.io" }
		}
	}

Step 2. Add the dependency

	dependencies {
	        compile 'com.github.radzio:android-data-binding-command:1.0.4'
	}

Share this release:

Make sure that you are using Android Data Binding

  • In your main build.gradle you have gradle plugin 1.5.0 or newer:
classpath 'com.android.tools.build:gradle:1.5.0'
  • In your app build.gradle you've enabled databinding:
    dataBinding {
        enabled = true
    }

Add to your code

Modify your layout, add bind:command="@{viewModel.myCommand}"

        <android.support.design.widget.FloatingActionButton
            bind:command="@{viewModel.myCommand}"
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_email"/>

Add in your binding class

    public ICommand myCommand = new Command()
    {
        @Override
        public void execute()
        {
            MainActivityViewModel.this.view.showMessage("Hello from Command!");
        }
    };

Command features

Each command implements ICommand interface

    public interface ICommand extends Observable
    {
        @Bindable
        boolean isEnabled();
    
        void isEnabled(boolean isEnabled);
    
        @Bindable
        boolean isRefreshing();
    
        void isRefreshing(boolean isRefreshing);
    
        void execute();
    }

You can use isEnabled and isRefreshing properties in your java code and xml layouts:

     <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:layout_below="@+id/button"
            bind:command="@{viewModel.enableButtonCommand}"
            android:text="@{viewModel.buttonCommand.enabled ? `Disable Timer` : `Enable Timer`}"
            android:enabled="@{viewModel.buttonCommand.refreshing == false}"
            android:layout_centerHorizontal="true"/>
Become a better Android Developer
Millions of developers are learning at MindOrks

Online Training Program

Featured Blogs

Our Team

MindOrks is Certainly one of the best online blog to stay on top of all the Android development news, coding and design patterns. Finally a blog I can count on to keep myself updated with latest and greatest things happening in Android world.

Vipul Shah
Android Developer
Microsoft

Become Pro in Android by watching videos

OUR LEARNERS WORK AT