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.

Evangelion Clock V2.0 released

Finally, after almost 2 years I remake the whole Evangelion Clock widget. This time is a total remake and I do all the codes and graphics from scratch, and if you are a lucky kid and have a HD screen, you would really notice the difference in details. In addition, due to some popular demand, I throw in some nice feature to it and soon will release more, including:

  1. Customize Color Theme (Currently not including background color, but will soon include it)
  2. Tap to Launch (It’s Still here! Don’t trust those comments in app store) Remember to touch the “clock” instead of buttons.. buttons is doing something else
  3. Config button REALLY brings you to config
  4. Time will be current time (anyone need another time zone? poll it!)
  5. Count (not working now) is supposed to be a simple count down timer, like 3 minutes (for making cup noodles)…
  6. Batt (not now) is the “guessed” run-time remaining in your phone
  7. Mini version (removed, consider to re-include it later)

I need some help. Since multiple state will be available in the widget (battery, countdown, time etc.) I need some good Japanese words to be replaced in “活動限界までActive Time Remaining”. 活動限界まで will be reserved to battery only, but countdown/time would need a different name. If you have good idea, please comment!

Oh! forgot to mention, the config screen is powered by Android-Binding: MVVM framework for Android!

When an attribute or widget is not yet supported by Android-Binding

There are so many default view widgets in Android, although we tried hard to keep up, some attributes or views are still not supported by Android-Binding. Of course, we open to any suggestion to include them, please report it in the issue tracker or post in the discussion group.

Wait, you want to DIY? We have two approaches:

  1. Extend that View that you need attributes, and treat it like Custom View (Easier)
  2. Create your own Binding Provider (Better reusability, and please submit it to me!)
Following I will discuss with adding a attribute to WebView, named “data”, and the usage is simple:
binding:data=”STRING”
This reflects the method WebView.loadData();

Read more…

[Tutorial] Lazy Loading : List Views in Android Binding (4/4)

This is the  last post in this series, which is about lazy loading. Note this lazy loading is different from the lazy loading on the other infinite list post. Which, for infinite list, you don’t yet know how many items will be on your list, you just load them bit by bit whenever necessary; but for the lazy loading here, you know what and how many items here, but you don’t load “partial” of the content until it is necessary.

The most common scenario is a list of images, either coming from local disk or the Internet. Setting Image to Image View is a quite expensive operation, even you already cached the image in local drive. People love to fast scroll to skim a list, and this makes list very unresponsive if the image is loaded even in fast scrolling mode.

Kindly note this is the last post of this series, where the rest are:

  1. Introduction to List Views Binding
  2. Custom Row Model for List View
  3. Binding to Cursors
  4. Lazy Loading (this)

Read more…