<?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 Furkan Ozcan on Medium]]></title>
        <description><![CDATA[Stories by Furkan Ozcan on Medium]]></description>
        <link>https://medium.com/@ozcanfurkan?source=rss-65792e75cc83------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ICJ-sq9PvsDdjXxRVxgkPg.jpeg</url>
            <title>Stories by Furkan Ozcan on Medium</title>
            <link>https://medium.com/@ozcanfurkan?source=rss-65792e75cc83------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 15:06:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ozcanfurkan/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[Pattern lock screen on Android using custom view]]></title>
            <link>https://ozcanfurkan.medium.com/pattern-lock-screen-on-android-using-custom-view-8b87374c1362?source=rss-65792e75cc83------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b87374c1362</guid>
            <category><![CDATA[canvas]]></category>
            <category><![CDATA[custom-view-android]]></category>
            <category><![CDATA[dagger-hilt]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-security]]></category>
            <dc:creator><![CDATA[Furkan Ozcan]]></dc:creator>
            <pubDate>Tue, 04 May 2021 12:55:37 GMT</pubDate>
            <atom:updated>2021-05-07T20:30:35.551Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6PQ_R0mySz_d_qOT34oqvQ.png" /></figure><p>This is my first article about using custom view. In this article, we’ll write customizable pattern lock view and use in an application. Everyone using android has experienced the pattern lock screen. Pattern lock helps secure devices such as mobile phones or tablets. It is preferred by many users against PIN codes or text passwords. It looks like below;</p><figure><img alt="android pattern lock screen" src="https://cdn-images-1.medium.com/max/200/1*qU5Rmyk8WCvz5cyoB6CIjQ.gif" /><figcaption>pattern lock screen</figcaption></figure><p>Creating pattern password scenario has two stages. First we need to connect <strong>at least 4 dots.</strong> Then apply this pattern again on the second screen. These two patterns must be same. Finally, our pattern password is ready to use.</p><h3>Adding Dot View</h3><p>We create <strong><em>DotView</em></strong> class extends from <strong><em>AppCompatImageView</em></strong>. This class helps us customize the dots. Also each dot has a key like number, alphabetic or any string value. After pattern drawn these keys combined.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55a0f9896ea6db1c441219972bddaa3a/href">https://medium.com/media/55a0f9896ea6db1c441219972bddaa3a/href</a></iframe><p>And create <strong><em>Dot </em></strong>data class for holding some information about a dot.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e21da9da5b9bfa3a79956ec9b5bf086f/href">https://medium.com/media/e21da9da5b9bfa3a79956ec9b5bf086f/href</a></iframe><h3>Adding Pattern Lock View</h3><h4>Define Custom Attributes</h4><p>Can you want to specify your view in the XML ? If your answer is yes, you must define custom attributes for your view in a <strong><em>&lt;declare-styleable&gt;</em></strong> resource element. It’s customary to put these resources into a <strong>res/values/attrs.xml</strong> file. Generally, the name of the resource element same as the name of the your view.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ecabe2db450d1a65c75544ffaeda67b/href">https://medium.com/media/3ecabe2db450d1a65c75544ffaeda67b/href</a></iframe><p>Before begin to create <strong>PatternLockView</strong> class, let’s look some terms and methods.</p><h4>1- Canvas</h4><p>When we want to draw something on Android view, we use a canvas object. Thus, we can draw 2D graphics such as shapes (rectangle, arcs, paths styled etc.), styled text.</p><h4>2- Paint</h4><p>When creating a custom view class, we can think ourselves as painter. One of the painter tools is a brush. Through <strong>Paint</strong> class we have a brush. This class has some properties like color, style etc.</p><h4>3- Overriding onDraw</h4><p>The <strong>onDraw</strong> method provides us the <strong>Canvas</strong> object that we can make some drawing. We invoke <strong>invalidate</strong> (UI thread) or <strong>postInvalidate</strong> ( non-UI thread) methods then the <strong>onDraw</strong> will be called.</p><h4>4- Overriding onTouchEvent</h4><p>The <strong>onTouchEvent </strong>method<strong> </strong>provides us the <strong>MotionEvent</strong> object. The object is used to catch movement (mouse, pen, finger, trackball) events. This object returns action codes such as <strong>ACTION_DOWN</strong>, <strong>ACTION_UP</strong> and <strong>ACTION_MOVE</strong> etc. For example, when the user first touches the screen, the <strong>onTouchEvent</strong> method gives a <strong>MotionEvent</strong> object with <strong>ACTION_DOWN</strong> code.</p><h4>Anymore let’s start get our hands dirty step-by-step</h4><p><strong>Step 1:</strong> Create a class called by <strong>PatternLockView </strong>extends from LinearLayout.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0adf8a81c3ee0aa6de2ae33062f992f2/href">https://medium.com/media/0adf8a81c3ee0aa6de2ae33062f992f2/href</a></iframe><p><strong>Step 2:</strong> In init block, obtain styled attributes, set view properties and add dots for 3x3 matrix view. For obtain this matrix view, we create a <strong>drawPatternView</strong> method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f51c7cfb1b9c8bfec898bc432ed46143/href">https://medium.com/media/f51c7cfb1b9c8bfec898bc432ed46143/href</a></iframe><blockquote>We have to call setWillNotDraw(false), to make the onDraw of your subclass of ViewGroup being called.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/32b66afa7fc951aa9e0e9a37e1844db0/href">https://medium.com/media/32b66afa7fc951aa9e0e9a37e1844db0/href</a></iframe><p>This method with parameters helps us creating rows and columns. <strong>createRow</strong> method add linear layout as view to parent. The other method <strong>createColumn</strong> create <strong>DotView</strong> and add it to created the row.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a466d0cec9f836f883c217d1ee1f0316/href">https://medium.com/media/a466d0cec9f836f883c217d1ee1f0316/href</a></iframe><p><strong>Step 3:</strong> Override the onDraw as follows</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c16ea37b4b0a191db7a79d7f5778ba05/href">https://medium.com/media/c16ea37b4b0a191db7a79d7f5778ba05/href</a></iframe><p>Let’s look into this method closely;</p><p>After our class called init block, it trigger this method. Also when every invalidate function invoked then onDraw will be called. Our the matrix is prepared in init block by <strong>drawPatternView</strong> function. Anymore, we can connect dots with lines.</p><p>The <strong>addInitialData</strong> takes the position points, indexes, and key of each dot. Thus, we will be able to access information about the dots.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d2f246541cda5cb1c293c596a2fb8d3/href">https://medium.com/media/7d2f246541cda5cb1c293c596a2fb8d3/href</a></iframe><p>The <strong>updateViewState</strong> method with state parameter update the color of the marked dots as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a94de3dec7b61b5bd0f5eecb0e4cc19c/href">https://medium.com/media/a94de3dec7b61b5bd0f5eecb0e4cc19c/href</a></iframe><p>Last our important method <strong>drawLine</strong> helps drawing line on canvas. Firstly, it connect marked dots with drawing line. After that, it draw line from last marked dot to every touched points.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b0bc4e33396488b43a671c2f9e8ea47/href">https://medium.com/media/3b0bc4e33396488b43a671c2f9e8ea47/href</a></iframe><p>The output is as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*QQgFdxepulkPcf80Wkglxw.png" /></figure><p><strong>Step 4:</strong> Override the <strong>onTouchEvent</strong> to catch movement events</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f944fa4ded070849aec339f01c090ac4/href">https://medium.com/media/f944fa4ded070849aec339f01c090ac4/href</a></iframe><p>Lets we handle this method’s action codes with rules;</p><p><strong>1- MotionEvent.ACTION_DOWN</strong></p><p>When the user first touches the screen, the method gives a this code with touched location points. We have to touch any dots so we can start draw line.</p><p><strong>2- MotionEvent.ACTION_UP</strong></p><p>When the user leave touches, we take this code. On stage 1, we have to connect at least 4 dots. In this case, we can update our state as Success. On stage 2, two passwords have to match for Success state. Otherwise, our state always is Error.</p><p><strong>3- MotionEvent.ACTION_MOVE</strong></p><p>When we move our finger over the screen, callback is return it. If our state is <strong>STARTED</strong> and marked dot list size is not equal to <strong>maxCount</strong>, we can invoke invalidate function.</p><p>Here is the output for success state of stage 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*33aKnT7TBbhUkCDsR-bC_A.png" /></figure><p>Finally, we add <strong>PatternLockView</strong> to our xml as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e06c22c805beab092462b6d70e102cd2/href">https://medium.com/media/e06c22c805beab092462b6d70e102cd2/href</a></iframe><p>Checkout the complete code gist at the <a href="https://gist.github.com/furkanozcan/7e72a14acd9452a2be77a8a7e746aced"><strong>link</strong></a><strong>.</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FZhtOYz9LkaE%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DZhtOYz9LkaE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FZhtOYz9LkaE%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f59470c129fe76c63e8a612f1917ff0f/href">https://medium.com/media/f59470c129fe76c63e8a612f1917ff0f/href</a></iframe><p>If you want to know more about creating a custom view, here is the <a href="https://developer.android.com/guide/topics/ui/custom-components"><strong>documentation</strong></a>.</p><p>Thank you for reading. You can find this example on <a href="https://github.com/furkanozcan/pattern-lock"><strong>github</strong></a> link below.</p><p><a href="https://github.com/furkanozcan/pattern-lock">furkanozcan/pattern-lock</a></p><p>Happy Coding :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b87374c1362" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>