Example in Collection Observer – Collection Size Converter

In Android-Binding, if you need to “observe” an Observable, you can implement DependentObservable or simply the Observer.

For ObservableCollections (i.e. CursorCollection / ArrayListObservable),  the change of collection items is not notified. If you need to know the changes of collection items, you need CollectionObserver.

Collection Observer will raise in following situations:

  1. Item Added
  2. Item Remove
  3. Item Replaced
  4. Item Moved
  5. Collection Resetted (Cleared, Requery, etc)

All these information is given in the CollectionChangedEventArgs.

Example – Collection Size Converter

In a recent project, I need to show the number of items in an collection. So, I made a simple converter named COLLECTION_SIZE. It takes only one argument, which is the collection, and returns an integer of the number of items in it. Here is the code:

public class COLLECTION_SIZE extends Converter<Integer> {
	public COLLECTION_SIZE(IObservable<?>[] dependents) {
	    super(Integer.class, dependents);
    }

	private IObservableCollection<?> mCol = null;

	@Override
    public Integer calculateValue(Object... arg0) throws Exception {
		IObservableCollection<?> collection = (IObservableCollection<?>)arg0[0];
		if (mCol != collection){
			if (mCol!=null){
				mCol.unsubscribe(observer);
			}
			mCol = collection;
			collection.subscribe(observer);
		}

		return collection.size();
    }

	private CollectionObserver observer = new CollectionObserver(){
		@Override
        public void onCollectionChanged(IObservableCollection<?> arg0,
                CollectionChangedEventArg arg1, Collection<Object> arg2) {
			COLLECTION_SIZE.this.setDirty(true);
			COLLECTION_SIZE.this.notifyChanged();
        }
	};
}

Upcoming major release of Android-Binding

In the next major release of Android-Binding, quite a number of enhancements were made:

  1. New IKernel class to allow pluggable behavior in Android-Binding, includes adding custom Binding Providers, and possibly of changing the Syntax resolution algorithm
  2. Support Binding to View Pager which involves a few new Converters to bind “Collection” to View Pager
  3. More ICS support, particularly the Action Mode and Action Bar
  4. Added Generic View Attribute, which allows virtually any View attribute with “getter” to be functional
  5. Binding Syntax now supports “Float”, e.g. binding:width=”0.5″ will translate to FloatObservable(0.5f)

Apart from new features, some of the deprecated method/class is officially removed from the framework. Probably one that matters much will be the gueei.binding.Validation is gone from the main framework but continue to distribute as plugin. Another removed method will be setAndBindRootView/setAndBindView etc. from Binder and BindingActivity. I want to encourage the use of Activity Meta XML file for better code/frontend separation and easier to target different version of the framework.

I am expecting this will be released by end of October, if you are interested in what’s new here, please check the MarkupDemoICS in the v0.6 branch.

Detect Home Screen is running or not (Android)

Since home screen widget animation is very expensive, we might want to know if the widget is “visible” or not, to determine whether need to do the animation. In such case, we need home screen detection.

Following is how I do in the Eva Clock HD:

public class DetectHomeScreen {
	private static List<String> homePackageNamesCache;

	public static List<String> homePackageName(Context context){
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		List<ResolveInfo> info = context.getPackageManager().queryIntentActivities(intent, 0);
		List<String> homes = new ArrayList<String>(info.size());
		for(int i=0; i<info.size(); i++){
			homes.add(info.get(i).activityInfo.packageName);
		}
		homes.add(DetectHomeScreen.class.getPackage().getName());
		return homes;
	}

	public static boolean isHomeRunning(Context context){
		if (homePackageNamesCache==null)
			homePackageNamesCache = homePackageName(context);
		return isHomeRunning(context, homePackageNamesCache);
	}

	public static boolean isHomeRunning(Context context, List<String> homePackages){
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		String topPackage = am.getRunningTasks(1).get(0).topActivity.getPackageName();
		return homePackages.contains(topPackage);
	}
}

The above is checked periodically, or query it right before you want to update/start your home animation. Since querying all packages with HOME intent is very expensive, I cached that part, such that it may not work when user installed a NEW launcher and use it (well, a rare case).

Using Wheel View Widget: Adopting third-party widgets to Android-Binding

Wheel View in action from my exposure helper

Last year I bought a Pen-FT from ebay, which became my most favorite film camera. Only thing that bothers me is the malfunction light meter and there seems no way to fix. There are many apps in Android that can help calculating a-s settings, but I don’t find they are handy enough. So, I started the above stuff (note: the above is still work-in-progress so it looks ugly)

In designing the interface, I found that I need to use a vertical wheel, after some research, I end up finding the above one from here. This one looks good and so I decided to include in my project; but I need to make it compatible with Android-Binding.

It’s not difficult to make a view compatible with Android-Binding, especially when you are not going to ship that widget as an independent library: you just need to implement those attributes that you needed. In the Wheel View, I need a few attributes only:

  1. adapter: by default, WheelView takes WheelViewAdapter as adapter, and it is NOT inherited from android.widget.adapter. We’ll investigate this later;
  2. selectedPosition: Integer, two-way bindable; and if I change the value, it got cool animation animating the movement.

Read more…

[Tutorial] Hello Android Binding (new)

Update 2

The build file is upgraded to have “Wizard-like” choices. No need to choose which target to run, just right-click on the “wizard.xml” and run as ant build.

Update

Just released an ant build file to help generating activity and application. The following tutorial is updated with using ant build.

For those who are already using Android-Binding

I just released the Android-Binding 0.5, this is the first official release to support features in HC/ICS, which includes:

  1. Action Bar
  2. Action Modes

As mentioned in previous post, we decided not to include fragment and we provide alternatives to fragments, and those already included in previous releases.

If you are targeting platforms prior to HC, do you need this update? Yes, because we also included binding to ViewPager and to Activity itself.

Please notice about the new binding to activity xml metadata file, as we are deprecating the setAndBindRootView() method.

For everyone

Android-Binding is a MVVM framework for used in Android. It help separate the View (widgets) from View Model (Java codes). This post should be your first post into working with Android-Binding.

Read more…

Android Color Picker Widget – Creating MVVM aware Widget

Download the source project for this Color Picker Widget.

In the recent release Eva Clock HD, the setting screen is implemented with Android-Binding. In that setting screen, I need a color picker to let user select their preferred color to appear in the clock, here’s how it looks like:

It’s consisting of two parts. First, a TextView that the background color is the same as the selected color, and second, a color picker dialog, which shows when user tap on the TextView.

Read more…

Eva Clock HD comments post

I will prioritize the following new features base on votes. Thanks for your feedback!

For comments and other suggestion, please post as comment.

Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: