<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by salis on Medium]]></title>
        <description><![CDATA[Stories by salis on Medium]]></description>
        <link>https://medium.com/@salismt?source=rss-ce1e45f5127d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Dm14QKGfplnDO-XNtLgOQA.jpeg</url>
            <title>Stories by salis on Medium</title>
            <link>https://medium.com/@salismt?source=rss-ce1e45f5127d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 05:12:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@salismt/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Android Vector and DataBinding Marriage part 2]]></title>
            <link>https://medium.com/@salismt/android-vector-and-databinding-marriage-part-2-8e9afd68fdaf?source=rss-ce1e45f5127d------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e9afd68fdaf</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[vector]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-binding]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[salis]]></dc:creator>
            <pubDate>Sat, 17 Sep 2016 04:32:36 GMT</pubDate>
            <atom:updated>2016-09-17T04:32:36.617Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5Pqd_3TpVaf_kPqnqHH00Q.jpeg" /></figure><p>I am back, now in the second part I want to talk about Vector and use databinding for it. In case you guys haven’t read the first part, here is the <a href="https://medium.com/@salismt/android-vector-and-databinding-marriage-part-1-bb0ac0a399ab#.wmxax8vao">link..</a></p><p>If you guys ever dealt with multiple screen density, then you must know the pain to have 5 identical images for your image. Not only it’s waste of time (thankfully there this <a href="http://romannurik.github.io/AndroidAssetStudio/icons-generic.html#source.type=image&amp;source.space.trim=1&amp;source.space.pad=0&amp;size=40&amp;padding=1&amp;color=33b5e5%2C0&amp;name=ic_home_bottom_tab_transaction">website</a>), but it’s also increase the APK size. Vector is really small in size, but you can scale it to the size of a football because it’s only save the coordinate of the image. Android has finally support Vector on <a href="http://android-developers.blogspot.com/2016/02/android-support-library-232.html">Android Support Library 23.2</a>, here’s a more information about <a href="https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.n1dsh3jtl">Android Vector</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*VtDDeHlRWl9qnsfJxmLalg.png" /><figcaption>say bye to this</figcaption></figure><p>First, let’s import an SVG into android studio. Right click on your <strong>res</strong> file -&gt; <strong>new</strong> -&gt; <strong>Vector Asset</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SQZRwDmDVoHZjfjzEqzGMQ.png" /></figure><p>You can also import from using Material Icon.</p><h4>Enabling the flag</h4><p>If you’re using v2.0 or above of the Gradle plugin, in your app gradle.</p><pre>android {<br>  defaultConfig {<br>    <strong>vectorDrawables.useSupportLibrary = true</strong><br>  }<br>}</pre><p>There are ways to use the Vector, you can inflate it from your layout or inflate it in your java file, either ways work.</p><pre>&lt;<strong>ImageView<br>    android:layout_width=&quot;wrap_content&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    app:srcCompat=&quot;@drawable/ic_android&quot;</strong>/&gt;</pre><pre>or</pre><pre>iv.setImageResource(R.drawable.ic_android);</pre><p>But you guys came here not for this right, as you might already find these resources somewhere else :D. Let’s put these together!!</p><p><strong>DataBinding and Vector</strong></p><p>Finally, now create a class <strong><em>BindingAdapters.java</em></strong> this class will contain the binding method for the vector.</p><pre><strong>public class </strong>BindingAdapters {<br><br>    @BindingAdapter(<strong>&quot;app:srcVector&quot;</strong>)<br>    <strong>public static void </strong>setSrcVector(ImageView view, @DrawableRes <strong>int </strong>drawable) {<br>        view.setImageResource(drawable);<br>    }<br><br>}</pre><p>A static binding adapter method with the <a href="https://developer.android.com/reference/android/databinding/BindingAdapter.html">BindingAdapter</a> annotation allows the developer to customize how a setter for an attribute is called. Add them on your xml layout.</p><pre>&lt;<strong>ImageView<br>    android:layout_width=&quot;wrap_content&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    app:srcVector=&quot;@{viewModel.image}&quot;</strong>/&gt;</pre><pre>@Bindable<br><strong>public int </strong>getImage() {<br>    <strong>return </strong>R.drawable.<strong><em>ic_android</em></strong>;<br>}</pre><p>Now you can set image just like we set the text, and voilaa!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*Y6-g7fDP8LKE4xpzRKoyvQ.gif" /></figure><p>That’s it guys, <strong><em>BindingAdapter </em></strong>is so powerful, you can also use <strong>Glide / Picasso</strong> to display your image, you can even have multiple parameters in the static functions.</p><p>Here is the link to the <a href="https://medium.com/@salismt/android-vector-and-databinding-marriage-part-1-bb0ac0a399ab#.5j7vnyv3u">project</a></p><p>If you guys have any questions, let me know!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e9afd68fdaf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Vector and DataBinding Marriage part 1]]></title>
            <link>https://medium.com/@salismt/android-vector-and-databinding-marriage-part-1-bb0ac0a399ab?source=rss-ce1e45f5127d------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb0ac0a399ab</guid>
            <category><![CDATA[vector]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[data-binding]]></category>
            <category><![CDATA[vectordrawable]]></category>
            <dc:creator><![CDATA[salis]]></dc:creator>
            <pubDate>Fri, 16 Sep 2016 16:05:52 GMT</pubDate>
            <atom:updated>2016-09-17T03:23:51.756Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5Pqd_3TpVaf_kPqnqHH00Q.jpeg" /></figure><blockquote>I think Android DataBinding is one of the best Android libraries long since AppCompat, so does Vector despite all of its limitations. The question remains, how can I diminish its limitation by marrying them together?</blockquote><p>Databinding is one of the greatest leap in Android development ecosystem since AppCompat first Introduce, if done properly it will reduce the amount of logic in your activity, especially UI related. You could easily change the state of your layout by passing different value inside. The most important thing is, say goodbye to <em>findViewById </em>and <em>setText. </em>Wohooo!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*OOZmGKHJqJYNfOpY_9sZcA.jpeg" /><figcaption><em>Every Single Time…</em></figcaption></figure><p>Well before I start marrying this match made in heaven, first let me tell you the basic stuff on how the databinding works. I split it into two parts, the first part I want to talk about the databinding, the second part about Vector and how can we combine them together.</p><p>first, on your project<strong> <em>build.gradle</em></strong> set Android Plugin for Gradle <strong>1.5.0-alpha1</strong> or higher. This time we will use <strong>2.1.3</strong></p><pre>buildscript {<br>    repositories {<br>        jcenter()<br>    }<br>    dependencies {<br>        classpath <strong>&#39;com.android.tools.build:gradle:2.1.3&#39;</strong><em><br>    </em>}<br>}</pre><p>and in your app <strong><em>build.gradle</em></strong> add the dataBinding</p><pre>android {<br>    ....</pre><pre>    dataBinding {<br>        enabled = <strong>true<br>    </strong>}<br>}</pre><p>that’s all you need to setup databinding support, but how do I use it? Ok, first start small.</p><p>first create a layout like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*aJ7J7bOEjL16U-_RtZnKrw.png" /></figure><p>nothing ordinary, but the difference is you will set a data from java file.</p><p>How would you normally do it? You would do <em>findViewById()</em> for the <em>TextView</em> and do <em>setText</em> in your activity. What if you have so many UI interaction and showing different state? You will see bunch of <em>setText, setVisibility, </em>and the list goes on…</p><p>This is where DataBinding becomes handy. Instead you can pass a <strong>ViewModel</strong> to act as a reference/bind to the UI state, hence the name is DataBinding or this will lead to MVVM <em>Daahh..</em></p><p>Normal layout, you will see</p><pre>@Override<br><strong>protected void </strong>onCreate(Bundle savedInstanceState) {<br>    <strong>super</strong>.onCreate(savedInstanceState);<br>    setContentView(R.layout.<strong><em>activity_main</em></strong>);</pre><pre><em>&lt;?</em><strong>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</strong><em>?&gt;<br></em>&lt;<strong>LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>.....&gt;<br>    <br>    &lt;--! Body of your layout --&gt;<br>&lt;/LinearLayout&gt;</strong></pre><p>Instead, you go to your XML layout, and wrap it inside this bracket <strong>&lt;layout&gt;</strong></p><pre><em>&lt;?</em><strong>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</strong><em>?&gt;<br></em>&lt;<strong>layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</strong>&gt;<br><br>    &lt;<strong>LinearLayout<br>        android:id=&quot;@+id/content_main&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        ....&gt;<br>        &lt;--! Body of your layout--&gt;<br>    &lt;/LinearLayout&gt;<br>&lt;/layout&gt;</strong></pre><p>And it will automatically generate a binding class in your java file, so you can do this to <em>setContentView</em> your layout</p><pre>ActivityMainBinding <strong>binding</strong>;<br><br>@Override<br><strong>protected void </strong>onCreate(Bundle savedInstanceState) {<br>    <strong>super</strong>.onCreate(savedInstanceState);<br>    <strong>binding </strong>= DataBindingUtil.<em>setContentView</em>(<strong>this</strong>, R.layout.<strong><em>activity_main</em></strong>);</pre><p>ActivityMainBinding is your xml in Java representation, if you go to its declaration, it will go to the xml file. Hence the name is the same with your layout file with <strong><em>Binding</em></strong><em> </em>suffix. It will simply inflate the layout into your activity.</p><p>Now how do I bind the data to the layout, you can pass a model to act as a <strong>ViewModel</strong>, now let’s create <strong>ExampleViewModel.java</strong> and pass it to your xml like this</p><pre><em>&lt;?</em><strong>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</strong><em>?&gt;<br></em>&lt;<strong>layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</strong>&gt;<br><br>    &lt;<strong>data</strong>&gt;<br>        &lt;<strong>variable<br>            name=&quot;viewModel&quot;<br>            type=&quot;com.bloonerd.vectordatabinding.ExampleViewModel&quot;</strong>/&gt;<br>    &lt;/<strong>data</strong>&gt;<br><br>    &lt;<strong>LinearLayout<br>        android:id=&quot;@+id/content_main&quot;<br>        ....</strong></pre><p>inside <strong>&lt;data&gt; name</strong> acts like a variable to the layout, while the <strong>type </strong>is the Model you reference to. Set the viewModel like this</p><pre><strong>private </strong>String <strong>text</strong>;<br><br><strong>public </strong>ExampleViewModel(String text) {<br>    <strong>this</strong>.<strong>text </strong>= text;<br>}<br><br><strong>public </strong>String getText() {<br>    <strong>return text</strong>;<br>}</pre><p>and on your layout, set the text reference to the <em>getText()</em></p><pre>&lt;<strong>TextView<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;30dp&quot;<br>    android:background=&quot;@color/colorPrimary&quot;<br>    android:textColor=&quot;@android:color/white&quot;<br>    android:gravity=&quot;center&quot;<br>    android:text=&quot;@{viewModel.text}&quot;</strong>/&gt;</pre><p>Wait, why it’s only <strong><em>viewModel.text</em></strong><em> </em>instead of <strong><em>viewModel.getText</em></strong><em>? </em>Actually, android DataBinding follows JavaBean convention, so it will ignore suffix <strong><em>is</em> </strong>for boolean, and <strong><em>get </em></strong>for other types.</p><p>Ok, now let’s bind the ViewModel with the Layout</p><pre>@Override<br><strong>protected void </strong>onCreate(Bundle savedInstanceState) {<br>    <strong>super</strong>.onCreate(savedInstanceState);<br>    <strong>binding </strong>= DataBindingUtil.<em>setContentView</em>(MainActivity.<strong>this</strong>, R.layout.<strong><em>activity_main</em></strong>);<br><br>    <strong>binding</strong>.setViewModel(<strong>new </strong>ExampleViewModel(<strong>&quot;Hello world&quot;</strong>));</pre><p>that’s it, it will show the Hello World on the blue box. Note that <strong><em>setViewModel </em></strong>function is depending on you variable name inside your xml.</p><pre>&lt;<strong>layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</strong>&gt;<br><br>    &lt;<strong>data</strong>&gt;<br>        &lt;<strong>variable<br>            name=&quot;viewModel&quot;</strong></pre><p>if you name it to something else like <strong><em>ironMan</em></strong> it will have <strong><em>setIronMan</em></strong> instead.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/1*i1MOYbawkSJ8ovv07X02Ag.png" /></figure><p>What if you want to update the value for example by click a button, this is when <em>BaseObservable</em> come into play. Now on your <strong><em>ExampleViewModel.java</em></strong> extend it to <em>BaseObservable.</em></p><pre><strong>public class </strong>ExampleViewModel <strong>extends </strong>BaseObservable {<br>  <br>  ...</pre><pre>  @Bindable <br>  public String getText() {<br>    return text;<br>  }</pre><pre>  public void setText(String text) {<br>    this.text = text;<br>    notifyChange() // it will notify all the @Bindable methods<br>    // or notifyPropertyChanged(BR.text) but need to rebuild<br>  }</pre><pre>viewModel = <strong>new </strong>ExampleViewModel(<strong>&quot;Hello world&quot;</strong>);<br>    <strong>binding</strong>.setViewModel(viewModel);<br>}<br><br><strong>public void </strong>onButtonClick(View view) {<br>    viewModel.setText(<strong>&quot;It&#39;s clicked&quot;</strong>);<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*tDA6jX0XcrrdyiegBfZztw.png" /><figcaption>Voila, the text changed!</figcaption></figure><p>What if if I want to bind a <em>TextView</em> to an <em>EditText</em> as I type, the value changes. Well you have it, it’s Two Way DataBinding. In two way databinding, you can call the <em>View</em><strong><em> </em></strong>method inside your xml, and have the viewModel as the parameter of its method.</p><pre>&lt;<strong>EditText<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    android:layout_marginTop=&quot;5dp&quot;<br>    android:addTextChangedListener=&quot;@{viewModel.textWatcher}&quot;</strong>/&gt;</pre><pre>&lt;<strong>TextView<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    android:text=&quot;@{viewModel.inputText}&quot;</strong>/&gt;</pre><pre>@Bindable<br><strong>public </strong>String getInputText() {<br>    <strong>return inputText</strong>;<br>}<br><br><strong>public </strong>TextWatcher <strong>textWatcher </strong>= <strong>new </strong>TextWatcher() {<br>    @Override<br>    <strong>public void </strong>beforeTextChanged(CharSequence s, <strong>int </strong>start, <strong>int </strong>count, <strong>int </strong>after) {<br><br>    }<br><br>    @Override<br>    <strong>public void </strong>onTextChanged(CharSequence s, <strong>int </strong>start, <strong>int </strong>before, <strong>int </strong>count) {<br><br>    }<br><br>    @Override<br>    <strong>public void </strong>afterTextChanged(Editable s) {<br>        <strong>inputText </strong>= s.toString();<br>        notifyChange();<br>    }<br>};</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*iuf4ECEykGMcRfafqwAdEA.gif" /></figure><p>That’s it for the first part. Next I will talk how can we use the <em>View</em><strong><em> </em></strong>method in the layout and <strong><em>BindingAdapter</em></strong> in order to bind vector. We will also talk about <em>VectorCompat</em> and how can we diminish its limitation with Databinding..</p><p>You can find the project in this <a href="https://github.com/salismt/databinding-vector">link</a></p><p>STAY TUNE!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb0ac0a399ab" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>