<?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 Sidhant Rajora on Medium]]></title>
        <description><![CDATA[Stories by Sidhant Rajora on Medium]]></description>
        <link>https://medium.com/@sidhantrajora?source=rss-75d44ff863ac------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*cUSj0I4l-CSqr-Zq.</url>
            <title>Stories by Sidhant Rajora on Medium</title>
            <link>https://medium.com/@sidhantrajora?source=rss-75d44ff863ac------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 10:34:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sidhantrajora/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[KMP String Resources]]></title>
            <link>https://medium.com/@sidhantrajora/kmp-string-resources-627f42165bbb?source=rss-75d44ff863ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/627f42165bbb</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[kotlin-multiplatform]]></category>
            <category><![CDATA[localization]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Sidhant Rajora]]></dc:creator>
            <pubDate>Thu, 07 Mar 2024 19:37:35 GMT</pubDate>
            <atom:updated>2024-03-07T19:38:45.324Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RtQ6g1YedG9I8ia8" /><figcaption>Photo by <a href="https://unsplash.com/@charlie_wollborg?utm_source=medium&amp;utm_medium=referral">Charlie Wollborg</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>With recent changes to <a href="https://github.com/JetBrains/compose-multiplatform">compose-multiplatform</a> the team introduced a few new capabilities to handle the resources like, drawables, strings (values), files and fonts.</p><p>This blog will elaborate on strings and how to have localized strings for UI component based on device language</p><p>So once we create a new project using <a href="https://kmp.jetbrains.com/">https://kmp.jetbrains.com/</a> it will come with default project structure, and it will have<br>projectName/composeApp/src/commonMain/composeResources this folder for now and it will have drawable folder with one vector resource, to proceed further lets create a new folder named values <br>This folder will contain the corresponding default local strings for our project, for this instance lets consider this to be English and will have the following values</p><pre>&lt;resources&gt;<br>    &lt;string name=&quot;button_title&quot;&gt;Hello&lt;/string&gt;<br>    &lt;string name=&quot;lable_with_name&quot;&gt;Hello %1$s&lt;/string&gt;<br>    <br>    &lt;string name=&quot;greet_two_folks&quot;&gt;Hello %1$s and %2$s&lt;/string&gt;<br>    <br>    &lt;string-array name=&quot;item_list&quot;&gt;<br>        &lt;item&gt;item 1&lt;/item&gt;<br>        &lt;item&gt;item 2&lt;/item&gt;<br>        &lt;item&gt;item 3&lt;/item&gt;<br>    &lt;/string-array&gt;<br>&lt;/resources&gt;</pre><p>and once we use these strings values in the compose multiplatform app, using stringResource or stringArrayResource like below</p><pre>@Composable<br>fun ColumnList(){<br>  Column {<br>    Button(onClick = {  }) {<br>                    Text(stringResource(Res.string.button_title))<br>                }        <br>    Text(stringResource(Res.string.lable_with_name, &quot;Author&quot;))<br>    Text(stringResource(Res.string.greet_two_folks, &quot;Author1&quot;, &quot;Author2&quot;))<br>    Text(stringArrayResource(Res.string.item_list).joinToString(&quot;,&quot;)) <br>  }<br>}</pre><p>and here the Res comes from generated files in the build folder, this Generated files gets generated using a gradle task namely generateComposeResClass if you want to look at the task code here is the link to the <a href="https://github.com/JetBrains/compose-multiplatform/blob/master/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt">task</a> as of today Strings resource that this task can generate are limited to Array and Value types, which can be seen <a href="https://github.com/JetBrains/compose-multiplatform/blob/master/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt">here</a> .<br>Adding a snippet here for brevity</p><pre>private sealed interface StringItem {<br>    data class Value(val text: String) : StringItem<br>    data class Array(val items: List&lt;String&gt;) : StringItem<br>}</pre><p>Res will likely come from an import that looks something like</p><p>import &lt;package&gt;.generated.resources.Res</p><p>For formatted strings like &lt;string name=”greet_two_folks”&gt;Hello %1$s and %2$s&lt;/string&gt; additional arguments can be passed to stringResource and the formatted string with supplied argument will be retuned and can be displayed on the screen like stringResource(Res.string.greet_two_folks, “Author1”, “Author2”))</p><p>In the world of android, we do have another thing called quantity strings, which are generated using an another tag called plurals and can gives us quantified strings, like 1 Item or 3 Items based on some quantity, which is currently not supported and can be done using if else check for now, on the quantities and respective string values can be used from the string.xml</p><h3>How to add more supported languages?</h3><p>To add more strings for all the language the app supports, we just need to make another values folder with LanguageQualifier like for Spanish the folder will look like values-es and for Hindi it will look like values-hi</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*igGShrBf2YOr7Vh1eDoYJA.png" /></figure><p>and all of those folder will have the same strings.xml file with same xml nodes and keys, but different values like for Spanish it will have these values</p><pre>&lt;resources&gt;<br>    &lt;string name=&quot;button_title&quot;&gt;Hola&lt;/string&gt;<br>    &lt;string name=&quot;lable_with_name&quot;&gt;Hola %1$s&lt;/string&gt;<br>    <br>    &lt;string name=&quot;greet_two_folks&quot;&gt;Hola %1$s, %2$s&lt;/string&gt;<br>    <br>    &lt;string-array name=&quot;item_list&quot;&gt;<br>        &lt;item&gt;artículo 1&lt;/item&gt;<br>        &lt;item&gt;artículo 2&lt;/item&gt;<br>        &lt;item&gt;artículo 3&lt;/item&gt;<br>    &lt;/string-array&gt;<br>&lt;/resources&gt;</pre><p>and now if you run the app you should see English as default and from setting if you change the language to Spanish it should display the new Spanish values, respectively it should do the same for Hindi as well.</p><p>In these string values it does supports unicode, newline, escape characters for example</p><p>‘\n’ becomes new line</p><p>‘\t’ becomes tab</p><p>‘\uXXXX’ becomes unicode symbol</p><p>As discussed above, how these values are accessed, from a generated file Res which resides in the build folder, it looks something like below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kCT-CTxuk5em15RGXSAwRw.png" /></figure><p>This is how the UI looks with localized string resource on iOS, for Spanish and English</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bvPOFfSY2tP-crIqLur4eA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9G2ChbIS3HjJ31bhicOccg.png" /><figcaption>Spanish and English</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=627f42165bbb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Doc: 101 Sorted/Sort]]></title>
            <link>https://medium.com/@sidhantrajora/java-doc-101-sorted-sort-2b6baf27d99a?source=rss-75d44ff863ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b6baf27d99a</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <dc:creator><![CDATA[Sidhant Rajora]]></dc:creator>
            <pubDate>Mon, 19 Feb 2024 19:09:42 GMT</pubDate>
            <atom:updated>2024-02-19T19:09:42.912Z</atom:updated>
            <content:encoded><![CDATA[<p>A series on java doc, let get started with sort and what brings on the table and how its not just about merge sort or any of the algorithms, and how internally many design decision were made to incorporate the sorting in multiple ways.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66b55bd748048cf1f29287c90f0b4ca4/href">https://medium.com/media/66b55bd748048cf1f29287c90f0b4ca4/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b6baf27d99a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Interesting case of ViewTree*Owner]]></title>
            <link>https://medium.com/mindorks/the-interesting-case-of-viewtree-owner-87da772519c4?source=rss-75d44ff863ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/87da772519c4</guid>
            <category><![CDATA[compose]]></category>
            <category><![CDATA[compose-ui]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Sidhant Rajora]]></dc:creator>
            <pubDate>Thu, 21 Oct 2021 10:42:38 GMT</pubDate>
            <atom:updated>2021-10-21T10:48:16.555Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VP53TykQ4JitcUXV7osLxg.jpeg" /></figure><p>So, while building a sample app, I was famished by a very sketchy exception which is here down below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd7d92802cb92e7773689d7a5920ec1a/href">https://medium.com/media/cd7d92802cb92e7773689d7a5920ec1a/href</a></iframe><p>I was bamboozled when I saw this error, as this was happening only, when I was inflating a ComposeView inside a fragment, if instead I inflate a layout using LayoutInflaterreturn it on onCreateView() the code was running as expected. I looked around on StackOverflow, issuetracker and few blogs, and they all suggested to update the appcompat to 1.3.0 or later, but I was already on 1.3.1 so, needed to figure out what went wrong where?</p><h3>The Origin</h3><p>So I started with a simple MainActivity and a project with viewBinding enabled, here is the snippet</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e3e28314a3399954bd136ad4489a876/href">https://medium.com/media/5e3e28314a3399954bd136ad4489a876/href</a></iframe><p>So In here you can see, that I am using SampleBinding.inflate() and attachToRoot set to true, that basically attaches this layout to the window root, and shows it to the user, and I began my fragmentTransaction which tries to inflate a DemoFragment which in turn inflates a ComposeView like below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35c97eb56c96e0a9d9e29012fe5dd340/href">https://medium.com/media/35c97eb56c96e0a9d9e29012fe5dd340/href</a></iframe><p>and when I ran it, it crashed with the above stack trace.</p><blockquote>After looking into the ComposeView and AbstractComposeView , I came across a few code snippets which made me realize the obvious mistake that I was committing.</blockquote><h3>The Dig</h3><p>So basically to lay out Composables we need a Recomposer which is just a Scheduler “<em>for performing recomposition and applying updates to one or more Composition” </em>this Recomposer which needs to know to what lifecycle this needs to be binded to, this is resolved via a few way which can be seen in the below snippet from ComposeView.android.kt</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/566743c3ae9a6fe0af4d5fc575789aaa/href">https://medium.com/media/566743c3ae9a6fe0af4d5fc575789aaa/href</a></iframe><p>this is all to create a LifecycleAwareViewTreeRecomposer for obvious reasons. So at this point I knew that I messed up somewhere, and needed to know where, so I tried calling</p><pre>ViewTreeLifecycleOwner.set(requireActivity().<em>window</em>.<em>decorView</em>, this)</pre><p>before returning the ComposeView in the DemoFragment and it worked as expected.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b217c5675a184b0d4659a8bf0cef48a3/href">https://medium.com/media/b217c5675a184b0d4659a8bf0cef48a3/href</a></iframe><p>but now to the main point, this can’t be the solution as I am going to do this manually for each and every fragment and that’s not the best solution.</p><h3>The Solve</h3><p>So, basically the whole concept of ViewTree*Owner was introduced recently, and there are three ViewTreeLifecycleOwner, ViewTreeViewModelStoreOwner and ViewTreeSavedStateRegistryOwner and there name are self explanatory about what they are owner of, and in the latest source of appcompat to be specific AppCompatActivity you see that we have setContnetView() method which inturn calls initViewTreeOwners() and that does the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e276990c59dd2640fef8e0b23dd66ce6/href">https://medium.com/media/e276990c59dd2640fef8e0b23dd66ce6/href</a></iframe><p>it sets all of them. So if you go back to MainActivity you can see that I never called in the setContentView() with binding.root as parameter to it, thus skipping the call to initViewTreeOwners() thus the exception.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01b8d662d6844bfcdd1fb00da2c6e9ab/href">https://medium.com/media/01b8d662d6844bfcdd1fb00da2c6e9ab/href</a></iframe><p>Few links to reference the AppCompatActivity , ComponentActivity and all are below.</p><ul><li><a href="https://androidx.tech/artifacts/appcompat/appcompat/1.3.1-source/androidx/appcompat/app/AppCompatActivity.java.html">AppCompatActivity.java</a></li><li><a href="https://android.googlesource.com/platform/frameworks/support/+/b3e8b51/lifecycle/lifecycle-runtime/src/main/java/androidx/lifecycle/ViewTreeLifecycleOwner.java">lifecycle/lifecycle-runtime/src/main/java/androidx/lifecycle/ViewTreeLifecycleOwner.java - platform/frameworks/support - Git at Google</a></li><li><a href="https://android.googlesource.com/platform/frameworks/support/+/47fd275/activity/activity/src/main/java/androidx/activity/ComponentActivity.java">activity/activity/src/main/java/androidx/activity/ComponentActivity.java - platform/frameworks/support - Git at Google</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87da772519c4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mindorks/the-interesting-case-of-viewtree-owner-87da772519c4">The Interesting case of ViewTree*Owner</a> was originally published in <a href="https://medium.com/mindorks">MindOrks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AlertDialog JetPack Compose]]></title>
            <link>https://medium.com/mindorks/alertdialog-jetpack-compose-f16887f0d1c4?source=rss-75d44ff863ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/f16887f0d1c4</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[google]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[declarative-programming]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Sidhant Rajora]]></dc:creator>
            <pubDate>Sun, 10 Nov 2019 10:32:59 GMT</pubDate>
            <atom:updated>2019-11-14T18:43:01.989Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*LqvDde2A4Gg6O9V1jTaUhA.jpeg" /><figcaption>source : <a href="https://ourpastimes.com/">https://ourpastimes.com</a></figcaption></figure><blockquote><strong>Let’s compose AlertDialog using/in Jetpack compose.</strong></blockquote><p>If you are not aware of Jetpack compose and what it is? then just for the sake of making this tutorial understandable lets first begin with Jetpack compose itself.</p><p><strong>What is Jetpack Compose?</strong></p><p>According to <a href="https://developer.android.com/jetpack/compose"><em>Compose</em></a><em> </em>it is “Android’s modern toolkit for building native UI”, which in turn is created to help developers dive in <a href="https://www.youtube.com/watch?v=VsStyq4Lzxo">Declarative UI Pattern</a>(you can click on it, and watch an introductory video on this UI pattern), if you familiar with <a href="https://flutter.dev/">flutter</a>, <a href="https://developer.apple.com/xcode/swiftui/">swiftUI</a>, or <a href="https://facebook.github.io/react-native/">React Native</a> then it’s quiet easy for you grasp and come along.</p><blockquote>The idea behind the whole compose thing is to make the UI a first class member, instead of making it dependent on other files such as xml(android),xib(iOS, basically it is also XML) which is imperative style of designing, in which we create the full-functioned UI entity and then hook those components to our business logic via outlets(in iOS), or by using findViewById&lt;&gt;() (in Android) and then mutate them using property getters and setters.</blockquote><p>let’s begin with what we intend to learn today.</p><p>In order to begin with, lets clear the requirements first</p><ol><li>Android Studio 4.0 canary 1/2(Preview Version)</li></ol><p>Now, lets create a new project and choose “Empty Compose Activity”, it will just basically automate the boilerplate of adding the gradle libraries such as</p><pre>implementation &#39;androidx.ui:ui-layout:0.1.0-dev02&#39;<br>implementation &#39;androidx.ui:ui-material:0.1.0-dev02&#39;<br>implementation &#39;androidx.ui:ui-tooling:0.1.0-dev02&#39;</pre><p>so that you don’t have to manually figure those out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*UqwiDN7JgE9FBNO2wRQ9QA.png" /></figure><p>and then enter your project details and target platform<strong><em>(minimum will be 21) </em></strong>and let the project creation wizard create the project and sync the project with gradle, and now you see a MainActivity file with the code like below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c84e118822b8549a505d22ccd07feaf/href">https://medium.com/media/5c84e118822b8549a505d22ccd07feaf/href</a></iframe><p>in here, you see something different setContentView() is no more here and instead we have setContent{} which in turn have the MaterialTheme and a child Composable method which renders the Text()</p><blockquote><strong>What is composable method?</strong></blockquote><blockquote>Composable( written with @Composable annotation), method is just a sugar method which just help you to organise the code in better fashion and reduce the vertical hight of the renderer setContent{} method</blockquote><p>And from the code itself:</p><pre><em>Composable functions are the fundamental building blocks of an application built with Compose.<br>Composable can be applied to a function or lambda to indicate that the function/lambda can be used as part of a composition to describe a transformation from application data into a tree or hierarchy.</em></pre><p>and when you run this app, you see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/284/1*joRmKelg-4neM76L-FXwLQ.png" /><figcaption>screenshot</figcaption></figure><p>now, let’s add a way to show a dialog, for that we will need a button, let clear the code from the setContent{} and add our own code for button</p><pre><em>Button</em>(&quot;Show Alert&quot;,<br>        onClick = <strong>{<br>            <br>        }<br></strong>)</pre><p>we just have to add in this code in order to show a button on the UI but as this is not aware of the constraints about it’s height or width it will just fill in the complete view, so we will put it into Column widget</p><pre><em>Column </em><strong>{<br>    </strong><em>Button</em>(&quot;Show Alert&quot;,<br>            onClick = <strong>{<br><br>            }<br>    </strong>)<br><strong>}</strong></pre><p>now you might be wondering why column? Then by the official documentation</p><blockquote><strong>Column</strong> is a composable that places its children in a vertical sequence and is able to assign them heights</blockquote><p>now we have to add in the alert dialog as well by using the AlertDialog()</p><pre><em>AlertDialog</em>(<br>    <strong>// This is responsible for handling the close requests, this acts as setCancelable(true/false)<br>    // here you can mutate the state if you want to dissmiss the dialog<br>    // or do nothing if you don&#39;t want it to be dismissed if user clicks<br>    // outside the alert dialog</strong><br>    onCloseRequest = <strong>{<br>        </strong><em>showDialog</em>.value = false<br>    <strong>}</strong>,<br>    title = <strong>{<br>        </strong><em>Text</em>(text = &quot;Alert Dialog&quot;)<br>    <strong>}</strong>,<br>    text = <strong>{<br>        </strong><em>Text</em>(&quot;JetPack Compose Alert Dialog!&quot;)<br>    <strong>}</strong>,<br>    confirmButton = <strong>{<br>        </strong><em>Button</em>(&quot;Confirm&quot;, onClick = <strong>{<br> </strong>       <strong>}</strong>)<br>    <strong>}<br></strong>)</pre><p>and the code after adding in both composable the code looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b57a3e112622244e15cfc7af264cad9b/href">https://medium.com/media/b57a3e112622244e15cfc7af264cad9b/href</a></iframe><p>and if you run the app now, you will see both, button and the alert dialog.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/286/1*BsxX5jnJyLUH-ypNtymhJg.png" /><figcaption>button and alert dialog</figcaption></figure><p>so in the code, we created in the two composable methods one for adding in the button and one for alert dialog, now in order to mutate the states of the composables or the widgets we need to make them <strong><em>state</em></strong> dependent.</p><p>Let’s do it.</p><pre>val showDialog  = +<em>state</em><strong>{<br>    </strong>false<br><strong>}<br>// Here I have created a variable of type State&lt;Boolean&gt; showDialog with default value false</strong></pre><p>and will use this state variable to show/hide the dialog</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbc5ca60b7e03b51ed6d0889287844d9/href">https://medium.com/media/dbc5ca60b7e03b51ed6d0889287844d9/href</a></iframe><p>now run the app and now you only can see the button only, and on pressing the button the button you can see the alert dialog, now lets hook in the logic to dismiss that dialog button, on pressing the “<strong><em>confirm</em></strong>” button.</p><p>This is how the final result looks like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*QJBq6Q_gMitGcB5Zzeju-g.gif" /><figcaption>final destiny</figcaption></figure><p>and the final code for the same is</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4237ef7a2d36f6c6a3611bbd6e74f55/href">https://medium.com/media/c4237ef7a2d36f6c6a3611bbd6e74f55/href</a></iframe><p>Learn more about <strong>Jetpack Compose </strong>at</p><p><a href="https://developer.android.com/jetpack/compose">Jetpack Compose UI App Development Toolkit - Android Developers</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f16887f0d1c4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mindorks/alertdialog-jetpack-compose-f16887f0d1c4">AlertDialog JetPack Compose</a> was originally published in <a href="https://medium.com/mindorks">MindOrks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making ViewModel Observable]]></title>
            <link>https://medium.com/mindorks/making-viewmodel-observable-6a5398fdb0c8?source=rss-75d44ff863ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a5398fdb0c8</guid>
            <category><![CDATA[viewmodel]]></category>
            <category><![CDATA[androidviewmodel]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[data-binding]]></category>
            <category><![CDATA[android-data-binding]]></category>
            <dc:creator><![CDATA[Sidhant Rajora]]></dc:creator>
            <pubDate>Mon, 28 Jan 2019 10:50:27 GMT</pubDate>
            <atom:updated>2019-01-28T17:53:20.614Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VBdKpaowaEdI_54fSwZOfw.jpeg" /></figure><p>While making android applications, we tend to follow some kind of design pattern or try to architect our application using some of the common patterns like MVVM, MVP, MVC or any other to simply the code readability and make the code more testable.</p><p>MVVM is the one that makes a good effort in making out business logic completely testable in isolation. When I started using it, initially I thought that it’s like a simple model, which can be binded with the UI using data binding. But as I started to dig in more, I came to know that it can do more then that, like it can survive the configuration changes if we use the “<a href="https://developer.android.com/reference/android/arch/lifecycle/AndroidViewModel"><strong><em>AndroidViewModel</em></strong></a>” and provide the view model binding using the “<a href="https://developer.android.com/reference/android/arch/lifecycle/ViewModelProviders"><strong><em>ViewModelProviders.of(this)</em></strong></a><strong><em>”</em></strong>.</p><p>But prior to moving on that, we need to understand that whenever we change the data of <strong>ViewModel</strong>, it gets reflected to the UI components with whihc our ViewModel binded to, and all happens because of the class <strong>BaseObservable </strong>that we tend to use with the normal <strong>ViewModel</strong>(not the <strong>AndroidViewModel</strong>) by calling in the method <strong>notifyChange()</strong> and as in Java/Kotlin we only can extend from one class, we either can use the <strong>AndroidViewModel</strong>, or the <strong>BaseObservable</strong>. We cannot use both of them together</p><p>The best solution is to look into <strong>BaseObservable </strong>and try to understand, that what it does, in order to notify changes to UI whenever the <strong>ViewModel </strong>Data is changed and If you go in there and you will see that it implements an interface i.e, <strong>Observable</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62777780ba536c8caa65d0a7f651d546/href">https://medium.com/media/62777780ba536c8caa65d0a7f651d546/href</a></iframe><p>and if you go into the <strong>Observable </strong>interface you will see that there are two method declarations and one abstract class is declared in it which has onPropertyChanged which is called by an Observable whenever an observable property changes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2e65ff35c3ef20410b717c6232d2748/href">https://medium.com/media/c2e65ff35c3ef20410b717c6232d2748/href</a></iframe><p>Now we understand that how <strong>BaseObservable </strong>utilizes the <strong>Observable </strong>interface and uses <strong>PropertyChangeRegistry </strong>which in the end helps in notifying the changes to the UI and load in the ViewModel.</p><h3><strong>What are we waiting for?</strong></h3><p>we just need to make a custom ViewModel class lets name it <strong>ObservableViewModel </strong>and extend from the <strong>AndroidViewModel </strong>and implement the <strong>Observable </strong>interface, and then implement all the methods of it and that will be all.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ba54041c7e112ae5c212f7366dd298c/href">https://medium.com/media/2ba54041c7e112ae5c212f7366dd298c/href</a></iframe><p>now the last thing that we need to do is to extend this class instead of AndroidViewModel and call in the notifyChange() if you want to update all the views, or call in the notifyChange(viewId) just to update a specific element whenever you want.</p><blockquote>In method notifyChange() on line 20, in its body we are calling mCallBacks.notifyChange(this, 0), here 0 means update every property.</blockquote><p><strong><em>If you liked this article, don’t forget to clap, comment on it and share with your friends.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a5398fdb0c8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mindorks/making-viewmodel-observable-6a5398fdb0c8">Making ViewModel Observable</a> was originally published in <a href="https://medium.com/mindorks">MindOrks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Better Logging In Android Using Timber]]></title>
            <link>https://medium.com/mindorks/better-logging-in-android-using-timber-72e40cc2293d?source=rss-75d44ff863ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/72e40cc2293d</guid>
            <category><![CDATA[android-studio]]></category>
            <category><![CDATA[proguard]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Sidhant Rajora]]></dc:creator>
            <pubDate>Thu, 15 Nov 2018 09:27:21 GMT</pubDate>
            <atom:updated>2019-10-31T03:45:36.541Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*8WF5GxCHrCpHIGgWg5fgsw.jpeg" /><figcaption>logs logs logs…</figcaption></figure><p>While developing android applications we as developers tend to put in a lot of logs with in different different priorities for this sake android sdk comes in with a utility class <a href="https://developer.android.com/reference/android/util/Log">Log</a>, which does have utility methods to log our messages with different priorities the most common methods that we use for different cases like we uses Log.e(TAG,”message”) in the cases we want to show some error (this usually appears in red color in the logcat window of android studio), or Log.d(TAG, &quot;message&quot;) when we want to print some message for the purpose of debugging, like value of some data.</p><p>Usually when the application development is completed and its the time to release the app on the play store, we need to remove all <strong>Log</strong> statement from the app, so that none of the application data such as <em>user information, hidden application data, auth-tokens </em>are<em> </em>available to user in logcat as plain text and this becomes a cumbersome task</p><p>There are several ways to tackle this problem</p><h3>1. Condition based logging</h3><p>In this we can make a public static boolean variable in Application class like in the below code snippet.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1ce3247eba853874d93d24a04bbcd5c/href">https://medium.com/media/b1ce3247eba853874d93d24a04bbcd5c/href</a></iframe><p>and use this variables value as check for logging like the code snippet below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81a82cf7bb73cfc7bdf9a79dad1d4086/href">https://medium.com/media/81a82cf7bb73cfc7bdf9a79dad1d4086/href</a></iframe><p>and use this boolean variable at every place where you want to put a log message, and once you are ready to release the app, just set isDebug = false in the application class, but this approach puts in an extra line of code, which is completely unnecessary and can be avoided.</p><p>This simply can be achieved using android’s in built build configuration, BuildConfig.<em>DEBUG </em>can be used instead of that boolean variable for checking if the build type is release or debug.</p><h3><strong>2. ProGuard</strong></h3><p>This approach is all also a solution to this problem, you can render the Log class ineffective and can remove those statement in the release build by putting in</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3943130b8d0f51d42596b6dd33620e81/href">https://medium.com/media/3943130b8d0f51d42596b6dd33620e81/href</a></iframe><p>into the proguard rules file.</p><h3><strong>Here comes Timber, the best solution to tackle this problem</strong></h3><p>What if I tell you there exists a simple library called “<a href="https://github.com/JakeWharton/timber">Timber</a>” which can log your messages and gives you the control over the flow of logs. Timber is a yet another cool library developed by an awesome developer <a href="https://medium.com/u/8ddd94878165">Jake Wharton</a>.</p><p><strong>Adding timber to you project</strong></p><p>To add timber to your android studio project, just paste in the below line in to /app/build.gradle file and sync the project.</p><pre>implementation &#39;com.jakewharton.timber:timber:4.7.1&#39;</pre><p>Once the project is synced just plant a timber debug tree in the Application class, because as per the instruction on Timber’s github page and I quote it</p><blockquote>Installation of Tree should be done as early as possible. The onCreate of your application is the most logical choice.</blockquote><p>Here, Timber.DebugTree() prints the logs when your application type is debug application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7224da08dee58a2d2954050463d36f09/href">https://medium.com/media/7224da08dee58a2d2954050463d36f09/href</a></iframe><p>And that’s all we need to do in order to use timber. Wherever you have used that Log…(…) statement just remove that tag parameter and replace Log with Timber.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f04f000870d18c4d15433c604ec4523/href">https://medium.com/media/1f04f000870d18c4d15433c604ec4523/href</a></iframe><p>as you can see in the above gist, that it is not required to pass in the tag parameter in the Timber.d(...) or Timber.e(...) statement, as Timber automatically detects the class in which logs were written.</p><p>But there might me requirement when you need to specify a custom tag which can be simply achieved by</p><p>Timber.tag(...).e(...)</p><p>as shown in the above gist. More about Timber can be found at :</p><p><a href="https://github.com/JakeWharton/timber">GitHub - JakeWharton/timber: A logger with a small, extensible API which provides utility on top of Android&#39;s normal Log class.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=72e40cc2293d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mindorks/better-logging-in-android-using-timber-72e40cc2293d">Better Logging In Android Using Timber</a> was originally published in <a href="https://medium.com/mindorks">MindOrks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cool animations with Lottie]]></title>
            <link>https://medium.com/mindorks/cool-animations-with-lottie-524d1de6584?source=rss-75d44ff863ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/524d1de6584</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[lottie-animation]]></category>
            <category><![CDATA[android-animations]]></category>
            <dc:creator><![CDATA[Sidhant Rajora]]></dc:creator>
            <pubDate>Tue, 26 Jun 2018 09:19:00 GMT</pubDate>
            <atom:updated>2019-05-09T06:52:26.109Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*8SWrGNKQ9deleErDaEinYw.png" /></figure><p>In this blog post we will learn about a cool animation library “<a href="https://github.com/airbnb/lottie-android">Lottie</a>”, in the days prior to lottie, building complex animations for Android, iOS and React Native, was a cumbersome task and required a lot of lines of code which was hard to understand and maintain, plus a lot screen dependent assets for each type of screen and then some cool guys at AirBnb created lottie to ease out this task of making cool and amazing animations.</p><p>Lottie is a mobile library that works for Android, iOS and Web as well, it’s an Adobe After Effects animation renderer, i.e., it renders the cool animation you construct using Adobe After Effects. For achieving this task you either have to make a cool animation using Adobe After Effects and export that animation as json file using <a href="https://aescripts.com/bodymovin/">Bodymovin</a>, it is plusing for After Effects that allows you to export animations as json file.</p><p><strong>Let’s Get Started With Lottie</strong></p><p>In order to begin with lottie library for android we need to do the following things</p><p>1. Add lottie as a gradle dependency to our android project in android studio</p><p>For this, just go into your app level build.gradle file located at app/build.gradle and add the following lines in dependencies code block.</p><pre>implementation &#39;com.airbnb.android:lottie:2.5.5&#39;</pre><p>2. Make cool animation using Adobe After Effects and export it as json, using Bodymovin, or instead of going through the hassle of “How to make animations in Adobe After Effects” you can go to <a href="https://www.lottiefiles.com/">LottieFiles</a> and browse the showcased animations and select the one you like and download the json file.</p><p>Now, you are done with adding the lottie library and selecting the desired animation, now we have to add that json file into android assets folder into the project, if you already have assets folder under app directory, then just copy the json file and paste it in there. But if you don’t have assets folder then you will have to create the folder, by following this chain of sequences <strong>app&gt;New&gt;Folder&gt;Assets Folder </strong>and click finish, if you got lost somewhere, just follow the below screenshot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*9RN1bUOsXu8l3rYf8Dy97g.png" /></figure><p>and you will have assets folder created under app directory, verify it with the below screenshot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/268/1*nOqq76S7_bSHAqwbBsRpmQ.png" /></figure><p>once the assets folder is created just add the json file via pasting the file into the folder, and you will have folder structure like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/311/1*hONA1MyKN8ciidCnSh3FMg.png" /></figure><p>now, open the layout file where you wish to add the animation, and add the below code under the root layout or wherever you want to add the animation.</p><pre>&lt;com.airbnb.lottie.LottieAnimationView</pre><pre>    android:id=&quot;@+id/animation_view&quot;</pre><pre>    android:layout_width=&quot;wrap_content&quot;</pre><pre>    android:layout_height=&quot;wrap_content&quot;</pre><pre>    app:lottie_fileName=&quot;loading.json&quot;</pre><pre>    app:lottie_loop=&quot;true&quot;</pre><pre>    android:layout_centerInParent=&quot;true&quot;</pre><pre>    app:lottie_autoPlay=&quot;true&quot; /&gt;</pre><p>if you just a sample project, your complete layout file will be like</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</pre><pre>&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</pre><pre>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;</pre><pre>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;</pre><pre>    android:layout_width=&quot;match_parent&quot;</pre><pre>    android:layout_height=&quot;match_parent&quot;</pre><pre>    tools:context=&quot;.MainActivity&quot;&gt;</pre><pre>    &lt;com.airbnb.lottie.LottieAnimationView</pre><pre>        android:id=&quot;@+id/animation_view&quot;</pre><pre>        android:layout_width=&quot;wrap_content&quot;</pre><pre>        android:layout_height=&quot;wrap_content&quot;</pre><pre>        app:lottie_fileName=&quot;loading.json&quot;</pre><pre>        app:lottie_loop=&quot;true&quot;</pre><pre>        android:layout_centerInParent=&quot;true&quot;</pre><pre>        app:lottie_autoPlay=&quot;true&quot; /&gt;</pre><pre>&lt;/RelativeLayout&gt;</pre><p>under LottieAnimationView element, you will see a tag lottie_fileName, it is the name of the file you downloaded and copy pasted into assets folder, next thing is to run the application on mobile device, just click on green play button, choose your device/emulator and you will see the magic happening without writing a single java code, you will have the output like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*PZkI0f5t7yFUxhSpaGxmow.gif" /></figure><p>and the loading indicator will be animating like expected.</p><p>If you wish to have more control over the animation, you can always hook the LottieAnimationView with it’s corresponding java object and hook it up with the UI using <em>findViewById(“id of view”)</em>and then you can utilize the methods like</p><p>setMinFrame(…)</p><p>setMinFrame()</p><p>setMaxProgress()</p><p>setMaxProgress()</p><p>setMinAndMaxFrame()</p><p>setMinAndMaxProgress()</p><p>and you can add animation listeners to track the progress and completion of the animation and do other actions depending upon the condition.</p><p>To learn more about lottie visit their docs site at <a href="http://airbnb.io/lottie/android/android.html#getting-started">http://airbnb.io/lottie/android/android.html#getting-started</a>and the github repo at <a href="https://github.com/airbnb/lottie-android">https://github.com/airbnb/lottie-android</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=524d1de6584" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mindorks/cool-animations-with-lottie-524d1de6584">Cool animations with Lottie</a> was originally published in <a href="https://medium.com/mindorks">MindOrks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>