Profile cover photo
Profile photo
yiğit boyar
796 followers
796 followers
About
yiğit boyar's posts

Post has shared content
Vector drawables in XML, even with support 23.3.0. Guess how?

You've probably heard that the latest support library release today removed some support for backward-compatible inflation of vector drawables.¹ 😭 You can still use app:srcCompat="@drawable/vector", but android:src won't work and neither will android:drawableLeft (and so on).

However, if you use data binding, this isn't a huge deal. Overloading android: XML attributes has been one of the perks of data binding from the start!² I've posted a gist showing how to inflate and set vector drawables using the same XML attributes you were using before.

https://gist.github.com/lisawray/78c33f76809d2bcbbec9983e2c141a70

Note that:
1) You must inflate your layout using data binding
2) Import R in the <data></data> block and reference your vector drawable like R.drawable.vector instead of @drawable/vector.
3) For non-vector drawables, simply don't use data binding, e.g. android:src="@drawable/png".

¹https://plus.google.com/+AndroidDevelopers/posts/iTDmFiGrVne
²http://developer.android.com/tools/data-binding/guide.html
Photo

Post has shared content
Full example on Github: Custom fonts in XML using data binding

I've posted a full sample project which uses data binding to add a custom font attribute to TextView.  It includes a font cache and reads font names automatically from your assets/fonts folder, so there is no setup required.  Just paste the files into your own project and drag and drop your own fonts!  Have fun.

Post has shared content

Post has shared content
more data binding awesomeness
Pro tip: More data binding — Easy view visibility in XML!

I heard you guys like data binding.  Here's another trick I've added to my portfolio: boolean visibility.

When I first started on Android, it seemed so obvious to me a View was visible, or not, I often made mistakes in my xml files like:

android:visible="false"

Don't laugh!  You know you do too.  After four years I can now type View.GONE in my sleep, so when I first started with data binding, I imported View and wrote code like this:
<data>
<import type="android.view.View"/>
</data>

<TextView 
android:visibility="@{user.role== null ? View.GONE : View.VISIBLE}"
/>

You know this pattern; it exists wherever optional fields of data are displayed in the UI.  My code reviewer objected this was awkward and wordy.  I began to point out that View has three states of visibility and therefore a boolean would not do when I realized he was right.  I had some kind of View-induced Stockholm syndrome and there was now a better way — I had known it when I first started.

I now use 
app:visible="@{user.role!= null}"

and in my Java code, I wrote ONCE AND FOREVER:
@BindingAdapter({"bind:visible"})
public static void setVisible(View view, boolean visible) {
view.setVisibility(visible ? View.VISIBLE : View.GONE);
}

AND THEN I WAS FREE.

Notes:
- Someone will probably ask; data binding does avoid NPEs (e.g. android:text="@{user.role}" would not throw an exception if role is null) but it will not automatically hide the view—just make it empty. Sometimes this is enough; often (like in the example user badge) you need to hide the view to achieve correct alignment or avoid gaps.
- What about View.INVISIBLE?!  You can still use it. If you want.

Data binding: 
https://developer.android.com/tools/data-binding/guide.html
Photo

Post has shared content
Dev Tip
One-line drawable tint using in XML using data binding

Inspired by +Lisa Wray tip[1] to customise fonts using DataBinding I started exploring this lib yesterday. 
Here there is an easy way to tint Drawables using DataBinding[2] and DrawableCompat[3] 

@BindingAdapter("bind:colorTint")
    public static void setColorTint(ImageView view,@ColorRes int color) {
        DrawableCompat.setTint(view.getDrawable(), color);
    }

In XML it looks like:
<ImageView
  style="@style/circle"
  android:src="@drawable/circle"
  app:colorTint="@{item.color}"/>

I feel we have only started scratching the surface of DataBinding power. Definitively check it out. It is a game changer.

[1] Custom Fonts tip: https://plus.google.com/+LisaWrayZeitouni/posts/LTr5tX5M9mb
[2] DataBinding https://developer.android.com/tools/data-binding/guide.html
[3] DrawableCompat https://developer.android.com/reference/android/support/v4/graphics/drawable/DrawableCompat.html 

Post has shared content
Android pro tip: One-line custom fonts in XML with data binding!

I started playing with data binding last night for real, and I'm amazed by the power it gives you.  Check out this one-liner to set a custom font on a TextView:

@BindingAdapter({"bind:font"})
public static void setFont(TextView textView, String fontName){
 textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName));
}

In XML:
<TextView
app:font="@{`Source-Sans-Pro-Regular.ttf`}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

Previously, this used to take endless lines of repetitive Java code (one for each TextView!). The missing custom typeface support in TextView has been a grievance held against the Android platform for years.  There's even an inventive library to address it[2]. Data binding makes all of that obselete.  If you can't tell, I'm in love!!  It's terse, powerful, and one of the biggest leaps forward I've seen for Android productivity.

As usual, you need to put your font file in assets/fonts/, and make sure to include the data binding framework.  And if you're doing this to many TextViews, just get a little more fancy and cache the Typeface instead of creating it every time (thanks for the reminder +Ian Lake!)

[1] https://developer.android.com/tools/data-binding/guide.html
[2] Calligraphy: https://github.com/chrisjenx/Calligraphy
Photo

Post has shared content
A fellow Android UI Toolkit team member, +yiğit boyar, just posted two great articles about RecyclerView. In particular, he discusses how animations work, and what you need to understand about RecyclerView and LayoutManager in order to get great animations with your own custom LayoutManager.

RecyclerView was one of my favorite features in the Lollipop release. For one thing, it took another run at the recycling-container problem with a more flexible architecture than ListView. But also, it offers default animations for basic add/remove operations in the container. No longer will you need to watch 10 different DevBytes on how to animate views in ListView! In fact, you should just get everything for free if you use the default LayoutManagers that the support library provides. But if you want to do your own custom layout, check out Yigit's articles to understand how it all works.

http://www.birbit.com/recyclerview-animations-part-1-how-animations-work/

http://www.birbit.com/recyclerview-animations-part-2-behind-the-scenes/

Post has attachment
my way of writing responsive REST clients.

Post has attachment
checkout our first open source Android project. A Job Manager to manage your app's background activity.

Post has attachment
Wait while more posts are being loaded