<?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 Aman Shandilya on Medium]]></title>
        <description><![CDATA[Stories by Aman Shandilya on Medium]]></description>
        <link>https://medium.com/@shandilya.aman?source=rss-fc100cb1a78f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ng9Clvr8KQo_FYJtgWJ5gA.jpeg</url>
            <title>Stories by Aman Shandilya on Medium</title>
            <link>https://medium.com/@shandilya.aman?source=rss-fc100cb1a78f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 21:03:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shandilya.aman/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: Use ComposeView with RecyclerView]]></title>
            <link>https://medium.com/@shandilya.aman/android-use-composeview-with-recyclerview-3183c95f76a1?source=rss-fc100cb1a78f------2</link>
            <guid isPermaLink="false">https://medium.com/p/3183c95f76a1</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[recyclerview]]></category>
            <category><![CDATA[composeview]]></category>
            <dc:creator><![CDATA[Aman Shandilya]]></dc:creator>
            <pubDate>Fri, 09 Aug 2024 09:44:29 GMT</pubDate>
            <atom:updated>2024-08-09T09:53:01.182Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*O2mUW5uuPzgQg7Zx" /><figcaption>Photo by <a href="https://unsplash.com/@claybanks?utm_source=medium&amp;utm_medium=referral">Clay Banks</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>RecyclerView is a powerful and flexible UI component in Android development which is used to display large datasets efficiently. Almost in every Android application development we use it. We can mold it as per our need e.g. to display different type of views in terms of height, width and working. It has efficient mechanism to recycle the views, animating the views and has adapter which act as bridge between your data and RecyclerView.</p><p>Since Jetpack Compose is introduced it is gaining popularity in a much faster pace. Jetpack Compose is a modern UI toolkit available and i hope you all must be learning and implementing it in your current projects.</p><p>Unlike the traditional imperative approach with XML layouts, Compose allows you to describe your UI by calling composable functions that take data and emit UI elements. In this blog we will try to use Jetpack Compose with RecyclerView. This can help you to migrate old android application to Jetpack Compose without changing the code completely.</p><p>We will create a RecyclerView which will inflate traditional UI component and UI component from Jetpack UI toolkit together.</p><p>Assuming we have all the required dependency added for Jetpack and other components.</p><p>First we will create an Activity class.<br>While seeing below Activity class code we can easily understand that we are creating RecyclerView and attaching Adapter with this to display the data.</p><pre>class ComposeInNativeViewActivity : AppCompatActivity() {<br><br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br><br>        val activityComposeBinding =<br>            DataBindingUtil.setContentView&lt;ActivityComposeBinding&gt;(this, R.layout.activity_compose)<br><br>        val adapter = ActivityComposeAdapter()<br><br>        activityComposeBinding.recyclerView.layoutManager =<br>            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)<br><br>        activityComposeBinding.recyclerView.adapter = adapter<br>    }<br><br>}</pre><p>Now create xml file activity_compose.xml</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;<br><br>    &lt;LinearLayout<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>        android:orientation=&quot;vertical&quot;&gt;<br><br>        &lt;androidx.recyclerview.widget.RecyclerView<br>            android:id=&quot;@+id/recyclerView&quot;<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            android:paddingHorizontal=&quot;16dp&quot;/&gt;<br><br>    &lt;/LinearLayout&gt;<br>&lt;/layout&gt;</pre><p>Now we will create the adapter and in the adapter we will inflate two different view items. One is to use traditionally available UI component and another is to use ComposeView from Jetpack Compose UI toolkit.</p><p><strong>ComposeView</strong> is a bridge that allows you to embed Jetpack Compose UI elements within your existing traditional Android views and layouts. It acts as a container for Compose content, enabling you to gradually adopt Compose in your app without rewriting everything at once. This will help us to combine traditional views and Compose elements in the same layout.</p><p>So now will create two different xml files as explained above.<br>1. item_native.xml<br>2. item_compose.xml</p><p>Create item_native.xml file.</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;<br><br>    &lt;TextView<br>        android:id=&quot;@+id/textView&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:layout_marginTop=&quot;16dp&quot;<br>        android:text=&quot;Native View Item&quot;<br>        android:textColor=&quot;@color/white&quot;<br>        android:textSize=&quot;20sp&quot;<br>        android:textStyle=&quot;bold&quot; /&gt;<br><br>&lt;/layout&gt;</pre><p>Create item_compose.xml file.</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;<br><br>    &lt;androidx.compose.ui.platform.ComposeView<br>        android:id=&quot;@+id/compose_view&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;/&gt;<br><br>&lt;/layout&gt;</pre><p>Now we will use both xml files in our Adapter class and will combine traditional views and Compose elements in the same layout.</p><p>Create Adapter class for RecyclerView</p><pre>class ActivityComposeAdapter() : RecyclerView.Adapter&lt;MyViewHolder&gt;() {<br><br><br>    class MyViewHolder(val viewDataBinding: ViewDataBinding) :<br>        RecyclerView.ViewHolder(viewDataBinding.root) {<br><br>    }<br><br>    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {<br>        val view = when (viewType) {<br>            1 -&gt; {<br>                DataBindingUtil.inflate&lt;ItemNativeBinding&gt;(<br>                    LayoutInflater.from(parent.context),<br>                    R.layout.item_native,<br>                    parent,<br>                    false<br>                )<br>            }<br><br>            else -&gt; {<br>                DataBindingUtil.inflate&lt;ItemComposeBinding&gt;(<br>                    LayoutInflater.from(parent.context),<br>                    R.layout.item_compose,<br>                    parent,<br>                    false<br>                )<br>            }<br>        }<br>        return MyViewHolder(view)<br>    }<br><br>    override fun getItemCount(): Int {<br>        return 30<br>    }<br><br>    override fun getItemViewType(position: Int): Int {<br>        val pos = position % 2<br>        return when (pos) {<br>            0 -&gt; 1<br>            else -&gt; 2<br>        }<br>    }<br><br>    override fun onBindViewHolder(holder: MyViewHolder, position: Int) =<br>        when (getItemViewType(position)) {<br>            1 -&gt; {<br>                val binding = holder.viewDataBinding as ItemNativeBinding<br>                binding.textView.setText(&quot;Native view inflated&quot;)<br>            }<br><br>            else -&gt; {<br>                val binding = holder.viewDataBinding as ItemComposeBinding<br>                binding.composeView.setContent {<br>                    Text(<br>                        modifier = Modifier.padding(top = 16.dp),<br>                        text = &quot;Compose view inflated&quot;,<br>                        style = TextStyle(fontSize = 20.sp, fontWeight = FontWeight.Bold)<br>                    )<br>                }<br>            }<br>        }<br>}</pre><p>As you can see in the above Adapter class how in the onCreateViewHolder() method we are using the item_compose.xml file which contains the ComposeView from Jetpack Compose UI toolkit while utilizing the same traditional way of create the RecyclerView. And in the onBindViewHolder() see how we are using ComposeView to add Jetpack Compose UI element in it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*vl29XMB_eiu0MI3PLigYmw.png" /></figure><p><strong>Conclusion:<br></strong>With above approach we can gradually move to Jetpack Compose without changing most of the code. As we know that Jetpack Compose requires less code than traditional XML layouts which makes code easier to understand and maintain.</p><p>Thanks for reading.<br>Happy Coading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3183c95f76a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ways to improve UI performance in Native Android]]></title>
            <link>https://medium.com/@shandilya.aman/ways-to-improve-ui-performance-in-native-android-a827085acc31?source=rss-fc100cb1a78f------2</link>
            <guid isPermaLink="false">https://medium.com/p/a827085acc31</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Aman Shandilya]]></dc:creator>
            <pubDate>Mon, 18 Dec 2023 18:21:53 GMT</pubDate>
            <atom:updated>2023-12-18T18:40:52.994Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qYZlJiqiLx6zHnFW" /><figcaption>Photo by Kelly Sikkema on Unsplash</figcaption></figure><h3><strong>Optimize layout hierarchies:</strong></h3><p>It is understood that using basic layout structures are the efficient way to create UI.<br>Here we need to understand that each widget that we add in our layout structure requires initialization and drawing.</p><p>Here we will talk about some of the basic techniques or components which can be easily implemented in native Android applications.</p><h3><strong>ViewStub:</strong></h3><p>ViewStub is a zero-sized view which is invisibleinitially. This can be used to lazily inflate the views at runtime.<br>When the viewstub is made visible or we invoke the inflate() method then it will be inflated.<br>It is used to increase the performance of the UI as the view is only inflated at runtime if required else the view will not be added in the view hierarchy.</p><p>Declare the ViewStub in xml:</p><pre><br>&lt;ViewStub<br>android:id=&quot;@+id/viewstub&quot;<br>android:inflatedId=&quot;@+id/error_layout&quot;<br>android:layout=&quot;@layout/errorLayout&quot;<br>android:layout_width=&quot;match_parent&quot;<br>android:layout_height=&quot;match_parent&quot;<br>/&gt;<br></pre><p>After inflating the layout &quot;errorLayout&quot; ViewStub will be removed.<br>Preferred way to perform the inflation is :</p><pre>ViewStub stub = findViewById(R.id.viewstub);<br>View inflated = stub.inflate();<br></pre><p>When inflate() is invoked, the ViewStub is replaced by the inflated View and the inflated View is returned.<br>You can also get callback once the layout is inflated by using <strong>ViewStub.onInflateListener</strong>.</p><h3>&lt;merge&gt; tag:</h3><p>Excessive layout initialization and drawing canslows down the UI performance. We can improve the performance byflattening the layout.<br>The &lt;merge&gt; tag helps eliminate redundant view groups in your view hierarchy when including one layout within another.<br>Example: If we are using &lt;include&gt; tag to include another layout form another file without using merge.<br>layout1.xml:</p><pre>&lt;FrameLayout&gt;<br>&lt;include layout=&quot;@layout/layout2&quot;/&gt;<br>&lt;/FrameLayout&gt;<br></pre><p>layout2.xml:</p><pre>&lt;FrameLayout&gt;<br>&lt;TextView /&gt;<br>&lt;TextView /&gt;<br>&lt;/FrameLayout&gt;<br></pre><p>which is functionally equivalent to this single layout:</p><pre>&lt;FrameLayout&gt;<br>&lt;FrameLayout&gt;<br>&lt;TextView /&gt;<br>&lt;TextView /&gt;<br>&lt;/FrameLayout&gt;<br>&lt;/FrameLayout&gt;<br></pre><p>and if we use &lt;merge&gt; tag with &lt;include&gt; tag then it will look like this:<br>layout2.xml:</p><pre>&lt;merge&gt;<br>&lt;TextView /&gt;<br>&lt;TextView /&gt;<br>&lt;/merge&gt;<br></pre><p>This is functionally equivalent to this layout:</p><pre>&lt;FrameLayout&gt;<br>&lt;TextView /&gt;<br>&lt;TextView /&gt;<br>&lt;/FrameLayout&gt;<br></pre><h3>AsyncLayoutInflator:</h3><p>It helps inflating layoutsasynchronously. This can be used when part of the UI created lazily or in response to user interactions.<br>This allows the UI thread to continue to be responsive and animate while the relatively heavy inflate is being performed.</p><h3>RecyclerView pool:</h3><p>RecyclerView is the widget that we use frequently. Use of recyclerview inside a recyclerview is a very common design pattern now a days.<br>As we already use ViewHolder pattern while creating recyclerview, same pool kind of concept can be use here when we are using nested recyclerview.<br>Because most of the time we same ViewType between the different recyclerviews. As each recyclerview already have this pool concept individually but that can be used as common approch for all the recyclerview.<br>That means all the recyclerview can share same view pool.<br>This can be done using static RecycledViewPool class which remains in RecyclerView class.</p><pre>RecyclerView.setRecycledViewPool(mRecycledViewPool).<br></pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a827085acc31" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to establish communication between Flutter and platform-specific code?]]></title>
            <link>https://medium.com/@shandilya.aman/how-to-establish-communication-between-flutter-and-platform-specific-code-e4c01b8f676d?source=rss-fc100cb1a78f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4c01b8f676d</guid>
            <category><![CDATA[flutter-to-native]]></category>
            <category><![CDATA[method-channel]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[platform-channel]]></category>
            <category><![CDATA[dart]]></category>
            <dc:creator><![CDATA[Aman Shandilya]]></dc:creator>
            <pubDate>Tue, 14 Sep 2021 09:43:03 GMT</pubDate>
            <atom:updated>2021-09-14T09:43:03.763Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*T1U7u771enHqzngD" /><figcaption>Photo by <a href="https://unsplash.com/@mischievous_penguins?utm_source=medium&amp;utm_medium=referral">Casey Horner</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>There are certain number of task which can only be achieved through platform-specific code. For example if you are developing a media streaming application for Android, iOS and Web where you will be implementing DRM for right protected content, then you will have to write platform-specific code.</p><p>Some of platform specific functionalities are already available through Flutter existing packages. But we still need to do platform-specific code in many cases while developing cross platform application in Flutter.</p><p>Flutter provide a general framework to communicate with platform specific code. Flutter uses a flexible system that allows you to call platform-specific APIs whether available in Kotlin or Java code on Android, or in Swift or Objective-C code on iOS.</p><blockquote><strong>How it works?</strong></blockquote><p>Let’s understand this with a simple diagram.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*CP-iQ7TJs_an49QSg5jI4w.png" /></figure><p>In above diagram we have three entities i.e. Client, MethodChannel and Host.<br>Let’s understand them one by one.</p><p><strong>Client:</strong> Client is which make request or consume the output. So in our case it is Flutter code from where we request for some native operation from platform specific code. <br><strong>Platform Method Channel:</strong><a href="https://flutter.dev/docs/development/platform-integration/platform-channels">Platform Method Channel</a> is something that provide a interface to easily interact with platform specific code. Technically, we use <a href="https://api.flutter.dev/javadoc/io/flutter/plugin/common/MethodChannel.html">MethodChannel</a> for Android and <a href="https://api.flutter.dev/objcdoc/Classes/FlutterMethodChannel.html">FlutterMethodChannel</a> for iOS. A name should be specified whenever you create a platform method channel. <br>For example, if you are creating a method channel for android and iOS platform, you can name like <em>com.appname/android </em>for Android platform and<em> com.appname/ios </em>for iOS platform<em>.</em> We can have multiple methodchannel at the same time.<em><br></em><strong>Host:</strong><em> </em>Host is something that respond to request. In over case platform specific code is our host.</p><p>Requests and responses are passed asynchronously, to ensure the user interface remains responsive.</p><p>We will be creating a simple application in Flutter that will send and receive data from Android platform-specific code.<br>First we will create a client that will be Flutter code written in Dart, where we will have a Button and a Text widget.</p><pre><strong>import &#39;package:flutter/material.dart&#39;</strong>;<br><strong>import &#39;package:flutter/services.dart&#39;</strong>;<br><br><strong>void </strong>main() {<br>  runApp(MyApp());<br>}<br><br><strong>class </strong>MyApp <strong>extends </strong>StatelessWidget {<br>  @override<br>  Widget build(BuildContext context) {<br>    <strong>return </strong>MaterialApp(<br>      title: <strong>&#39;Method Channel&#39;</strong>,<br>      theme: ThemeData(<br>        primarySwatch: Colors.<em>red</em>,<br>      ),<br>      home: MyHomePage(title: <strong>&#39;Platform Method Channel&#39;</strong>),<br>    );<br>  }<br>}<br><br><strong>class </strong>MyHomePage <strong>extends </strong>StatefulWidget {<br>  MyHomePage({Key? key, <strong>required this</strong>.<strong>title</strong>}) : <strong>super</strong>(key: key);<br>  <strong>final </strong>String <strong>title</strong>;<br><br>  @override<br>  _MyHomePageState createState() =&gt; _MyHomePageState();<br>}<br><br><strong>class </strong>_MyHomePageState <strong>extends </strong>State&lt;MyHomePage&gt; {<br>  <strong>static const </strong><em>platform </em>= MethodChannel(<strong>&#39;com.channel/android&#39;</strong>);<br><br>  String <strong>_operationResultState </strong>= <strong>&#39;Unknown operation&#39;</strong>;<br><br>  Future&lt;<strong>void</strong>&gt; _performOperation() <strong>async </strong>{<br>    String operationResult;<br>    <strong>try </strong>{<br>      <strong>final </strong>int result =<br>          <strong>await </strong><em>platform</em>.invokeMethod(<strong>&#39;perform_operation&#39;</strong>, {<strong>&quot;arg1&quot;</strong>: 20, <strong>&quot;arg2&quot;</strong>: 40});<br>      operationResult = <strong>&#39;Performed operation value is </strong>$result<strong> .&#39;</strong>;<br>    } <strong>on </strong>PlatformException <strong>catch </strong>(e) {<br>      operationResult = <strong>&quot;Failed to perform operation: &#39;</strong>${e.<strong>message</strong>}<strong>&#39;.&quot;</strong>;<br>    }<br><br>    setState(() {<br>      <strong>_operationResultState </strong>= operationResult;<br>    });<br>  }<br><br>  @override<br>  Widget build(BuildContext context) {<br>    <strong>return </strong>Scaffold(<br>      appBar: AppBar(<br>        title: Text(<strong>widget</strong>.<strong>title</strong>),<br>      ),<br>      body: Center(<br>        child: Column(<br>          mainAxisAlignment: MainAxisAlignment.<strong>spaceEvenly</strong>,<br>          children: &lt;Widget&gt;[<br>            ElevatedButton(<br>                onPressed: () {<br>                  _performOperation();<br>                },<br>                child: Text(<strong>&#39;Perform Operation&#39;</strong>)),<br>            Text(<strong>_operationResultState</strong>, style: TextStyle(fontSize: 22),)<br>          ],<br>        ),<br>      ), <em>// This trailing comma makes auto-formatting nicer for build methods.<br>    </em>);<br>  }<br>}</pre><p>In above piece of code we have one Button and on pressing it we are calling a method _performOperation(). This is a async method.<br>Now we will create PlatformMethodChannel.</p><p><strong>static const </strong><em>platform </em>= MethodChannel(<strong>‘com.channel/android’</strong>);</p><p>We have created a object of MethodChannel with a specific name.</p><p>Now using platform object we will call method from native code.</p><pre>Future&lt;<strong>void</strong>&gt; _performOperation() <strong>async </strong>{<br>  String operationResult;<br>  <strong>try </strong>{<br>    <strong>final </strong>int result =<br>        <strong>await </strong><em>platform</em>.invokeMethod(<strong>&#39;perform_operation&#39;</strong>, {<strong>&quot;arg1&quot;</strong>: 20, <strong>&quot;arg2&quot;</strong>: 40});<br>    operationResult = <strong>&#39;Performed operation value is </strong>$result<strong> .&#39;</strong>;<br>  } <strong>on </strong>PlatformException <strong>catch </strong>(e) {<br>    operationResult = <strong>&quot;Failed to perform operation: &#39;</strong>${e.<strong>message</strong>}<strong>&#39;.&quot;</strong>;<br>  }<br><br>  setState(() {<br>    <strong>_operationResultState </strong>= operationResult;<br>  });<br>}</pre><p>Above method is async method. Here we are invoking a native method registered with name <em>‘perform_operation’</em> in our <em>host</em>. Also, we are passing two arguments <em>arg1</em> and <em>arg2.</em></p><p>Now let’s move to our host. In our host(Android native code here), you will find an Activity already created that extends FlutterActivity class.</p><pre><strong>class </strong>MainActivity : FlutterActivity() {<br>    <strong>private val CHANNEL </strong>= <strong>&quot;com.channel/android&quot;<br><br>    override fun </strong>configureFlutterEngine(flutterEngine: FlutterEngine) {<br>        <strong>super</strong>.configureFlutterEngine(flutterEngine)<br>        MethodChannel(flutterEngine.<em>dartExecutor</em>.<em>binaryMessenger</em>, <strong>CHANNEL</strong>)<br>            .setMethodCallHandler <strong>{ </strong>call, result <strong>-&gt;<br>                if </strong>(call.<strong>method </strong>== <strong>&quot;perform_operation&quot;</strong>) {<br>                    <strong>val </strong>operationResult = performOperation(call.argument&lt;Int&gt;(<strong>&quot;arg1&quot;</strong>)!!, call.argument&lt;Int&gt;(<strong>&quot;arg2&quot;</strong>)!!)<br>                    result.success(operationResult)<br>                } <strong>else </strong>{<br>                    result.notImplemented()<br>                }<br>            <strong>}<br>    </strong>}<br><br>    <strong>private fun </strong>performOperation(argument1: Int, argument2: Int): Int {<br>        <strong>return </strong>(argument1 + argument2)<br>    }<br>}</pre><p>As you can see in above we have CHANNEL name same as we provided before in our <em>client(Flutter Code). </em>With same channel name we are creating a MethodChannel in Host and setting-up a methodCallHandler. So whenever we invoke a method from our client then our methodCallHandler will give us that invocation in ‘<em>call’</em> object parameter. So here we are invoking ‘<strong>perform_operation</strong>’ method from our client and as you can see in our host we have one check for this invocation. So when above method invoked from client we are calling <em>‘performOperation(arg1, arg2)’</em> method that takes two Int parameter and do operation on them.</p><p>This is a very basic example for calling native code using method channel in Flutter. In the same manner we can use any native api that we don’t have in Flutter or due to some limitations we want to use it from native only.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4c01b8f676d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Architecture Components — Part 1 (Room)]]></title>
            <link>https://medium.com/android-news/android-architecture-component-part-1-room-a524e9aa8910?source=rss-fc100cb1a78f------2</link>
            <guid isPermaLink="false">https://medium.com/p/a524e9aa8910</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[architecture-components]]></category>
            <category><![CDATA[room]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Aman Shandilya]]></dc:creator>
            <pubDate>Sat, 19 May 2018 16:36:19 GMT</pubDate>
            <atom:updated>2018-05-23T18:44:14.056Z</atom:updated>
            <content:encoded><![CDATA[<p>If you have been working with android, then you will be familiar with SQLite Database which is an inbuilt local database for android.</p><p>Remember the amount of boilerplate code you had to write to create and manipulate even a very small database? You had to define the database structure, create an <strong>SQLiteOpenHelper</strong> class etc.</p><p>There in SQLite database no compile time verification of raw SQL quires. Also you have to maintain the CursorAdapter to fetch data from SQLite database. And if your schema get changed you need to update the affected SQL quires.</p><p>Above are some problems with SQLite databases.</p><p>First of all we’ll point out some benefits of the Room persistence library from android architecture components and then we’ll create a very basic android example using SQLite with Room.</p><p><strong>Let’s start Room from here:</strong></p><p><em>Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.</em></p><p><em>Compile-time verification of SQL queries — each query and entity is checked at the compile time, so there’s no risk of run-time error that might crash your app (and it doesn’t check only syntax, but also e.g. missing tables).</em></p><p><em>Very little boilerplate code.</em></p><p><strong>There are some basic needs of Room:</strong></p><ol><li><strong>@Entity(tableName = “name_of_your_table”)</strong>: <em>this is an annotated class that describes a database table. Each @Entity class represents an entity in a table. Annotate your class declaration to indicate that it’s an entity. Specify the name of the table if you want it to be different from the name of the class.</em></li><li><strong>@PrimaryKey: </strong><em>Every entity needs a primary key. To keep things simple, each word acts as its own primary key.</em></li><li><strong>@ColumnInfo(name = “name_of-your_column”): </strong><em>Specify the name of the column in the table if you want it to be different from the name of the member variable.</em></li><li><em>Every field that’s stored in the database needs to be either public or have a “getter” method.</em></li><li><strong>DAO</strong>: <em>Data access object. A mapping of SQL queries to functions. You used to have to define these painstakingly in your </em><a href="https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html"><em>SQLiteOpenHelper</em></a><em> class. When you use a DAO, you call the methods, and Room takes care of the rest. </em><em>In the </em><a href="https://developer.android.com/training/data-storage/room/accessing-data.html"><em>DAO</em></a><em> (data access object), you specify SQL queries and associate them with method calls. The compiler checks the SQL and generates queries from convenience annotations for common queries, such as @Insert, @Delete etc. </em><strong>The DAO must be an interface or abstract class.</strong></li><li><strong>Room database:</strong> <em>Database layer on top of SQLite database that takes care of mundane tasks that you used to handle with an SQLiteOpenHelper. Database holder that serves as an access point to the underlying SQLite database. The Room database uses the DAO to issue queries to the SQLite database.</em></li></ol><h3>Let’s Create a basic example:</h3><p><strong>Add dependencies: </strong><br>In your app level build.gradle file add these two dependencies for Room library.</p><p><em>compile ‘android.arch.persistence.room:runtime:1.0.0;<br>annotationProcessor ‘android.arch.persistence.room:compiler:’1.0.0;</em></p><pre><strong>Create Entity:</strong></pre><pre><strong><br>import</strong> android.arch.persistence.room.ColumnInfo;<br><strong>import</strong> android.arch.persistence.room.Entity;<br><strong>import</strong> android.arch.persistence.room.PrimaryKey;</pre><pre>@Entity<br><strong>public</strong> <strong>class</strong> User {</pre><pre>@PrimaryKey(autoGenerate = <strong>true</strong>)<br><strong>private</strong> <strong>int</strong> uid;</pre><pre>@ColumnInfo(name = &quot;first_name&quot;)<br><strong>private</strong> String firstName;</pre><pre>@ColumnInfo(name = &quot;last_name&quot;)<br><strong>private</strong> String lastName;</pre><pre>@ColumnInfo(name = &quot;age&quot;)<br><strong>private</strong> <strong>int</strong> age;</pre><pre><strong>public</strong> <strong>int</strong> getUid() {<br>   <strong>return</strong> uid;<br>}</pre><pre><strong>public</strong> <strong>void</strong> setUid(<strong>int</strong> uid) {<br>   <strong>this</strong>.uid = uid;<br>}</pre><pre><strong>public</strong> String getFirstName() {<br>   <strong>return</strong> firstName;<br>}</pre><pre><strong>public</strong> <strong>void</strong> setFirstName(String firstName) {<br>   <strong>this</strong>.firstName = firstName;<br>}</pre><pre><strong>public</strong> String getLastName() {<br>   <strong>return</strong> lastName;<br>}</pre><pre><strong>public</strong> <strong>void</strong> setLastName(String lastName) {<br>   <strong>this</strong>.lastName = lastName;<br>}</pre><pre><strong>public</strong> <strong>int</strong> getAge() {<br>   <strong>return</strong> age;<br>}</pre><pre><strong>public</strong> <strong>void</strong> setAge(<strong>int</strong> age) {<br>   <strong>this</strong>.age = age;<br>}<br>}</pre><p>As you can see in above code I am creating a Entity by using @Entity annotation. This annotation defines user class as a database table and data in it will be saved in this table. In User entity there I have defined a primary key by using <em>@PrimaryKey(autoGenerate = </em><strong><em>true</em></strong><em>) and three other columns by using @ColumnInfo(name = “name_of_column”).</em></p><p>Now I am creating DAO for User entity.</p><pre>@Dao<br><strong>public</strong> <strong>interface</strong> UserDao {</pre><pre>@Query(&quot;select * from USER&quot;)<br>List&lt;User&gt; getAll();</pre><pre>@Query(&quot;SELECT * FROM user where first_name LIKE  :firstName AND last_name LIKE :lastName&quot;)<br>User findByName(String firstName, String lastName);</pre><pre>@Query(&quot;SELECT COUNT(*) from user&quot;)<br><strong>int</strong> countUsers();</pre><pre>@Insert<br><strong>void</strong> insert(User user);</pre><pre>@Delete<br><strong>void</strong> delete(User user);<br>}</pre><p>In the above code snippet you can see DAO is an interface and is annotated by @DAO annotation. By this annotation room database treat this interface as a DAO. In UserDao interface I am defining raw queries by using @Query annotation and direct method to insert and delete user from database by using @Insert and @Delete annotation respectively.</p><p>As we have table and quries for database now finally we will create RoomDatabase.</p><pre>@Database(entities = {User.<strong>class</strong>}, version = 1, exportSchema = <strong>false</strong>)</pre><pre><strong>public</strong> <strong>abstract</strong> <strong>class</strong> AppDatabase <strong>extends</strong> RoomDatabase {</pre><pre><strong>private</strong> <strong>static</strong> AppDatabase INSTANCE;<br><strong>public</strong> <strong>abstract</strong> UserDao userDao();</pre><pre><strong>public</strong> <strong>static</strong> AppDatabase getAppDatabase(Context context) {<br>  <strong>if</strong> (INSTANCE == null) {<br>      <strong>synchronized</strong> (AppDatabase.<strong>class</strong>) {<br>         <strong>if</strong> (INSTANCE == null) {<br>      INSTANCE=Room.databaseBuilder(context.getApplicationContext(),  AppDatabase.<strong>class</strong>, &quot;user-database&quot;)<br>.allowMainThreadQueries().build();<br>}<br>}<br>}<br><strong>return</strong> INSTANCE;<br>}</pre><pre><strong>public</strong> <strong>static</strong> <strong>void</strong> destroyInstance() {<br>   INSTANCE = null;<br>}<br>}</pre><p>As you can see I have created a singleton abstract class by extending RoomDatabase class. As we already know Database class must be abstract and must extend RoomDatabase class. I am using Singletom pattern because I want only one instance of AppDatabase class in whole application. Above AppDatabase class is annotated by- <br>@Database(entities = {User.<strong>class</strong>}, version = 1, exportSchema = <strong>false</strong>).<br>Here in above code snippet entities is an array of all entities those will be added in your database then there is database version number after that exportSchema=false.</p><p>Now lets add database in our MainActivity class. <strong><em>You can find the complete code </em></strong><a href="https://github.com/shandilyaaman/ExampleRoomDatabase"><strong><em>here</em></strong></a><strong><em>.</em></strong> But in this doc I am just pasting my MainActivity class just for the understanding.</p><pre><strong>public</strong> <strong>class</strong> MainActivity <strong>extends</strong> AppCompatActivity {</pre><pre><strong>private</strong> Button btnAdd;<br><strong>private</strong> EditText etFname, etLname, etAge;<br><strong>private</strong> RecyclerView recyclerView;</pre><pre><strong>private</strong> AppDatabase appDatabase;<br><strong>private</strong> UserDao userDao;<br><strong>private</strong> MyAdapter myAdapter;</pre><pre>@Override<br><strong>protected</strong> <strong>void</strong> onCreate(Bundle savedInstanceState) {<br>  <strong>super</strong>.onCreate(savedInstanceState);<br>  setContentView(R.layout.activity_main);<br>  <br>  appDatabase = AppDatabase.getAppDatabase(MainActivity.<strong>this</strong>);<br>  userDao = appDatabase.userDao();<br> <br>  btnAdd = findViewById(R.id.btn_adduser);<br>  etFname = findViewById(R.id.et_fname);<br>  etLname = findViewById(R.id.et_lname); <br>  etAge = findViewById(R.id.et_age);<br>  recyclerView = findViewById(R.id.recyclerview);</pre><pre>  RecyclerView.LayoutManager linearLayoutManager = <strong>new</strong>    <br>  LinearLayoutManager(MainActivity.<strong>this</strong>);<br>  recyclerView.setLayoutManager(linearLayoutManager);</pre><pre>  List&lt;User&gt; userList = <strong>new</strong> ArrayList&lt;&gt;();<br>  myAdapter = <strong>new</strong> MyAdapter(userList, MainActivity.<strong>this</strong>);<br>  recyclerView.setAdapter(myAdapter);</pre><pre>  userList = userDao.getAll();<br>  myAdapter.refreshList(userList);</pre><pre>  btnAdd.setOnClickListener((view) -&gt; {<br>      User user = <strong>new</strong> User();<br>      user.setFirstName(etFname.getText().toString());<br>      user.setLastName(etLname.getText().toString());<br>      user.setAge(Integer.parseInt(etAge.getText().toString()));<br>      userDao.insertAll(user);</pre><pre>  });</pre><pre>  }<br>}</pre><p>In above class I am creating instance of AppDatabse class which is a Singleton class that extend RoomDatabse class.</p><p><em>Find the complete code </em><a href="https://github.com/shandilyaaman/ExampleRoomDatabase"><strong><em>here</em></strong></a><em>.</em> In this code I am using LiveData also but we will talk about LiveData and ViewModel in next part with the same example till then Happy Coding :)</p><p><em>Thanks for reading.</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F3e491f%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F3e491f%2F&amp;image=http%3A%2F%2Fapi.screenshotlayer.com%2Fapi%2Fcapture%3Faccess_key%3Dfe59908dad3baab69ffab249a2224b03%26viewport%3D1024x612%26width%3D1000%26url%3Dhttps%253A%252F%252Fupscri.be%252F3e491f%253Fscreenshot&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href">https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a524e9aa8910" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/android-architecture-component-part-1-room-a524e9aa8910">Android Architecture Components — Part 1 (Room)</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dive into Java Method Overloading]]></title>
            <link>https://medium.com/android-news/dive-into-java-method-overloading-afc22bcf9678?source=rss-fc100cb1a78f------2</link>
            <guid isPermaLink="false">https://medium.com/p/afc22bcf9678</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java-method-overloading]]></category>
            <category><![CDATA[method-overloading]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[overloading]]></category>
            <dc:creator><![CDATA[Aman Shandilya]]></dc:creator>
            <pubDate>Sat, 01 Apr 2017 18:45:47 GMT</pubDate>
            <atom:updated>2017-07-24T14:51:38.490Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*aGkeTAAygDLAOgnghFwL8Q.png" /></figure><p>Method overloading allows a class to have two or more methods having same name. But there are some conditions applied if you overload a method. Let’s discuss on then.</p><p>First of all we should know “<em> Why do we use method overloading in Java?</em>” .</p><p>Suppose we have to perform addition of given number but there can be any number of arguments, if we write method such as <strong>methodAddTwoNo(int a, int b) </strong>for two arguments, <strong>methodAddThreeNo(int a, int b, int c)</strong> for three arguments then it is very difficult for you and other programmer to understand purpose or behaviors of method they can not identify purpose of method. So we use method overloading to easily figure out the program. For example above two methods we can write sum(int a, int b) and sum(int a, int b, int c) using method overloading concept.</p><p>So it is clear that to achieve method overloading concept methods should have same name but with different parameters.</p><p>But what will happen if we put methods name same and also the same parameters.</p><p>Let’s see:</p><p><strong>public class OverloadJava<br>{<br> public static void main(String[] args){<br> sum();<br> }</strong></p><p><strong>void sum(){<br>	}<br>	<br>	void sum(){<br>	}<br>}</strong></p><p>In the above class two methods with same name and no input parameter. This will be a compile time error because these methods are duplicate.</p><p><em>Now if we change the return type of one method, what will happen???</em></p><p>Still the same because JVM is still in confusion that which method​ is being called. So again it’s a compile time error.</p><blockquote>Now the question is “Can we overload main method?”</blockquote><p><strong>Yes we can overload main method because main method is static and it follows all rules for static keyword in java, so you can say that we can overload a static method in java.</strong></p><p>public class OverloadJava<br>{<br>	public static void main(String[] args){<br>	System.out.println(&quot;main&quot;);<br>	}<br>	<br>	public static void main(){<br>	System.out.println(&quot;main 2&quot;);<br>	}</p><p>}</p><blockquote>Now you will be thinking can we overload a final method??</blockquote><p>Answer is Yes, we can overload a final method but remember input parameters should be different.</p><p>So the conclusion is “One and only rule of method overloading in Java is that method signature of all overloaded method must be different. Method signature is changed by changing either number of method arguments, or type of method arguments e.g. System.out.println() method is overloaded to accept different primitive types like int, short, byte, float etc. They all accept just one argument but their type is different. You can also change method signature by changing order of method argument.”</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F3e491f%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F3e491f%2F&amp;image=http%3A%2F%2Fapi.screenshotlayer.com%2Fapi%2Fcapture%3Faccess_key%3Dfe59908dad3baab69ffab249a2224b03%26viewport%3D1024x612%26width%3D1000%26url%3Dhttps%253A%252F%252Fupscri.be%252F3e491f%253Fscreenshot&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href">https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afc22bcf9678" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/dive-into-java-method-overloading-afc22bcf9678">Dive into Java Method Overloading</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implement Android Data Binding library in your android app: Part-2]]></title>
            <link>https://medium.com/android-news/implement-android-data-binding-library-in-your-android-app-part-2-d30c717b1b63?source=rss-fc100cb1a78f------2</link>
            <guid isPermaLink="false">https://medium.com/p/d30c717b1b63</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android-data-binding]]></category>
            <dc:creator><![CDATA[Aman Shandilya]]></dc:creator>
            <pubDate>Sun, 26 Mar 2017 11:06:22 GMT</pubDate>
            <atom:updated>2017-03-27T18:19:25.602Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ELRVnirY-gR76OhAjH0Yxw.gif" /></figure><p>In <a href="https://android.jlelse.eu/lets-implement-android-data-binding-in-your-android-app-d6c567c242c9#.s5ienjcub"><strong>Part-1</strong></a> we discussed about how to implement Android Data Binding library in your android app. In this part we will see how we can bind data within our layout xml files and also the clickListeners on buttons.</p><p>Data-binding layout files are slightly different and start with a root tag of <strong>layout</strong> followed by a <strong>data</strong> element and a <strong>view</strong> root element. This view element is what your root would be in a non-binding layout file. A sample file looks like this:</p><pre>&lt;layout&gt;<br>&lt;<strong>data</strong>&gt;<br><br>    &lt;<strong>variable<br>        name=&quot;model&quot;<br>        type=&quot;sample.android.databinding.SampleModel&quot; </strong>/&gt;<br>&lt;/<strong>data</strong>&gt;</pre><pre>...</pre><pre>&lt;/layout?</pre><p>The model <strong>variable</strong> within <strong>data</strong> describes a property that may be used within this layout.</p><p>You can also import reference classes in your layout file within data tag.</p><pre>&lt;import type=&quot;android.view.View&quot;/&gt;</pre><p><strong>Important</strong>, if you are implementing Android Data Binding library with <strong>fragments </strong>so you have to import Fragment class in your fragment’s layout file.</p><pre>&lt;import type=&quot;android.support.v4.app.Fragment&quot;/&gt;</pre><p>Here is the TextView to show if user is online or not. Expressions within the layout are written in the attribute properties using the “@{}&quot; syntax.</p><p>See the code below:</p><pre>&lt;<strong>TextView<br>    android:id=&quot;@+id/userStatus&quot;<br>    android:layout_width=&quot;50dp&quot;<br>    android:layout_height=&quot;50dp&quot;<br>    android:layout_marginRight=&quot;10dp&quot;<br>    android:background=&quot;@{model.online == true ? @color/colorAccent : @color/colorPrimary}&quot; </strong>/&gt;</pre><p>Here in the above piece of code we are setting TextView background color by checking if user isOnline or not.</p><p>Taking another example setting the username value from model(POJO class) class in the xml directly :</p><pre>&lt;<strong>TextView<br>   android:id=&quot;@+id/name&quot;<br>   android:layout_width=&quot;wrap_content&quot;<br>   android:layout_height=&quot;wrap_content&quot;<br>   android:layout_toEndOf=&quot;@id/userStatus&quot;<br>   android:layout_toRightOf=&quot;@id/userStatus&quot;<br>   android:text=&quot;@{model.username}&quot;<br>   android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><p>SampleModel.java</p><pre><strong>public class </strong>SampleModel <strong>extends </strong>BaseObservable {<br>    <strong>private </strong>String <strong>username</strong>, <strong>useremail</strong>;<br>    <strong>private boolean isOnline</strong>;<br><br>    <strong>public </strong>SampleModel(String username, String useremail, <strong>boolean </strong>online) {<br>        <strong>this</strong>.<strong>username </strong>= username;<br>        <strong>this</strong>.<strong>useremail </strong>= useremail;<br>        <strong>isOnline </strong>= online;<br>    }<br><br>    <strong>public </strong>String getUsername() {<br>        <strong>return username</strong>;<br>    }<br><br>    <strong>public void </strong>setUsername(String username) {<br>        <strong>this</strong>.<strong>username </strong>= username;<br>        notifyChange();<br>    }<br><br>    <strong>public </strong>String getUseremail() {<br>        <strong>return useremail</strong>;<br>    }<br><br>    <strong>public void </strong>setUseremail(String useremail) {<br>        <strong>this</strong>.<strong>useremail </strong>= useremail;<br>        notifyChange();<br>    }<br><br>    <strong>public boolean </strong>isOnline() {<br>        <strong>return isOnline</strong>;<br>    }<br><br>    <strong>public void </strong>setOnline(<strong>boolean </strong>online) {<br>        <strong>isOnline </strong>= online;<br>        notifyChange();<br>    }<br>}</pre><p>By default, a Binding class will be generated based on the name of the layout file, converting it to Pascal case and suffixing “Binding” to it.</p><p>MainActivity.java</p><pre><strong>public class </strong>MainActivity <strong>extends </strong>AppCompatActivity {<br>    <strong>private </strong>ActivityMainBinding <strong>activityMainBinding</strong>;<br>SampleModel <strong>model</strong>;<br><br>@Override<br><strong>protected void </strong>onCreate(Bundle savedInstanceState) {<br>    <strong>super</strong>.onCreate(savedInstanceState);<br><br>    <strong>activityMainBinding </strong>= DataBindingUtil<br>            .<em>setContentView</em>(<strong>this</strong>, R.layout.<strong><em>activity_main</em></strong>);<br>    <strong>model </strong>= <strong>new </strong>SampleModel(<strong>&quot;Aman&quot;</strong>, <strong>&quot;shandilya.aman@gmail.com&quot;</strong>, <strong>true</strong>);<br>    <strong>activityMainBinding</strong>.setModel(<strong>model</strong>);<br>    <strong>activityMainBinding</strong>.setActivity(<strong>this</strong>);<br>}<br><br><strong>public void </strong>onButtonClick(<strong>boolean </strong>status) {<br>    <strong>model</strong>.setOnline(status);<br>}<br>}</pre><p>activity_main.xml</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;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;</strong>&gt;<br><br>    &lt;<strong>data</strong>&gt;<br><br>        &lt;<strong>variable<br>            name=&quot;model&quot;<br>            type=&quot;sample.android.databinding.SampleModel&quot; </strong>/&gt;<br><br>        &lt;<strong>variable<br>            name=&quot;activity&quot;<br>            type=&quot;sample.android.databinding.MainActivity&quot; </strong>/&gt;<br>    &lt;/<strong>data</strong>&gt;<br><br>    &lt;<strong>RelativeLayout<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>        android:padding=&quot;16dp&quot;<br>        tools:context=&quot;sample.android.databinding.MainActivity&quot;</strong>&gt;<br><br>        &lt;<strong>RelativeLayout<br>            android:id=&quot;@+id/top&quot;<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;wrap_content&quot;</strong>&gt;<br><br>            &lt;<strong>TextView<br>                android:id=&quot;@+id/userStatus&quot;<br>                android:layout_width=&quot;50dp&quot;<br>                android:layout_height=&quot;50dp&quot;<br>                android:layout_marginRight=&quot;10dp&quot;<br>                android:background=&quot;@{model.online == true ? @color/colorAccent : @color/colorPrimary}&quot; </strong>/&gt;<br><br>            &lt;<strong>TextView<br>                android:id=&quot;@+id/name&quot;<br>                android:layout_width=&quot;wrap_content&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_toEndOf=&quot;@id/userStatus&quot;<br>                android:layout_toRightOf=&quot;@id/userStatus&quot;<br>                android:text=&quot;@{model.username}&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;<br><br>            &lt;<strong>TextView<br>                android:id=&quot;@+id/email&quot;<br>                android:layout_width=&quot;wrap_content&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_below=&quot;@id/name&quot;<br>                android:layout_toEndOf=&quot;@id/userStatus&quot;<br>                android:layout_toRightOf=&quot;@id/userStatus&quot;<br>                android:text=&quot;@{model.useremail}&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;<br><br>            &lt;<strong>TextView<br>                android:id=&quot;@+id/status&quot;<br>                android:layout_width=&quot;wrap_content&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_below=&quot;@id/email&quot;<br>                android:layout_toEndOf=&quot;@id/userStatus&quot;<br>                android:layout_toRightOf=&quot;@id/userStatus&quot;<br>                android:text=&quot;@{model.online == true ? @string/online : @string/offline}&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;<br>        &lt;/<strong>RelativeLayout</strong>&gt;<br><br>        &lt;<strong>Button<br>            android:id=&quot;@+id/btnOnline&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:layout_below=&quot;@+id/top&quot;<br>            android:layout_marginTop=&quot;10dp&quot;<br>            android:onClick=&quot;@{()-&gt;activity.onButtonClick(true)}&quot;<br>            android:text=&quot;@string/online&quot; </strong>/&gt;<br><br>        &lt;<strong>Button<br>            android:id=&quot;@+id/btnOffline&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:layout_below=&quot;@+id/top&quot;<br>            android:layout_marginTop=&quot;10dp&quot;<br>            android:layout_toRightOf=&quot;@id/btnOnline&quot;<br>            android:onClick=&quot;@{()-&gt;activity.onButtonClick(false)}&quot;<br>            android:text=&quot;@string/offline&quot; </strong>/&gt;<br><br>    &lt;/<strong>RelativeLayout</strong>&gt;<br>&lt;/<strong>layout</strong>&gt;</pre><p>You can find the working sample code <a href="https://github.com/shandilyaaman/SampleAndroidDataBinding"><strong>here</strong></a><strong>.</strong></p><p><a href="https://github.com/shandilyaaman/SampleAndroidDataBinding">shandilyaaman/SampleAndroidDataBinding</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F3e491f%3Fas_embed%3Dtrue&amp;url=https%3A%2F%2Fupscri.be%2F3e491f%2F&amp;image=https%3A%2F%2Fupscri.be%2Fmedia%2Fform.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ae57c1b4448392f65f474b01a127fbf/href">https://medium.com/media/2ae57c1b4448392f65f474b01a127fbf/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d30c717b1b63" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/implement-android-data-binding-library-in-your-android-app-part-2-d30c717b1b63">Implement Android Data Binding library in your android app: Part-2</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Let’s implement Android Data Binding library in your android app: Part-1]]></title>
            <link>https://medium.com/android-news/lets-implement-android-data-binding-in-your-android-app-d6c567c242c9?source=rss-fc100cb1a78f------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6c567c242c9</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android-data-binding]]></category>
            <dc:creator><![CDATA[Aman Shandilya]]></dc:creator>
            <pubDate>Tue, 21 Mar 2017 17:39:21 GMT</pubDate>
            <atom:updated>2017-03-26T09:07:07.523Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*vOKbSoAW5UC4MqtsDwmIBQ.png" /></figure><p>Now there is no need for the third party libraries to bind views and data for better coding, because android recently introduced a new feature for this.</p><p><a href="https://developer.android.com/topic/libraries/data-binding/index.html"><strong>Android Data Binding</strong></a> is one of the most interesting feature recently introduced but still in beta version.</p><p>Android Data Binding exist between app presentation layer and data model layer that holds the information to show.</p><p><em>You can implement android data binding just in few steps.</em></p><p>To integrate binding we need to use Gradle 1.5.0 or above. Let’s update build.gradle file of the project by adding the following string:</p><pre>dependencies {<br>        classpath &#39;com.android.tools.build:gradle:1.5.0&#39;<br>    }</pre><p>To enable data binding in your project edit your <strong>build.gradle</strong>(app level)</p><p>just put this code in android tag:-</p><pre>android{</pre><pre>   ....<br>   dataBinding {<br>       enabled = <strong>true</strong>;<br>   }<br>}</pre><p>Here is the code for build.gradle(app level)</p><p>build.gradle</p><pre>apply <strong>plugin</strong>: <strong>&#39;com.android.application&#39;<br><br></strong>android {<br>    compileSdkVersion 25<br>    buildToolsVersion <strong>&quot;25.0.2&quot;<br>    <em>dataBinding {<br>        enabled = true;<br>    }</em></strong><br>    defaultConfig {<br>        applicationId <strong>&quot;sample.android.databinding&quot;<br>        </strong>minSdkVersion 15<br>        targetSdkVersion 25<br>        versionCode 1<br>        versionName <strong>&quot;1.0&quot;<br>    </strong>}<br>    buildTypes {<br>        release {<br>            minifyEnabled <strong>false<br>            </strong>proguardFiles getDefaultProguardFile(<strong>&#39;proguard-android.txt&#39;</strong>), <strong>&#39;proguard-rules.pro&#39;<br>        </strong>}<br>    }<br>}<br><br>dependencies {<br>    compile <strong>&#39;com.android.support:appcompat-v7:25.3.0&#39;<br></strong>}</pre><p>The next and very important step add &lt;layout &gt; tag as root in your xml.</p><p>activity_mail.xml</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;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;</strong>&gt;<br><br>    &lt;<strong>RelativeLayout<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>        tools:context=&quot;sample.android.databinding.MainActivity&quot;</strong>&gt;<br><br>        &lt;<strong>TextView<br>            android:id=&quot;@+id/textView&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:layout_centerInParent=&quot;true&quot;<br>            android:textSize=&quot;20sp&quot; </strong>/&gt;<br><br>    &lt;/<strong>RelativeLayout</strong>&gt;<br>&lt;/<strong>layout</strong>&gt;</pre><p>Now Android Data Binding library will handle rest. It will generate a binding class with your xml. All the widgets that you have added in your xml with there id’s (android:id=”@+id/textView”) have been added to ActivityMainBinding class.</p><p>Next step go to your main activity (find the code below):-</p><p>MainActivity.java</p><pre><strong>public class </strong>MainActivity <strong>extends </strong>AppCompatActivity {<br>    <strong>private </strong>ActivityMainBinding <strong>activityMainBinding</strong>;<br><br>    @Override<br>    <strong>protected void </strong>onCreate(Bundle savedInstanceState) {<br>        <strong>super</strong>.onCreate(savedInstanceState);<br><br>        <strong>activityMainBinding </strong>= DataBindingUtil<br>                .<em>setContentView</em>(<strong>this</strong>, R.layout.<strong><em>activity_main</em></strong>);<br>        <br>        <strong>activityMainBinding</strong>.<strong>textView<br>                </strong>.setText(<strong>&quot;Android Data Binding&quot;</strong>);<br>    }<br>}</pre><p>Yes just a few line of code and you are ready with <strong>Android Data Binding.</strong></p><p>Thanks for reading this short story about Android Data Binding library.</p><p>We will take this up to the next level with click listeners and declaring variable in xml with the help of Android Data Binding library.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F3e491f%3Fas_embed%3Dtrue&amp;url=https%3A%2F%2Fupscri.be%2F3e491f%2F&amp;image=https%3A%2F%2Fupscri.be%2Fmedia%2Fform.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ae57c1b4448392f65f474b01a127fbf/href">https://medium.com/media/2ae57c1b4448392f65f474b01a127fbf/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6c567c242c9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/lets-implement-android-data-binding-in-your-android-app-d6c567c242c9">Let’s implement Android Data Binding library in your android app: Part-1</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RecyclerView with EndlessScroll and Android Data Binding]]></title>
            <link>https://medium.com/android-news/recyclerview-with-endlessscroll-2c503008522f?source=rss-fc100cb1a78f------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c503008522f</guid>
            <category><![CDATA[android-data-binding]]></category>
            <category><![CDATA[endlesscrollistener]]></category>
            <category><![CDATA[recyclerview]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Aman Shandilya]]></dc:creator>
            <pubDate>Mon, 20 Mar 2017 09:26:27 GMT</pubDate>
            <atom:updated>2017-10-29T17:15:17.086Z</atom:updated>
            <content:encoded><![CDATA[<p>A common application feature is to load automatically more items as the user scrolls through the items (aka infinite scroll). This is done by triggering a request for more data once the user crosses a threshold of remaining items before they’ve hit the end.</p><p>Implementing endless pagination for <a href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html">RecyclerView </a>requires the following steps:</p><ol><li>Copy over the <a href="https://github.com/shandilyaaman/SampleEndlessRecyclerView/blob/master/SampleEndlessRecyclerView/endlessrecyclerview/src/main/java/com/endlessrecyclerview/android/EndlessRecyclerOnScrollListener.java">EndlessRecyclerViewOnScrollListener.java </a>into your application. This is an abstract class.</li><li>Now call addOnScrollListener() on your <a href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html">RecyclerView </a>to enable endless scrolling and pass EndlessRecyclerViewOnScollListener as parameter in addOnScrollListener() and implement onLoadMore() method which would be called when user scroll down bottom in the list.</li><li>In onLoadMore() method load your data using an api or some source from where you are fetching data.</li></ol><p>activity_mail.xml</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;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;</strong>&gt;<br><br>    &lt;<strong>RelativeLayout<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>        tools:context=&quot;com.sample.android.MainActivity&quot;</strong>&gt;<br><br>        &lt;<strong>android.support.v7.widget.RecyclerView<br>            android:id=&quot;@+id/recycler_view&quot;<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            android:layout_above=&quot;@+id/item_progress_bar&quot;<br>            android:layout_marginTop=&quot;8dp&quot; </strong>/&gt;<br><br>        &lt;<strong>ProgressBar<br>            android:id=&quot;@+id/item_progress_bar&quot;<br>            android:layout_width=&quot;30dp&quot;<br>            android:layout_height=&quot;30dp&quot;<br>            android:layout_alignParentBottom=&quot;true&quot;<br>            android:layout_centerHorizontal=&quot;true&quot;<br>            android:elevation=&quot;@dimen/padding_5&quot;<br>            android:padding=&quot;@dimen/padding_5&quot;<br>            android:visibility=&quot;gone&quot; </strong>/&gt;<br>    &lt;/<strong>RelativeLayout</strong>&gt;<br>&lt;/<strong>layout</strong>&gt;</pre><p>MainActivity.java</p><pre><strong>public class </strong>MainActivity <strong>extends </strong>AppCompatActivity {<br>    <strong>private </strong>List&lt;String&gt; <strong>mStringList</strong>;<br>    <strong>private int mLoadedItems </strong>= 0;<br>    <strong>private </strong>SampleAdapter <strong>mSampleAdapter</strong>;<br>    <strong>private </strong>ActivityMainBinding <strong>mActivityMainBinding</strong>;<br><br>    @Override<br>    <strong>protected void </strong>onCreate(Bundle savedInstanceState) {<br>        <strong>super</strong>.onCreate(savedInstanceState);<br><br>        <strong>mActivityMainBinding </strong>= DataBindingUtil.<em>setContentView</em>(<strong>this</strong>, R.layout.<strong><em>activity_main</em></strong>);<br><br>        LinearLayoutManager linearLayoutManager = <strong>new </strong>LinearLayoutManager(MainActivity.<strong>this</strong>);<br>        linearLayoutManager.setOrientation(LinearLayoutManager.<strong><em>VERTICAL</em></strong>);<br>        <strong>mActivityMainBinding</strong>.<strong>recyclerView</strong>.setLayoutManager(linearLayoutManager);<br>        <strong>mActivityMainBinding</strong>.<strong>recyclerView</strong>.addItemDecoration(<strong>new </strong>DividerItemDecoration(MainActivity.<strong>this</strong>, DividerItemDecoration.<strong><em>VERTICAL</em></strong>));<br>        <strong>mStringList </strong>= <strong>new </strong>ArrayList&lt;&gt;();<br>        <strong>mSampleAdapter </strong>= <strong>new </strong>SampleAdapter(<strong>mStringList</strong>);<br>        <strong>mActivityMainBinding</strong>.<strong>recyclerView</strong>.setAdapter(<strong>mSampleAdapter</strong>);<br>        addDataToList();<br><br>        <strong>mActivityMainBinding</strong>.<strong>recyclerView</strong>.addOnScrollListener(<strong>new </strong>EndlessRecyclerViewOnScrollListener(linearLayoutManager) {<br>            @Override<br>            <strong>public void </strong>onLoadMore() {<br>                addDataToList();<br>            }<br>        });<br>    }<br><br>    <strong>private void </strong>addDataToList() {<br>        <strong>mActivityMainBinding</strong>.<strong>itemProgressBar</strong>.setVisibility(View.<strong><em>VISIBLE</em></strong>);<br>        <strong>new </strong>Handler().postDelayed(<strong>new </strong>Runnable() {<br>            @Override<br>            <strong>public void </strong>run() {<br>                <strong>for </strong>(<strong>int </strong>i = 0; i &lt;= 30; i++) {<br>                    <strong>mStringList</strong>.add(<strong>&quot;SampleText : &quot; </strong>+ <strong>mLoadedItems</strong>);<br>                    <strong>mLoadedItems</strong>++;<br>                }<br>                <strong>mSampleAdapter</strong>.notifyDataSetChanged();<br>                <strong>mActivityMainBinding</strong>.<strong>itemProgressBar</strong>.setVisibility(View.<strong><em>GONE</em></strong>);<br>            }<br>        }, 1500);<br><br>    }<br>}</pre><p>SampleAdapter.java</p><pre><strong>public class </strong>SampleAdapter <strong>extends </strong>RecyclerView.Adapter&lt;SampleAdapter.ViewHolder&gt; {<br>    <strong>private </strong>List&lt;String&gt; <strong>myList</strong>;<br><br>    <strong>public </strong>SampleAdapter(List&lt;String&gt; myList) {<br>        <strong>this</strong>.<strong>myList </strong>= myList;<br>    }<br><br>    @Override<br>    <strong>public </strong>ViewHolder onCreateViewHolder(ViewGroup parent, <strong>int </strong>viewType) {<br>        View view = LayoutInflater.<em>from</em>(parent.getContext()).inflate(R.layout.<strong><em>item_my_list</em></strong>, parent, <strong>false</strong>);<br>        <strong>return new </strong>ViewHolder(view);<br>    }<br><br>    @Override<br>    <strong>public void </strong>onBindViewHolder(ViewHolder holder, <strong>int </strong>position) {<br>        holder.<strong>textView</strong>.setText(<strong>myList</strong>.get(position));<br>    }<br><br>    @Override<br>    <strong>public int </strong>getItemCount() {<br>        <strong>return myList</strong>.size();<br>    }<br><br>    <strong>static class </strong>ViewHolder <strong>extends </strong>RecyclerView.ViewHolder {<br>        AppCompatTextView <strong>textView</strong>;<br><br>        ViewHolder(View itemView) {<br>            <strong>super</strong>(itemView);<br>            <strong>textView </strong>= (AppCompatTextView) itemView.findViewById(R.id.<strong><em>text1</em></strong>);<br>        }<br>    }<br>}</pre><p>item_my_list.xml</p><pre><em>&lt;?</em><strong>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</strong><em>?&gt;<br></em>&lt;<strong>android.support.v7.widget.AppCompatTextView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    android:id=&quot;@+id/text1&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    android:padding=&quot;@dimen/padding_10&quot;<br>    android:text=&quot;@string/app_name&quot;<br>    android:textColor=&quot;@android:color/black&quot;</strong>&gt;<br><br>&lt;/<strong>android.support.v7.widget.AppCompatTextView</strong>&gt;</pre><h4>Find the sample code <a href="https://github.com/shandilyaaman/SampleEndlessRecyclerView">here</a></h4><p><strong>To reset the state when reload the data from initial</strong></p><p>to reset the state of endless recyclerview listener you just need to set previousTotal variable to 0.</p><p>endlessRecyclerListener.mProviousTotal = 0;</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F3e491f%3Fas_embed%3Dtrue&amp;url=https%3A%2F%2Fupscri.be%2F3e491f%2F&amp;image=https%3A%2F%2Fupscri.be%2Fmedia%2Fform.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ae57c1b4448392f65f474b01a127fbf/href">https://medium.com/media/2ae57c1b4448392f65f474b01a127fbf/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c503008522f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/recyclerview-with-endlessscroll-2c503008522f">RecyclerView with EndlessScroll and Android Data Binding</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>