<?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 Andranik Azizbekian on Medium]]></title>
        <description><![CDATA[Stories by Andranik Azizbekian on Medium]]></description>
        <link>https://medium.com/@azizbekian?source=rss-c05a99614dd4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*FRXI-q3FVolGpt5odDW1uw.png</url>
            <title>Stories by Andranik Azizbekian on Medium</title>
            <link>https://medium.com/@azizbekian?source=rss-c05a99614dd4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 01:43:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@azizbekian/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[Setup Jacoco & Sonarqube in multimodule multiflavor Kotlin Android project]]></title>
            <link>https://azizbekian.medium.com/setup-jacoco-sonarqube-in-multimodule-multiflavor-kotlin-android-project-d8e7b27aed36?source=rss-c05a99614dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8e7b27aed36</guid>
            <category><![CDATA[jacoco]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[sonarqube]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Andranik Azizbekian]]></dc:creator>
            <pubDate>Sun, 12 Jan 2020 18:58:06 GMT</pubDate>
            <atom:updated>2021-01-06T14:27:15.343Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OlB6pEMlnNCck_D5h_OFng.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@marcinjozwiak?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Marcin Jozwiak</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>Prelude</h3><p>The day has come at last! The day, when you are going to setup <a href="https://www.sonarqube.org/">SonarQube</a> in your Android project. And again, you are relying on google to lend you a helping hand, but this time it might not come that easy, because there is no <em>copy-paste</em> solution to take from the beloved stackoverflow.</p><p>At <a href="https://www.disqo.com/">Disqo</a> we are working on a multimodule multiflavor 100% Kotlin Android app. And the exact same day has came for us too. I’ve wasted a good amount of effort for simply writing a couple of dozen lines of code because:</p><ul><li>SonarQube docs are not straightforward: no clear understanding which properties should be used, which might be omitted</li><li>Few examples for Android projects and if you find some, they would mostly be related to Java or would be using SonarQube properties that are already obsolete (e.g. <strong>sonar.jacoco.reportPaths</strong>)</li><li>If you are lucky to find examples on Kotlin, then certainly they are solutions for single module projects</li><li>or maybe I’m extra bad at googling 🤷‍♂️</li></ul><p>Hopefully you will stumble across this article when trying to setup SonarQube on your project. Hopefully it will be helpful and will refrain you from wasting precious time! ⌛️</p><h3>Agenda</h3><p>We’ll first setup <a href="https://www.eclemma.org/jacoco/"><strong>JaCoCo</strong></a> — a tool, which will help us generate coverage report for the codebase. This report will contain detailed information as to which classes/paths have been covered by tests, and which are not covered.</p><p>Then we’ll setup SonarQube, which will use the coverage report produced by JaCoCo.</p><h3>Setting up JaCoCo</h3><p>In topmost <strong>build.gradle</strong> add JaCoCo to classpath:</p><pre>buildscript {<br>    repositories { ... }<br>    dependencies {<br>        ...<br>        classpath &quot;org.jacoco:org.jacoco.core:0.8.6&quot;<br>    }<br>}</pre><p>Inside the project create <strong>jacoco.gradle</strong> file with following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f25ae1a4891e9eb4c8dba23091bb4978/href">https://medium.com/media/f25ae1a4891e9eb4c8dba23091bb4978/href</a></iframe><p>Now inside each module’s build.gradle file <strong>apply</strong> the newly created <strong>jacoco.gradle </strong>as such:</p><pre>apply from: &quot;$project.rootDir/jacoco.gradle&quot;</pre><p>Now you’ll be able to generate JaCoCo coverage report. Assuming flavor name is <strong>dev</strong>, a report might be created by performing:</p><pre>./gradlew clean testDevDebugUnitTestCoverage</pre><h3>Setting up SonarQube</h3><p>You have to run SonarQube locally or on a remote machine. Setting that up is out of the scope of this post (that’s something that <a href="https://docs.sonarqube.org/latest/setup/get-started-2-minutes/">Google can certainly help with</a>). For simplicity we’ll assume that we have Sonar running remotely on some host.</p><p>Then in root <strong>build.gradle</strong> add SonarQube to classpath:</p><pre>buildscript {<br>    repositories { ... }<br>    dependencies {<br>        ...<br>        classpath &quot;org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.0&quot;<br>    }<br>}</pre><p>Inside the project create <strong>sonar.gradle</strong> file with the following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50661a5a7ae051cb19ae21e9ebfeb0c3/href">https://medium.com/media/50661a5a7ae051cb19ae21e9ebfeb0c3/href</a></iframe><p>Let’s apply the newly created <strong>sonar.gradle</strong> to our project. We should add following line inside <strong>root build.gradle:</strong></p><pre>apply from: &quot;$project.rootDir/sonar.gradle&quot;</pre><p>Now we’ll apply sonarqube plugin to all subprojects using the <strong>subprojects</strong> DSL inside topmost <strong>build.gradle:</strong></p><pre>subprojects {<br>    apply plugin: &quot;org.sonarqube&quot;<br>    sonarqube {<br>        properties {<br>            property &quot;sonar.coverage.jacoco.xmlReportPaths&quot;, &quot;$projectDir.parentFile.path/app/build/reports/jacoco/testDevDebugUnitTestCoverage/testDevDebugUnitTestCoverage.xml&quot;<br>        }<br>    }<br>}</pre><p>As a result root <strong>build.gradle</strong> should be similar to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/590d48d5e6bfa6d8b1421b55f295cc0c/href">https://medium.com/media/590d48d5e6bfa6d8b1421b55f295cc0c/href</a></iframe><p>Here’s the result that we were looking for:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_OjAxhFBWpp9xno7iQjXDg.png" /></figure><p>That’s it, folks! Feel free to reach out to me if you come up with a better approach!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/1*zxwArJYXUgIvcZjNeHEQLQ.gif" /><figcaption>Each clap counts!</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8e7b27aed36" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Curious case with Fragment’s onCreateAnimation()]]></title>
            <link>https://azizbekian.medium.com/curious-case-with-fragments-oncreateanimation-f231367b2502?source=rss-c05a99614dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/f231367b2502</guid>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Andranik Azizbekian]]></dc:creator>
            <pubDate>Sat, 18 Aug 2018 16:18:10 GMT</pubDate>
            <atom:updated>2018-08-18T16:18:10.556Z</atom:updated>
            <content:encoded><![CDATA[<p>What will happen if you override a method from a parent class and within this overriden method simply call super ‘s implementation?</p><p>The overriden method won’t introduce any issues and the behavior of the class won’t get changed — I bet you think just the way as I though.</p><p>You wouldn’t see this post if that was the case. Consider a simple fragment class with following implementation:</p><pre>class MyFragment : Fragment() {<br>  <br>  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {<br>    return inflater.inflate(R.layout.my_fragment, container, false)<br>  }<br>  <br>}</pre><p>Adding this fragment into activity will work as expected. Now consider exactly this fragment, just with one method overriden:</p><pre>class MyFragment : Fragment() {</pre><pre>  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {<br>    return inflater.inflate(R.layout.my_fragment, container, false)<br>  }</pre><pre>  override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation {<br>    return super.onCreateAnimation(transit, enter, nextAnim)<br>  }</pre><pre>}</pre><p>Will this fragment get displayed? Not only it will<strong> not</strong>, but the app will crash 💥</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*bqqw05vRr6e-iYXresLSiw.gif" /></figure><p>What do we have in stacktraces?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y-GlortEiXP51tUQ65f2jA.png" /><figcaption>super.onCreateAnimation(transit, enter, nextAnim) must not be null</figcaption></figure><p>Not super helpful stacktrace, is it?</p><p>At this point you should think <em>“ok, if </em><em>super.onCreateAnimation() must not be null, how the hell did it work back when I haven’t overriden that method??”</em></p><h3>Let me debug this sh**</h3><p>It’s the FragmentManagerImpl which calls fragment.onCreateAnimation(transit, enter, nextAnim) :</p><iframe src="https://cdn.embedly.com/widgets/media.html?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dqy6bsIxRTs4&amp;src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fqy6bsIxRTs4%3Ffeature%3Doembed&amp;type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c3a568439e7a09d2c50a9de62c12e38/href">https://medium.com/media/0c3a568439e7a09d2c50a9de62c12e38/href</a></iframe><p>But, as you can see in the video, the executions jumps to an unknown piece of code after executing super implementation.</p><p>Neither stacktrace, nor debugging gave any valuable information. Are we stuck? In fact I was…for a moment.</p><p>Then I tried to reproduce the issue using a Java code. Guess what? No crash happened, the code just worked.</p><p>So, the problem is with Kotlin code. What might the issue be connected with? We are somehow dealing with null/non-null issue here…</p><h3>A-HA!!</h3><p>Turns out it’s literally one-symbol absence issue. I think you already guessed which one…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/225/1*_JkoAAiYAqJx5aDVEb00YQ.jpeg" /></figure><p>The problem is that Android Studio’s autocompletion generated a code for us, which promised to return an Animation , which in Kotlin language is a promise <strong>not</strong> to be <strong>null</strong>. But, turns out, we violate this promise and return <strong>null</strong> on runtime.</p><p>Simply adding a question mark to the return value (thus making it nullable) will make the code run as expected:</p><pre>override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): <strong>Animation?</strong> {<br>  return super.onCreateAnimation(transit, enter, nextAnim)<br>}</pre><p>It’s a pity, that compiler doesn’t take care of providing a meaningful error message.</p><h3>TL;DR</h3><p>When overriding a method that is declared in Java make sure that parameters are correctly declared in Kotlin part, otherwise you’d end up in an untrackable issue.</p><p>Thanks for reading. Subscribe and happy droding! 🤖</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f231367b2502" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hidden APIs of Android, Part 2— LinearLayout dividers]]></title>
            <link>https://azizbekian.medium.com/hidden-apis-of-android-part-2-linearlayout-dividers-b564064570eb?source=rss-c05a99614dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/b564064570eb</guid>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Andranik Azizbekian]]></dc:creator>
            <pubDate>Sat, 09 Dec 2017 14:37:38 GMT</pubDate>
            <atom:updated>2017-12-12T08:43:50.429Z</atom:updated>
            <content:encoded><![CDATA[<p>In this series I’m going to talk about APIs of Android, that most of developers, despite being quite experienced with Android SDK, do not know about their existence.</p><p>In <a href="https://medium.com/@azizbekian/hidden-apis-of-android-part-1-blink-tag-3f43046c4ae0">part 1</a> we examined &lt;blink&gt; tag. In this episode we are going to dive into a feature of LinearLayout , that I have never seen anybody using.</p><p>Imagine a simple horizontal LinearLayout , wherein 5 fancy squares are shown:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*rmmuI3S0g-h9Fapy9QOfhw.png" /></figure><p>Now the requirement is to introduce a divider between these 5 views like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*ymx1AtmP3n9XQnBe3FJxSQ.png" /></figure><p>How would you solve the problem?</p><p>Would you introduce a new <em>divider</em> view between each of these squares?</p><pre>&lt;LinearLayout ...&gt;</pre><pre>    &lt;View .../&gt; <br>    &lt;include layout=&quot;<a href="http://twitter.com/layout/divider">@layout/divider</a>&quot;/&gt;<br>    &lt;View .../&gt; <br>    &lt;include layout=&quot;<a href="http://twitter.com/layout/divider">@layout/divider</a>&quot;/&gt;<br>    &lt;View .../&gt;<br>    &lt;include layout=&quot;<a href="http://twitter.com/layout/divider">@layout/divider</a>&quot;/&gt;<br>    &lt;View .../&gt;<br>    &lt;include layout=&quot;<a href="http://twitter.com/layout/divider">@layout/divider</a>&quot;/&gt;<br>    &lt;View .../&gt;</pre><pre>&lt;/LinearLayout&gt;</pre><p>Sure, this would solve the problem. But turns out that LinearLayout internally possesses such a feature. Please welcome <a href="https://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:divider">android:divider</a> tag:</p><pre>&lt;LinearLayout<br>    ...<br>    android:divider=&quot;<a href="http://twitter.com/drawable/divider">@drawable/divider</a>&quot;<br>    android:showDividers=&quot;middle&quot;&gt;<br> <br>    &lt;View .../&gt;<br>    &lt;View .../&gt;<br>    &lt;View .../&gt;<br>    &lt;View .../&gt;<br>    &lt;View .../&gt;</pre><pre>&lt;/LinearLayout&gt;</pre><p>Applying android:divider is not yet necessary - android:showDividers should also be set, otherwise it defaults to none . There are four options in total: middle , beginning , end and none .</p><p>As you have already guessed, beginning and end would apply divider only to beginning and and respectively.</p><p>U̶n̶f̶o̶r̶t̶u̶n̶a̶t̶e̶l̶y̶,̶ ̶t̶h̶e̶r̶e̶ ̶d̶o̶e̶s̶ ̶n̶o̶t̶ ̶e̶x̶i̶s̶t̶ ̶a̶ ̶w̶a̶y̶ ̶t̶o̶ ̶a̶p̶p̶l̶y̶ ̶d̶i̶v̶i̶d̶e̶r̶ ̶f̶o̶r̶ ̶b̶e̶g̶i̶n̶n̶i̶n̶g̶,̶ ̶m̶i̶d̶d̶l̶e̶ ̶a̶n̶d̶ ̶e̶n̶d̶ ̶s̶i̶m̶u̶l̶t̶a̶n̶e̶o̶u̶s̶l̶y̶,̶ ̶w̶h̶i̶c̶h̶ ̶i̶s̶ ̶a̶ ̶b̶i̶t̶ ̶w̶e̶i̶r̶d̶.̶ ̶</p><p>As <a href="https://medium.com/@aristides.papadopoulos/doesnt-android-showdividers-middle-begin-end-4f452467fdc">pointed out</a> by Aris Papadopoulos 👏, you can apply dividers for both beginning, middle and end just by bitwise OR-ing the flags: android:showDividers=&quot;beginning|middle|end&quot;.</p><p>Happy droiding! 🤖</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b564064570eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hidden APIs of Android, Part 1—<blink> tag]]></title>
            <link>https://azizbekian.medium.com/hidden-apis-of-android-part-1-blink-tag-3f43046c4ae0?source=rss-c05a99614dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f43046c4ae0</guid>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Andranik Azizbekian]]></dc:creator>
            <pubDate>Sat, 25 Nov 2017 08:00:05 GMT</pubDate>
            <atom:updated>2017-11-25T08:42:59.157Z</atom:updated>
            <content:encoded><![CDATA[<p>In this series I’m going to talk about APIs of Android, that most of developers, despite being quite experienced with Android SDK, do not know about their existence.</p><p>Image, that for some weird reason you have got a specification to implement such a feature:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*dQt4Bwu71gXWwHfaNK9ysQ.gif" /><figcaption><a href="https://github.com/googlesamples/android-topeka">Topeka app</a></figcaption></figure><p>Basically, you have to blink your view hierarchy indeterminately.</p><p>How are you going to achieve this?</p><p>Sure there are plenty of ways to implement it. But turns out there exists a specific API for this use-case. It’s the &lt;blink&gt; tag that will do the magic for you. All you have to do is to wrap your view hierarchy with that tag:</p><pre>&lt;blink android:layout_width=&quot;match_parent&quot;<br>       android:layout_height=&quot;match_parent&quot;&gt;<br>   <br>   &lt;FrameLayout&gt;<br>    ...<br>   &lt;/FrameLayout&gt;</pre><pre>&lt;/blink&gt;</pre><p>That’s it, this will make your view hierarchy to “<em>party like it’s 1995!</em>”. Wonder where is the quote taken from? Perfect time to dive into sources.</p><p>As you know, it’s the LayoutInflater that will create View out of the xml resource id that is provided as an input parameter to LayoutInflater. As soon as LayoutInflater will start to parse the tags of the xml, it will eventually end up calling <a href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/LayoutInflater.java#748">createViewFromTag()</a> :</p><pre>View createViewFromTag(View parent, String name, ...) {<br>    ...<br>    if (name.equals(TAG_1995)) {<br>        // Let&#39;s party like it&#39;s 1995!<br>        return new BlinkLayout(context, attrs);<br>    }<br>    ...<br>}</pre><p>Here you can see how the magic happens. If name is equal to blink , then a BlinkLayout would be created, which is a private static class declared in LayoutInflater . <a href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/LayoutInflater.java#1040">Here</a>’s the full implementation of BlinkLayout, which is quite easy to understand.</p><p>Note: there is no way to control blink in/out timing, it’s 500ms by default.</p><p>Clap if you liked the post. Subscribe to get updated with the next episodes of these series.</p><p>Happy droiding! 🤖</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f43046c4ae0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RxJava: listen for events after error]]></title>
            <link>https://azizbekian.medium.com/rxjava-listen-for-events-after-error-ce18f8d4858d?source=rss-c05a99614dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce18f8d4858d</guid>
            <category><![CDATA[rxjava]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Andranik Azizbekian]]></dc:creator>
            <pubDate>Sat, 18 Mar 2017 12:05:14 GMT</pubDate>
            <atom:updated>2017-03-18T12:05:14.172Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dq_xFW9zlDA6B1kgy7t5KQ.png" /></figure><p>I am not fond of long and wordy articles, thus I’ll try to be concise.</p><h3><strong>Problem</strong></h3><p>Imagine situation, when it is acceptable for the stream to include “error” events, and you want to continue listening to events regardless it is error or no, i.e. you do not want the stream to be interrupted.</p><p>As you know, the stream will be interrupted after once <em>onError()</em> has been called on a stream.</p><pre>PublishSubject&lt;String&gt; publishSubject = PublishSubject.<em>create</em>();<br><br>publishSubject.subscribe(System.<em>out</em>::println, System.<em>out</em>::println);<br><br>publishSubject.onNext(&quot;1&quot;);<br>publishSubject.onError(new IllegalArgumentException(&quot;err1&quot;));<br>publishSubject.onNext(&quot;2&quot;);</pre><p>This will result in the following output:</p><pre>1<br>java.lang.IllegalArgumentException: err1</pre><p>Note, “2” is not present in the output, because stream is interrupted as soon as <em>onError()</em> is called.</p><h3>Solution</h3><p>Rx comes with a <a href="https://github.com/ReactiveX/RxJava/blob/2.x/src/main/java/io/reactivex/Notification.java"><strong>Notification</strong></a><strong> </strong>wrapper class which can be helpful for this case.</p><pre>PublishSubject&lt;Notification&lt;String&gt;&gt; publishSubject = PublishSubject.<em>create</em>();<br><br>publishSubject.subscribe(stringNotification -&gt; {<br>    if(stringNotification.isOnError()) {<br>        System.<em>out</em>.println(stringNotification.getError());<br>    } else {<br>        System.<em>out</em>.println(stringNotification.getValue());<br>    }<br>});<br><br>publishSubject.onNext(Notification.<em>createOnNext</em>(&quot;1&quot;));<br>publishSubject.onNext(Notification.<em>createOnError</em>(new IllegalArgumentException(&quot;err1&quot;)));<br>publishSubject.onNext(Notification.<em>createOnNext</em>(&quot;2&quot;));</pre><p>The output is following:</p><pre>1<br>java.lang.IllegalArgumentException: err1<br>2</pre><p>Pay attention, there is “2” after an error event.</p><p>Thanks for reading! Bookmark, because you’ll need this one day!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce18f8d4858d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[WindowInsets??]]></title>
            <link>https://azizbekian.medium.com/windowinsets-24e241d4afb9?source=rss-c05a99614dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/24e241d4afb9</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Andranik Azizbekian]]></dc:creator>
            <pubDate>Sat, 21 Jan 2017 09:12:46 GMT</pubDate>
            <atom:updated>2017-11-25T12:44:37.847Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sLOrZrMkSjwxvOQF4owrqQ.jpeg" /></figure><p>Have you ever wondered what are <a href="https://developer.android.com/reference/android/view/WindowInsets.html">WindowInsets</a> used for? Why would you ever need them? You have seen people applying <a href="https://developer.android.com/reference/android/view/View.html#fitSystemWindows%28android.graphics.Rect%29">android:fitsSystemWindows=”true”</a> to their layouts but you actually see no difference sometimes? What an API is this? We’ll try to demystify them in this article.</p><p>WindowInsets are insets (or sizes) of system views (e.g. status bar, navigation bar), that are applied to the window of your application.</p><p>The best way to understand something — is to see it in example. Let’s imagine you have requirement to achieve this UI:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YL-vKczyfLhYRdrxDeNSlA.png" /></figure><p>How would I do that?</p><p>Well, there is aToolbar and an ImageView in this layout. I’d wrap them in FrameLayout and that’s it, right? Not yet. What I will get there is that theToolbar would be stretched by status bar, i.e. Toolbar will not have necessary top padding in order to be laid out below status bar.</p><p>What if I apply <strong>android:fitsSystemWindows=”true” </strong>to the FrameLayout ?</p><p>First, let’s understand what this flag is going to do. When a layout has this flag, it means that this layout is asking the system to be applied window insets. In our case that would mean, that FrameLayout desires to be applied a padding that is equal to status bar’s height.</p><p>Well, that’s nice. Is seems like a good idea, that will solve our problem. Actually it won’t, because now the ImageView will also receive this padding, and will be laid out below the status bar.</p><p>Luckily, there’s a nice API, that solves our problem — <a href="https://developer.android.com/reference/android/support/v4/view/ViewCompat.html#setOnApplyWindowInsetsListener(android.view.View,%20android.support.v4.view.OnApplyWindowInsetsListener)">ViewCompat.setOnApplyWindowInsetListener</a> . With this API you can apply window insets to a particular view.</p><pre>ViewCompat.<em>setOnApplyWindowInsetsListener</em>(toolbar, (v, insets) -&gt; {<br>    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();<br>    params.topMargin = insets.getSystemWindowInsetTop();<br>    return insets.consumeSystemWindowInsets();<br>});</pre><p><strong>But</strong> insets.getSystemWindowInsetTop() would return <strong>0 </strong>in our case, because, remember, we have wrapped our view in FrameLayout .</p><p>Standard layouts like FrameLayout , LinearLayout or RelativeLayout will not pass window insets to their children, whereas “materialish” layouts will do (e.g. DrawerLayout , CoordinatorLayout ). So we may change our layout to “materialish” one, or we can subclass a standard layout and pass the insets to the children of layout ourselves. We just have to override onApplyWindowInsets method.</p><pre>@TargetApi(Build.VERSION_CODES.<em>KITKAT_WATCH</em>)<br>@Override<br>public WindowInsets onApplyWindowInsets(WindowInsets insets) {<br>    int childCount = getChildCount();<br>    for (int index = 0; index &lt; childCount; ++index)<br>        getChildAt(index).dispatchApplyWindowInsets(insets); <br>        // let children know about WindowInsets<br><br>    return insets;<br>}</pre><p>That’s it. Now we have the UI we wanted to achieve.</p><p>You may also read an <a href="https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec">article</a> by Ian Lake and <a href="https://www.youtube.com/watch?v=_mGDMVRO3iE">a presentation</a> from Chris Banes concerning window insets.</p><p>Thanks for reading. Happy droiding! 🤖</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24e241d4afb9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>