<?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 Sachin Arora on Medium]]></title>
        <description><![CDATA[Stories by Sachin Arora on Medium]]></description>
        <link>https://medium.com/@sachin0691?source=rss-60a35e4174eb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ypcww0lIZ7RonXTXoNqlrw.jpeg</url>
            <title>Stories by Sachin Arora on Medium</title>
            <link>https://medium.com/@sachin0691?source=rss-60a35e4174eb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 15:36:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sachin0691/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[Spot the difference- extrapolating x, y coordinates correctly in different density android phones]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@sachin0691/spot-the-difference-extrapolating-x-y-coordinates-correctly-in-different-density-android-phones-2f8a270dfa54?source=rss-60a35e4174eb------2"><img src="https://cdn-images-1.medium.com/max/1280/1*pR0jHgUMfYP2bAzKVtiSsQ.jpeg" width="1280"></a></p><p class="medium-feed-snippet">Recently, I worked on a problem in which I had to mark certain x,y coordinate on an ImageView&#xA0;. Consider this problem similar to solving&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@sachin0691/spot-the-difference-extrapolating-x-y-coordinates-correctly-in-different-density-android-phones-2f8a270dfa54?source=rss-60a35e4174eb------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@sachin0691/spot-the-difference-extrapolating-x-y-coordinates-correctly-in-different-density-android-phones-2f8a270dfa54?source=rss-60a35e4174eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f8a270dfa54</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[spot-the-difference]]></category>
            <category><![CDATA[custom-view-android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[extrapolation]]></category>
            <dc:creator><![CDATA[Sachin Arora]]></dc:creator>
            <pubDate>Fri, 10 Jul 2020 05:28:16 GMT</pubDate>
            <atom:updated>2020-07-10T05:28:16.417Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Learning Custom Views in Android]]></title>
            <link>https://medium.com/@sachin0691/learning-custom-views-in-android-25304c2a5289?source=rss-60a35e4174eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/25304c2a5289</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[custom-view-android]]></category>
            <dc:creator><![CDATA[Sachin Arora]]></dc:creator>
            <pubDate>Mon, 07 Oct 2019 05:21:58 GMT</pubDate>
            <atom:updated>2019-10-13T14:59:40.801Z</atom:updated>
            <content:encoded><![CDATA[<p>Many times it happens that you need a views which do not come with android platform. There can be different reasons for this like:</p><ul><li>Designers wanting to go a little funky</li><li>A special interaction with the views</li><li>Reusability</li><li>Performance Optimization</li></ul><p>All the views in android platform extend fromView class. We generally use these views either programmatically or through xml files when the inflation of the view happens. To create our own views as per the need, the approach falls under two categories:</p><ul><li><strong>Custom View</strong>- In this we extend from any other the existing View class (directly or indirectly). Like Button extends from TextView . ImageView extends from View</li><li><strong>Compound View</strong>- In this we combine multiple views together and want to use it as a single entity</li></ul><p>Android follows three operation for drawing views:</p><ul><li>Measure- onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) to measure the size of the view. Then</li><li>Layout- onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) , for positioning the views in the view hierarchy</li><li>Drawing- onDraw(canvas: Canvas) After measurement and positioning is done. onDraw is called to finally draw the views on the canvas</li></ul><p>All these operations are carried out in a Depth-First manner. Meaning the parent is drawn first and then the children are drawn</p><h3>Starting with CustomView</h3><p>Let’s create a custom view which is actually a rectangle of the color chosen by user.</p><p>Create a class with name MyCustomView which extends from View . Now to use this view we need to provide constructor. There are different constructors available</p><pre>class MyCustomView: View {<br>   // constructor 1<br>   <strong>constructor</strong>(context: Context) : super(context)<br>   // constructor 2<br>   <strong>constructor</strong>(context: Context, attributeSet: AttributeSet?):              super(context, attributeSet)<br>   <strong>// </strong>constructor 3<strong><br>   constructor</strong>(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int): super(context, attributeSet, defStyleAttr)<br>}</pre><p>constructor1 can be used when we are programmatically defining a view. When we are using the views from our xml we need constructor2 or constructor3 . When using the custom view in xml we need to use the fully qualified name.</p><p>In your layout file, assuming for this exercise you are using the activity_layout which is created by default, paste the below mentioned code.</p><pre><em>&lt;?</em><strong>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</strong><em>?&gt;<br></em>&lt;<strong>LinearLayout <br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot;</strong>&gt;<br><br>    &lt;<strong>com.example.MyCustomView<br>        android:id=&quot;@+id/myCustomView&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>        </strong>/&gt;<br>&lt;/<strong>LinearLayout</strong>&gt;</pre><p>Now we need to use this layout file in the Activity to call this view. If you run the application now, you wouldn’t see anything as for now we have just defined the view but we are yet to tell how the view looks like.</p><p>We will be drawing a rectangle figure in the view. We can use the onDraw(canvas: Canvas) method which is present in the view class. We need to put the following code in the OnDraw method.</p><pre><strong>override fun onDraw(canvas: Canvas?) {<br>   super</strong>.onDraw(canvas)<br>   <strong>val </strong>rect = Rect(0, 0, 50, 50)<br>   <strong>val </strong>paint = Paint()<br>   paint.<em>color </em>= Color.<em>BLUE<br>   </em>paint.<em>style </em>= Paint.Style.<strong>FILL<br>   </strong>canvas!!.drawRect(rect, paint)<br>}</pre><p>canvas comes with a method drawRect(rect: Rect, paint: Paint) to draw a Rectangle. We created a rect object which starts from the very top left of the screen and has a width of 50and height of 50. Consider the screen/canvas a co-ordinate system, our rectangle starts from the top left which is (0,0) and expands till the mentioned bottom right coordinate</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*HE0j0cojWNgRZMuibIDpwg.jpeg" /><figcaption>Co-ordinate System for Android Screen</figcaption></figure><p>Now let’s try to provide the styling like size and colour of the rectangle from the xml file .</p><p>For this we need to define a declare-styleable in attrs.xml . This stylable would be used to define the custom attributes available on our view. For this case let us provide two attributes, one for the color of the square and the other for the size. The benefit of these styleable is that it could be used in the xml file as the property on the view. The declaration of the styleable would like below:</p><pre>&lt;<strong>declare-styleable</strong> <strong>name</strong>=&quot;MyCustomView&quot;&gt;<br>    &lt;<strong>attr</strong> <strong>name</strong>=&quot;square_color&quot; <strong>format</strong>=&quot;color&quot; /&gt;<br>    &lt;<strong>attr</strong> <strong>name</strong>=&quot;size&quot; <strong>format</strong>=&quot;dimension&quot; /&gt;<br>&lt;/<strong>declare-styleable</strong>&gt;</pre><p>and these can use along with the view like in the layout xml like</p><pre><em>&lt;?</em>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;<em>?&gt;<br></em>&lt;<strong>LinearLayout</strong>   xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    android:orientation=&quot;vertical&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;&gt;<br><br>    &lt;<strong>com.example.mycustomview.MyCustomView</strong><br>        android:layout_height=&quot;wrap_content&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:id=&quot;@+id/myCustomView&quot;<br>        app:square_size=&quot;100dp&quot;<br>        app:square_color=&quot;@android:color/holo_green_light&quot;<br>        /&gt;<br>&lt;/<strong>LinearLayout&gt;</strong></pre><p>Now let’s move to the code to see how these attributes are identified in MyCustomVie class. Remember the different constructors which were mentioned above ? If we look at the Attributes parameter, it corresponds to the custom attributes which are defined at the view level. So in this case the attributeSet will contain attributes for square_size and square_color . We can define a method which takes attributes as an input and store these values at the class level. We can then pass those values to the onDraw method.</p><pre><strong>private fun </strong>init(attributeSet: AttributeSet?) {<br>    attributeSet?.<em>let</em><strong>{<br>        val </strong>typedArray = <em>context</em>.obtainStyledAttributes(attributeSet, R.styleable.<em>MyCustomView</em>)<br>        <strong>squareColor </strong>= typedArray.getColor(R.styleable.<em>MyCustomView_square_color</em>, Color.<em>RED</em>)<br>        <strong>squareSize </strong>= typedArray.getDimensionPixelSize(R.styleable.<em>MyCustomView_size</em>, 50)<br><br>    <strong>}<br></strong>}</pre><p>Now the onDraw method can use the class member variables squareColor and squareSize and finally the onDraw() method would look like</p><pre><strong>override fun </strong>onDraw(canvas: Canvas?) {<br>    <strong>super</strong>.onDraw(canvas)<br>    <strong>val </strong>paint = Paint()<br>    paint.<em>color </em>= <strong>squareColor<br>    </strong>paint.<em>style </em>= Paint.Style.<strong>FILL<br>    val </strong>rect = Rect(0, 0, <strong>squareSize</strong>, <strong>squareSize</strong>)<br>    canvas?.let{ it.drawRect(rect, paint)}<br>}</pre><p>On running the app you should be able to see the view as below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*40PPRz_gqYSDauOoksxzFg.png" /></figure><p>For the next sections i will be adding how to interact with view, change color and Add animations. Till then happy learning..</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25304c2a5289" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Starting with Lambdas, can be confusing ? o_O]]></title>
            <link>https://medium.com/@sachin0691/starting-with-lambdas-can-be-confusing-o-o-39de11375350?source=rss-60a35e4174eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/39de11375350</guid>
            <category><![CDATA[lambda-function]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[higher-order-function]]></category>
            <category><![CDATA[lambda-expressions]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Sachin Arora]]></dc:creator>
            <pubDate>Fri, 07 Dec 2018 17:14:00 GMT</pubDate>
            <atom:updated>2018-12-11T04:03:20.058Z</atom:updated>
            <content:encoded><![CDATA[<h3>Starting with Lambdas, can be confusing ? o_O</h3><p>Hi, This Article will help you getting started with the Lambdas in Java.</p><p>If you are a java programmer and just getting started with lambdas I can understand your pain. Lambdas or Higher Order functions definitely makes our life easier but sometimes they can get confusing, especially when you are just starting to dip your toe in this area.</p><p>First thing we need to know about the lambdas is that they are backed by functional interfaces. Now what on earth is a functional interface ?. Calm down, we are going to cover this together and i hope by the end of the article you be able to move up your lambda game.</p><h4><strong>Functional Interfaces — Interfaces with only one abstract method</strong></h4><p>examples: Runnable, ActionListener etc.</p><p>Lets jump start our journey of Lambdas here :</p><p><strong>How to Start :</strong></p><pre>interface Conversion { void convert(String text);}</pre><p>The above interface has one abstract method convert . It takes in a String and doesn’t return anything. Which means it allows us to still define the behaviour when we are using this.</p><pre>Class Lambdas {</pre><pre>public static void main(String[] args){<br>  Conversion conversion = (text) -&gt; { <br>           // This lambda takes in text and does something        <br>    }<br> }</pre><pre>}</pre><p>In the above class the lambda allows to take in input string and allows us to define the behaviour in { } . Note the text is not a defined variable in the class. It just acts a a place holder for the String which would be passed to the lambda and allow access to it in the { } block.</p><h4><strong>Defining Lambda :</strong></h4><p>Now let’s define a functionality in the lambda block.</p><pre>Conversion conversion = (text) -&gt; { <br>           System.out.println(&quot;You passed&quot;+ text + &quot; as an input&quot;);       <br>    }</pre><p>Now the Lambda defined above takes in an input and prints the passed input. with some extra text provided in the lambda block { }</p><h4><strong>How to use this ?</strong></h4><pre>Class Lambdas {</pre><pre>public static void main(String[] args){<br>  Conversion conversion = (text) -&gt; { <br>           System.out.println(&quot;You passed&quot;+ text + &quot; as an input&quot;);        <br>    }<br> }<br>  conversion.convert(&quot;Learning Lambdas&quot;);</pre><pre>}</pre><p>The output of this will print the “You passed Learning Lambdas as an input”</p><h4><strong>Higher Order functions</strong></h4><p>when we pass functions or returns functions from other functions we can say we are dealing with Higher Order Functions.</p><p>Let’s say you have a requirement to calculate the area or perimeter of a square. Now instead of defining two functions which does this separately. We can define a high order function which takes in the side of the square as an input and also takes in a lambda/function as an other input, which leaves it to the caller to define the behaviour i.e to calculate or perimeter</p><p>Step 1: Defining the Interface / Finding an existing Interface as per requirement</p><pre>interface Calculate {<br>    int compute(int side);<br>}</pre><p>Calculate Interface defines a method compute which take in an int and returns an int . We can use this to our leverage to define a lambda which takes in a number and do some calculation, it can be whatever we want and then return an int at the end.</p><h4>Step 2 : Defining a lambda</h4><pre>int sideOfSquare = 5 ;<br>Calculate calculateArea = (side) -&gt; {<br>    return side * side;<br>};<br><br>Calculate calculatePerimeter = (side) -&gt; {<br>    return 4 * side;<br>};</pre><p>now these lambdas take in side and perform area or side calculations. Again don’t confuse with `side` in the lambdas some already defined member variable of class or method. It signifies the input which will be passed when we invoke the lambda.</p><h4>Step 3 : Defining a Higher Order function</h4><p>Higher Order function is a function which either takes as an input another function /lambda or return any of those. Let’s define a function which allows us to take in a lambda</p><pre>int calculate(int side, Calculate cal) {<br>    // return an Int<br>}</pre><h4>Step: 4 Invoking the lambda</h4><p>Now we have the calculate method which takes side and a lambda as an input and we can now invoke the lambda and return the calculated results</p><pre>int calculate(int side, Calculate cal) {<br>    return cal.compute(side);<br>}</pre><p>the statement cal.compute(side) is the statement where the lambda is invoked.</p><p>This completes the basic introduction to lambdas</p><p>Hope this article will help you in getting started with Lambdas. There is a lot more which we can do with Lambdas.</p><p>Keep Learning and Keep Exploring :-)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39de11375350" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>