<?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[Kotlindroid - Medium]]></title>
        <description><![CDATA[Learn Android with the use of Kotlin and more than code - Medium]]></description>
        <link>https://medium.com/android-kotlin?source=rss----6d1f56f450fb---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Kotlindroid - Medium</title>
            <link>https://medium.com/android-kotlin?source=rss----6d1f56f450fb---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 05:26:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/android-kotlin" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using Scalable DP and SP in your design]]></title>
            <link>https://medium.com/android-kotlin/using-scalable-dp-and-sp-in-your-design-879e1a6c4489?source=rss----6d1f56f450fb---4</link>
            <guid isPermaLink="false">https://medium.com/p/879e1a6c4489</guid>
            <category><![CDATA[responsive-design]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[ui-ux-design]]></category>
            <dc:creator><![CDATA[Chintan Patel]]></dc:creator>
            <pubDate>Fri, 06 Mar 2020 17:04:58 GMT</pubDate>
            <atom:updated>2019-09-17T18:06:38.569Z</atom:updated>
            <content:encoded><![CDATA[<p>Save your time to build responsive deigned app in android</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*Pt5r8IwQ0ExtCPL_-A4x4g.png" /><figcaption>Responsive App design in Android</figcaption></figure><p>I know most and beginners/intermediate android developers are not so used to build responsive designs and it’s so complex task to define different values for different dimensional devices as android has vast variety of devices which differs in size and it’s screen resolution rather than having known size like iPhone devices.</p><p>I’m also an android developer who knew about to build responsive designed app by defining different values in different dimens.xml in required dimensional folders.</p><p>When I started it, I had an interest to do that thing but After working on more than one project, I felt that it is so time wasting to define it every time when I start new project. Might you also have felt the same if you have worked on that.</p><p>But I was lucky that one of my team member <a href="https://medium.com/u/ea8b135cb66b">Shivamssoni</a> who was beginner at that time but he found and explored a library which provides a way to build responsive designed app using Scalable DP and SP.</p><p>I saved most of 60% of designing time using this library.</p><h3>SDP (Scalable DP)</h3><p><a href="https://github.com/intuit/sdp">GitHub - intuit/sdp: An Android SDK that provides a new size unit - sdp (scalable dp). This size unit scales with the screen size.</a></p><p>SDP library is helpful to define scalable size unit instead of just defining dp as unit. Example is</p><p>&lt;ImageView<br> android:src = &quot;@drawable/ic_image&quot;<br> android:layout_width = &quot;@dimen/_50sdp&quot;<br> android:layout_height = &quot;@dimen/_50sdp&quot; /&gt;</p><h3>SSP (Scalable SP)</h3><p><a href="https://github.com/intuit/ssp">GitHub - intuit/ssp: Variant of sdp project based on the sp size unit.</a></p><p>SSP library is helpful to define scalable size unit for defining text related dimens instead of just defining sp as unit. Example is</p><p>&lt;TextView<br> android:text = &quot;Hello SSP&quot;<br> android:textSize = &quot;@dimen/_16ssp&quot; /&gt;</p><blockquote>If you are providing normal sp as 16sp to your text size then in ssp unit you can give 14ssp which will make your design scalable in any device.</blockquote><p>Me and My team members used this library in our projects and saved lot of time and increased our productivity.</p><p>Now its your turn to use this. Let’s enjoy and save your time and energy both.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=879e1a6c4489" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-kotlin/using-scalable-dp-and-sp-in-your-design-879e1a6c4489">Using Scalable DP and SP in your design</a> was originally published in <a href="https://medium.com/android-kotlin">Kotlindroid</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handle Android Permissions Easily Even User Denied it Permanently]]></title>
            <link>https://medium.com/android-kotlin/handle-android-permissions-easily-even-user-denied-it-permanently-73c55f804da1?source=rss----6d1f56f450fb---4</link>
            <guid isPermaLink="false">https://medium.com/p/73c55f804da1</guid>
            <category><![CDATA[marshmallow]]></category>
            <category><![CDATA[app-permissions]]></category>
            <category><![CDATA[permission]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Chintan Patel]]></dc:creator>
            <pubDate>Thu, 03 Oct 2019 16:36:06 GMT</pubDate>
            <atom:updated>2019-10-03T16:36:06.559Z</atom:updated>
            <content:encoded><![CDATA[<p>Handling dynamic permissions in android in a single line code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0BRQnnNOY8JyYJ4V1r8ang.png" /><figcaption>Android M+ Dynamic Permissions</figcaption></figure><p>Today I am posting this article as I have created android library to check and request any single or multiple permission to get exact status of permission e.g. <strong>Allowed</strong>, <strong>Not requested yet and to be requested/Denied temporarily (for once only)</strong> or <strong>Denied permanently.</strong></p><p>I faced an issue in a project with dynamic permissions in Android when I was developing and my issue was to check if permission is denied temporarily or permanently. So as I researched a lot and found very small amount of solutions, hence me and my team thought, many of the developers are facing this issue and so let provide them a permanent solution.</p><p>First we’ll look into how the app permission works in a flow.</p><h3>Flow the Android Permission Works</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*jCXHbViFhgqfH0fWi7PfqQ.png" /><figcaption>P.C.: <a href="https://medium.com/programming-lite">Programming Lite on Medium</a></figcaption></figure><p>As we can see in the image above, there are 3 cases in which permission flow goes.</p><p><strong>Case 1<br></strong>Permission is never requested by app.</p><p><strong>Case 2<br></strong>Permission requested once or more times but user denied it temporarily (Without checking <strong>“Never Ask Again”</strong>)</p><p><strong>Case 3<br></strong>Permission requested and user denied it permanently (By checking <strong>“Never Ask Again”</strong> option)</p><p>Only one function can help us <strong>“shouldShowRequestPermissionRationale()”, </strong>which returns boolean value either true or false on the base of these cases.</p><h4>Case returns TRUE:</h4><ol><li>When app requested permission at least once and user denied it temporarily.</li></ol><h4>Case returns FALSE:</h4><ol><li>App never requested required permission before.</li><li>App requested permission and user denied permission</li></ol><p>So It is easy to detect that user yet not given permission even asked before but it’s tricky to define whether user has denied permission permanently or app never requested permission to user.</p><p>Finding a solution for this, we stored a flag value for each requested permission in a shared preference so that we can get know if permission is requested or not and so can satisfy both the cases when function returns false.</p><p>Then thought if this solution will be needed to implement in each project which will waste the time. So I created a library to handle it very easily.</p><p><a href="https://github.com/Promact/dynamic-permission-handling-android">Promact/dynamic-permission-handling-android</a></p><p>This library will return a response with status as,</p><h4>ALLOWED</h4><p>If you get this as final status for requested permission(s), it means all permission(s) are allowed by user, so can proceed further.</p><h4>DENIED_PERMANENTLY</h4><p>When user has already denied any/all of the requested permission(s), final status will be this as indicating that System will also not show a permission request dialog to the user so need to allow permission explicitly.</p><p><strong>PermissionUtils.askUserToRequestPermissionExplicitly()</strong></p><p>You can call this function to redirect user to allow permission explicitly if user has already denied requested permission permanently.</p><h4>NOT_GIVEN</h4><p>If user has not granted any/all of the requested permission(s), this status will be returned as final and also it will request permission which are needed. So no need to do any extra code to request permission again.</p><p>You can find full implementation guide on this library page which is too much easy for anyone. So Why to wait for anything? Implement it and enjoy.</p><p><a href="https://github.com/Promact/dynamic-permission-handling-android">Promact/dynamic-permission-handling-android</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=73c55f804da1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-kotlin/handle-android-permissions-easily-even-user-denied-it-permanently-73c55f804da1">Handle Android Permissions Easily Even User Denied it Permanently</a> was originally published in <a href="https://medium.com/android-kotlin">Kotlindroid</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Geo-Firestore Query with Native Query]]></title>
            <link>https://medium.com/android-kotlin/geo-firestore-query-with-native-query-bfb0ac1adf0a?source=rss----6d1f56f450fb---4</link>
            <guid isPermaLink="false">https://medium.com/p/bfb0ac1adf0a</guid>
            <category><![CDATA[firestore]]></category>
            <category><![CDATA[query]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[geofire]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Chintan Patel]]></dc:creator>
            <pubDate>Fri, 30 Aug 2019 18:11:23 GMT</pubDate>
            <atom:updated>2019-08-30T18:17:38.568Z</atom:updated>
            <content:encoded><![CDATA[<p>Optimizing query reads, faster the response and feel the code in native</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q9mENzO3ni34HcbfIvZm3A.jpeg" /><figcaption>Geo-Firestore</figcaption></figure><p>After the lot of research and working on Geo-Firestore query to get all the nearest list from the give location within a given distance, I found one which optimize the query calls, faster the response with all accurate results.</p><p>i.e. you are creating an restaurant list app which requires the list which are only located within 10 Km or 10 miles from my current location.</p><p>I hope you already have configured your project with Firebase Firestore SDK.</p><p>Let’s start an implementation of Geo-Firestore library to get ready your code with creating and searching the nearest restaurants.</p><h4><strong>Adding a dependency into your gradle files</strong></h4><p>Now add below line into your build.gradle <em>(project-level)</em>.</p><pre>allprojects {<br>  repositories {<br>	  ...<br>    maven { url &#39;https://jitpack.io&#39; }<br>  }<br>}</pre><p>Then add the library to your build.gradle <em>(app-level)</em></p><pre>dependencies {<br>  implementation &#39;com.github.chintan369:Geo-FireStore-Query:1.1.0&#39;<br>}</pre><p>Now you’ve added a dependency so waiting for what?, Click on <strong>“Sync Now”</strong>.</p><p>Before reading the list of the restaurants, we need to set location of the restaurant which is required and will help to find the required list. So set location first.</p><h4><strong>Setting a location of restaurant</strong></h4><pre>val db = FirebaseFirestore.getInstance()<br><br>val document = db.collection(&quot;restaurants&quot;).document(&quot;punjabi&quot;)<br><br>document.set(data)<br>	.addOnSuccessListener{ _ -&gt;<br>		//Set Location After your document created <br>		document.setLocation(latitude, longitude, fieldName)<br>	}<br>	.addOnFailureListener { exception -&gt;<br>		//Document write failed<br>	}</pre><p>Here, we’ve created one restaurant with document named <strong>punjabi </strong>with the data you have provided. <strong>You must have to call setLocation() after created document successfully.</strong></p><blockquote><strong>setLocation(latitude, longitude, fieldName, useUpdateMethod)</strong></blockquote><blockquote><strong>latitude and longitude (mandatory) —<em> </em></strong><em>location points of restaurant</em><strong><em><br></em>fieldName (optional) —<em> </em></strong><em>in which field name you want to store calculated point of location, if not passed then by default it will be stored in </em><strong>“g”<em><br></em>useUpdateMethod (optional) —<em> </em></strong><em>if you want to use update method or to use set method with merge options.</em></blockquote><p>This will set two fields in your document<br><strong>“g” (or your given fieldName) - </strong>Calculated degree match from given location point<strong><br>“geoLocation” -</strong> given location points in <em>GeoPoint </em>class</p><p>Now you’ve set location for restaurants. You can easily read the nearby list now.</p><h4><strong>Reading Nearby 10 Km restaurants from current location</strong></h4><p>You can read the data in two way as firestore provides in its native query.<br>Either <strong>Real-time snapshots</strong> or <strong>Single one-shot</strong></p><p>Let’s create required objects for current location and distance in which we have to query.</p><pre>val db = FirebaseFirestore.getInstance()<br><br>/*Create two object to pass location and distance for radius*/<br>val centerLocation = Location(centerLatitude, centerLongitude)<br>val distanceForRadius = Distance(10.0, DistanceUnit.KILOMETERS) <br>// or you can set unit as DistanceUnit.MILES</pre><p>Here Location is the android’s Location class, and Distance class with 2 arguments 10.0 as radius and DistanceUnit.KILOMETERS or DisrtanceUnit.MILES as distance unit.<br>Now use this two objects to query the list.</p><p>Now create the GeoQuery class object to build the query as follow:</p><pre>val geoQuery = GeoQuery()<br>		.collection(&quot;restaurants&quot;)<br>		.whereEqualTo(&quot;status&quot;,&quot;approved&quot;)<br>		.whereEqualTo(&quot;country&quot;,&quot;IN&quot;)<br>		.whereNearToLocation(centerLocation, distanceForRadius, fieldName) <br>		.startAfter(lastDocument) //optinal (for pagination)<br>		.limit(10) // If you requires only 10 data per query</pre><blockquote><strong>whereNearToLocation(centerLocation, distanceForRadius, fieldName)<br></strong><em>This function will be responsible to find the near-by list items where<br></em><strong>centerLocation</strong> <em>— object of current location from which near-by list is to be fetched<br></em><strong>distanceForRadius <em>—</em></strong><em> object of distance with radius and distance unit<br></em><strong>fieldName (optional) <em>—</em></strong><em> pass the same if you have passed in </em><strong>setLocation()</strong><em>’s fieldName</em></blockquote><p><strong>Let’s Go with Real-time snapshots first:</strong></p><pre>geoQuery.addSnapshotListener { firebaseFirestoreException, addedOrModifiedDataList, removedList  -&gt;<br>	...<br>	//Do your stuff here<br>	//If exception occurs, firebaseFirestoreException will not<br>          be null else<br>	//In addedOrModifiedDataList, you will get all data that<br>          falls within distance and given query <br>          as either ADDED for first time in query or has MODIFIED as<br>          was in the query from first.<br>	//In removedList, you will get data which is not relevent to<br>          your query or out of distance<br>}</pre><p><strong>You can listen One-Shot data as:</strong></p><pre>geoQuery.get()<br>	.addOnCompleteListener { firebaseFirestoreException, addedOrModifiedDataList, removedList  -&gt;<br>		...<br>		//Do your stuff here<br>	}</pre><p><strong>OR</strong></p><pre>geoQuery.get()<br>	.addOnSuccessListener { addedOrModifiedDataList, removedList  -&gt;<br>		...<br>		//Do your stuff here<br>	}<br>	.addOnFailureListener { exception -&gt;<br>		//If any exception occured<br>	}</pre><p>That’s all. You have implemented the Geo-Query in your firestore DB successfully with a single query read and faster response time.</p><h3><strong>Advantages Over Old Geo-Firestore Query</strong></h3><p><a href="https://github.com/firebase/geofire-android">Old Geo-Firestore SDK for Android</a></p><ul><li>Reducing the query calls <strong><em>from 3–4 query calls</em></strong> in a single query <strong><em>to just 1 query call</em></strong></li><li>Faster response due to reducing query call</li><li>Accurate Data for the given radius of distance</li><li><strong>Feel Code like Native Firestore Query implementation</strong></li><li>Real-time and One-shot both supported in the query</li></ul><h3><strong>Disadvantages due to firestore incapabilities</strong></h3><ul><li>Can not be able to sort data on multiple fields as firestore not supporting multiple order-By sorting. So you can Either make pagination on geo-based value the filedName given by you (or can default) Or getting data all at once and sort it at your local level on the other filed which you want e.g. createdDateTime or any other</li></ul><p>So What are you thinking now? Open your IDE and implement the same.</p><p><a href="https://github.com/chintan369/Geo-FireStore-Query">chintan369/Geo-FireStore-Query</a></p><p>Also, Don’t forgot to claps for this article if you found it worthwhile.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bfb0ac1adf0a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-kotlin/geo-firestore-query-with-native-query-bfb0ac1adf0a">Geo-Firestore Query with Native Query</a> was originally published in <a href="https://medium.com/android-kotlin">Kotlindroid</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Material Events on Calendar is now on Git]]></title>
            <link>https://medium.com/android-kotlin/material-events-on-calendar-is-now-on-git-a217b6e1e0bd?source=rss----6d1f56f450fb---4</link>
            <guid isPermaLink="false">https://medium.com/p/a217b6e1e0bd</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[calendar]]></category>
            <category><![CDATA[event-planning]]></category>
            <category><![CDATA[material-design]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Chintan Patel]]></dc:creator>
            <pubDate>Mon, 26 Nov 2018 16:10:01 GMT</pubDate>
            <atom:updated>2018-11-26T16:10:01.127Z</atom:updated>
            <content:encoded><![CDATA[<p>After so much time from writing the article about to develop material events on calendar with Kotlin in android, now I have pushed library for same on Github.</p><p>To use this without giving your time and efforts, to save your time to make your project fill with event calendar, use this library as shown below.</p><p>Link for library on Github is</p><p><a href="https://github.com/chintan369/Material-Event-On-Calendar">chintan369/Material-Event-On-Calendar</a></p><p>To add this library in your code, you will need to add maven(jitpack.io) in your root gradle file</p><pre>allprojects {<br> repositories {<br>  ...<br>	maven { url &#39;https://jitpack.io&#39; }<br>	}<br> }</pre><p>Now, paste this dependency in your app level gradle file</p><pre>dependencies {<br>        ...<br>        implementation &#39;com.github.chintan369:Material-Event-On-Calendar:latest-release&#39;<br>}</pre><p>Latest release you can find on github</p><p>And lastly put it in your layout.xml file as</p><pre>&lt;com.chintanpatel.materialeventcalendar.CalenderView<br>            android:id=&quot;@+id/eventCalendar&quot;<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;/&gt;</pre><p>and in your class/ kotlin file, you can use to add event as below.</p><pre>val eventList: ArrayList&lt;EventItem&gt; = arrayListOf()<br><br>eventList.add(<br>  EventItem(&quot;21-11-2018&quot;, &quot;29-11-2018&quot;, &quot;Gujarat Trip&quot;, &quot;#EEFFCC&quot;, &quot;1&quot;)<br> )<br>eventList.add(<br>   EventItem(&quot;30-11-2018&quot;, &quot;02-12-2018&quot;, &quot;Meeting&quot;)<br>)<br><br>eventCalendar.setCalenderEventClickListener(object : CalenderView.CalenderEventClickListener {<br>   override fun onEventClick(eventItem: EventItem) {<br>      //put your logic on event click<br>    }<br>})<br><br>eventCalendar.addEventList(eventList)<br>  eventCalendar.addEvent(<br>    EventItem(&quot;05-12-2018&quot;, &quot;07-12-2018&quot;)<br> )</pre><p>Pass <strong>true </strong>as second argument in <strong>addEventList() / addEvent()</strong>, if you want to clear previous events and add only you passed in first argument.</p><p>You can also change the header text color using <strong>setHeaderColor(color)</strong></p><p>That’s Done.</p><p>Please give your comments if you like this work. Thanks</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a217b6e1e0bd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-kotlin/material-events-on-calendar-is-now-on-git-a217b6e1e0bd">Material Events on Calendar is now on Git</a> was originally published in <a href="https://medium.com/android-kotlin">Kotlindroid</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing Material Designed Event Calender Part-3]]></title>
            <link>https://medium.com/android-kotlin/implementing-material-designed-event-calender-part-3-af7a37b4b00d?source=rss----6d1f56f450fb---4</link>
            <guid isPermaLink="false">https://medium.com/p/af7a37b4b00d</guid>
            <category><![CDATA[calendar]]></category>
            <category><![CDATA[material-design]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Chintan Patel]]></dc:creator>
            <pubDate>Mon, 26 Feb 2018 09:45:43 GMT</pubDate>
            <atom:updated>2018-02-26T09:45:43.014Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello all, we’ve created out Material Designed Event Calender. Now we’ll implement it in our activity / fragment to use it in our application.</p><p>If you’ve not gone through <strong>Part-1</strong> and <strong>Part-2</strong> of this tutorial, Please use the below link to create custom <strong>Material Designed Event Calender.</strong></p><ul><li><a href="https://medium.com/android-kotlin/creating-material-designed-event-calender-part-1-fa7e6677d586">Creating Material Designed Event Calender Part-1</a></li><li><a href="https://medium.com/android-kotlin/creating-material-designed-event-calender-part-2-ae38f85ce854">Creating Material Designed Event Calender Part-2</a></li></ul><p>Now, Create an Activity in an <strong>app </strong>and in layout for it named as <strong><em>activity_main.xml , </em></strong>write below code.</p><pre>&lt;<strong>com.example.CalenderView<br>    android:id=&quot;@+id/calender_event&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot; </strong>/&gt;</pre><p>Above code will show preview like image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*Y7YsUGDHLJNJ95x3jOnYbw.png" /><figcaption>CalenderView XML Preview in Android Studio</figcaption></figure><p>Now, Write code for adding events in CalenderView as below named Activity as <strong>MainActivity.kt</strong>. To use XML defined component directly without using<strong> findViewById, </strong>write below code in<strong> build.gradle </strong>files.</p><pre><em>//Project level build.gradle </em></pre><pre>dependencies {<br>    <strong>...<br>    </strong>classpath <strong>&quot;org.jetbrains.kotlin:kotlin-android-extensions:</strong>$kotlin_version<strong>&quot;<br></strong>}</pre><pre>//app level build.gradle, at the very bottom, end of file</pre><pre>apply <strong>plugin</strong>: <strong>&#39;kotlin-android-extensions&#39;</strong></pre><p>In <strong>MainActivity.kt, </strong>you can use now component directly as name of given id in xml file.</p><pre><strong>import </strong>kotlinx.android.synthetic.main.activity_main.*<br>...</pre><pre><strong>class </strong>MainActivity : AppCompatActivity() {<br>    <br>    <strong>override fun </strong>onCreate(savedInstanceState: Bundle?) {<br>        <strong>super</strong>.onCreate(savedInstanceState)<br>        setContentView(R.layout.<em>activity_main</em>)</pre><pre>        calender_trip.setCalenderEventClickListener(<strong>object </strong>:    CalenderView.CalenderEventClickListener {<br>    <br>        <strong>override fun </strong>onEventClick(eventItem: EventItem) {<br><em>                //Perform your code here using eventItem</em> <br>            }<br>        })</pre><pre>       addSomeEvents()</pre><pre>    }<br>}</pre><p>Now, we’ll add events in calender and it will display on calender.</p><pre><strong>fun </strong>addSomeEvents() {<br>    val eventList = ArrayList&lt;EventItem&gt;()</pre><pre>val event1 = EventItem(&quot;22-01-2018&quot;,&quot;01-02-2018&quot;,&quot;Company Holidays&quot;)<br>    val event2 = EventItem(&quot;14-01-2018&quot;,&quot;16-01-2018&quot;,&quot;Kite Festivals&quot;,&quot;#E87E04&quot;)<br>    val event3 = EventItem(&quot;25-12-2017&quot;,&quot;01-01-2018&quot;,&quot;Christmas Holidays&quot;,&quot;#F44336&quot;)<br>    val event4 = EventItem(&quot;05-02-2018&quot;,&quot;14-02-2018&quot;,&quot;Cousin Wedding&quot;,&quot;#FFC400&quot;)<br>    val event5 = EventItem(&quot;07-02-2018&quot;,&quot;014-02-2018&quot;,&quot;Not Free&quot;,&quot;#524b7e&quot;)<br>    val event6 = EventItem(&quot;15-02-2018&quot;,&quot;28-02-2018&quot;,&quot;Normal Meetings&quot;)</pre><pre>    eventList.add(event1)<br>    eventList.add(event2)<br>    eventList.add(event3)<br>    eventList.add(event4)<br>    eventList.add(event5)<br>    eventList.add(event6)</pre><pre><strong>    calender_event.</strong>addEventList(eventList)</pre><pre>}</pre><p>Now, Create <strong>CalenderEventClickListener </strong>interface for clicking on event and perform the code. Create this interface in <strong>CalenderView </strong>class file and also define method to set it from Activity.</p><pre><strong>var eventClickListener</strong>: CalenderEventClickListener? = <strong>null</strong></pre><pre><strong>fun </strong>setCalenderEventClickListener(eventClickListener: CalenderEventClickListener) {<br>    <strong>this</strong>.<strong>eventClickListener </strong>= eventClickListener<br>}<br><br><strong>interface </strong>CalenderEventClickListener {<br>    <strong>fun </strong>onEventClick(eventItem: EventItem)<br>}</pre><p>Now, <strong>Run </strong>your application and check it. It will look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*uTIvCwzqRnVtiHjFnjnwUQ.png" /><figcaption>CalenderView after Adding Events</figcaption></figure><p>That’s all. you have created an awesome calender which can show multiple events with range of the dates with different colors and effects. That’s very very awesome.</p><p>You can also create your custom methods to set dynamic background color of header, text color of Month title and change maximum limit of 3 Days of showing max. events per week.</p><p>Thank you for giving your valuable time to read this article and use this code. If you liked this tutorial, Please give 1, 2, 5 or 50 claps for this article. Enjoy this tutorial and follow me and this publication to get such amazing code updates. Thanks for your Support again…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af7a37b4b00d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-kotlin/implementing-material-designed-event-calender-part-3-af7a37b4b00d">Implementing Material Designed Event Calender Part-3</a> was originally published in <a href="https://medium.com/android-kotlin">Kotlindroid</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating Material Designed Event Calender Part-2]]></title>
            <link>https://medium.com/android-kotlin/creating-material-designed-event-calender-part-2-ae38f85ce854?source=rss----6d1f56f450fb---4</link>
            <guid isPermaLink="false">https://medium.com/p/ae38f85ce854</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[material-design]]></category>
            <category><![CDATA[calendar]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Chintan Patel]]></dc:creator>
            <pubDate>Mon, 26 Feb 2018 08:39:48 GMT</pubDate>
            <atom:updated>2018-02-26T12:07:23.480Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/909/1*ZlHddaHmwxfUOtL5qWmpsg.png" /></figure><p>Hello Developers, In this 2nd part of the material designed Event Calender, We’ll implement the main code part which shows dates and events for specific dates.</p><p>Let’s Start to implement our main function <strong>updateEventView() </strong>in <strong>CalenderView </strong>class.</p><p>Now, we’ll create variable named <strong><em>selectedCalender </em></strong>which will be used as current month calender and to count previous moth and next moth extra days to fill it out in week days. So Let’s start.</p><pre><strong>var </strong>selectedCalender: Calendar = Calendar.getInstance()<br><em>//This will get current calender time of system</em></pre><pre>selectedCalender.set(<strong>calender</strong>.get(Calendar.<em>YEAR</em>), <strong>calender</strong>.get(Calendar.<em>MONTH</em>), <strong>calender</strong>.get(Calendar.<em>DATE</em>))<br>//<em>This will set </em><strong><em>selectedCalender </em></strong><em>to selected month and year by next/previous buttons</em></pre><pre>//<em>This will set date to first</em><br>selectedCalender.set(Calendar.<em>DATE</em>, 1)<br>selectedCalender.set(Calendar.<em>HOUR</em>, 0) selectedCalender.set(Calendar.<em>MINUTE</em>, 0) selectedCalender.set(Calendar.<em>SECOND</em>, 0)</pre><pre><strong>calender</strong>.set(Calendar.<em>DATE</em>, 1)</pre><pre>//<em>This will count total days in selected month</em><br><strong>var </strong>totalDaysToShow: Int = <strong>calender</strong>.getActualMaximum(Calendar.<em>DAY_OF_MONTH</em>)</pre><pre>//total days extra to be added from previous month<br><strong>val </strong>toFillInPreviousMonthDays = 1 - <strong>calender</strong>.get(Calendar.<em>DAY_OF_WEEK</em>)<br><br><strong>calender</strong>.set(Calendar.<em>DATE</em>, totalDaysToShow)<br><br><em>//total days extra to be added from next month</em><strong><br>val </strong>toFillInNextMonthDays = 7 - <strong>calender</strong>.get(Calendar.<em>DAY_OF_WEEK</em>)<br><br><em>//total days to count max weeks in a month</em><br>totalDaysToShow += Math.abs(toFillInPreviousMonthDays) + toFillInNextMonthDays<br><br><strong>calender</strong>.set(Calendar.<em>DATE</em>, 1)<br><br><strong>if </strong>(toFillInPreviousMonthDays != 0) {<br>    selectedCalender.add(Calendar.<em>DAY_OF_YEAR</em>, toFillInPreviousMonthDays)<br>}</pre><p>Now, count max. number of weeks in a selected month do the below code which will give to show number of week layouts in a view and based on this make extra weeks visible or hidden and remove all the events which added before.</p><pre><strong>val </strong>totalRows = totalDaysToShow.div(7)</pre><pre><strong>when </strong>(totalRows) {<br>    4 -&gt; {<br>        <strong>dayViewRow5</strong>.<em>visibility </em>= View.<em>GONE<br>        </em><strong>dayViewRow6</strong>.<em>visibility </em>= View.<em>GONE<br>    </em>}<br>    5 -&gt; {<br>        <strong>dayViewRow5</strong>.<em>visibility </em>= View.<em>VISIBLE<br>        </em><strong>dayViewRow6</strong>.<em>visibility </em>= View.<em>GONE<br>    </em>}<br>    <strong>else </strong>-&gt; {<br>        <strong>dayViewRow5</strong>.<em>visibility </em>= View.<em>VISIBLE<br>        </em><strong>dayViewRow6</strong>.<em>visibility </em>= View.<em>VISIBLE<br>    </em>}<br>}</pre><pre><strong>val </strong>dateFormatter = SimpleDateFormat(<strong>dateFormat</strong>, Locale.getDefault())<br><br>(<strong>dayViewRow1</strong>.findViewById(R.id.<em>layout_tripEvents</em>) <strong>as </strong>LinearLayout).removeAllViews()</pre><pre>(<strong>dayViewRow2</strong>.findViewById(R.id.<em>layout_tripEvents</em>) <strong>as </strong>LinearLayout).removeAllViews()</pre><pre>...</pre><pre>(<strong>dayViewRow6</strong>.findViewById(R.id.<em>layout_tripEvents</em>) <strong>as </strong>LinearLayout).removeAllViews()</pre><p>Now, Check for the days and date and on base of this, add event views in the week row on specific date or in between dates.</p><pre><strong>for </strong>(i <strong>in </strong>1..totalDaysToShow <em>step </em>7) {<br>    <strong>when </strong>(i) {<br>        <strong>in </strong>1..7 -&gt; {<br>            <strong>val </strong>layout_tripEvents = <strong>dayViewRow1</strong>.findViewById(R.id.<em>layout_tripEvents</em>) <strong>as </strong>LinearLayout<br>            ...<br>        }<br>        <br>        <strong>in </strong>8..14 -&gt; {<br>            <strong>val </strong>layout_tripEvents = <strong>dayViewRow2</strong>.findViewById(R.id.<em>layout_tripEvents</em>) <strong>as </strong>LinearLayout<br>            ...<br>        }</pre><pre>        ...</pre><pre><strong>        in</strong> 36..42 -&gt; {<br>            <strong>val </strong>layout_tripEvents = <strong>dayViewRow6</strong>.findViewById(R.id.<em>layout_tripEvents</em>) <strong>as </strong>LinearLayout<br>            ...<br>        }<br>}</pre><p>Now, In a specific week row, assuming 1st row (i.e. 1..7), we’ll show dates falling on that week (i.e. 26, 27, 28, 1, 2, 3, 4) which are 26–28 are of previous month while 1–4 are of current month. So let code for 1st Week row and other’s will be the same except defining <strong>dayViewRow(n)</strong> as per the week <strong>n.</strong></p><pre><strong>val </strong>layout_tripEvents = <strong>dayViewRow1</strong>.findViewById(R.id.<em>layout_tripEvents</em>) <strong>as </strong>LinearLayout<br><br><em>//will take this date as min. date of this week row<br></em><strong>val </strong>minDate = selectedCalender.<em>time<br><br></em><strong>val </strong>day1 = <strong>dayViewRow1</strong>.findViewById(R.id.<em>day1</em>) <strong>as </strong>TextView<br>day1.<em>text </em>= selectedCalender.get(Calendar.<em>DATE</em>).toString()<br>selectedCalender = addSingleDay(selectedCalender)<br><br><strong>var </strong>widthOfText = day1.<em>measuredWidth<br><br></em><strong>val </strong>day2 = <strong>dayViewRow1</strong>.findViewById(R.id.<em>day2</em>) <strong>as </strong>TextView<br>day2.<em>text </em>= selectedCalender.get(Calendar.<em>DATE</em>).toString()<br>selectedCalender = addSingleDay(selectedCalender)<br><br><strong>val </strong>day3 = <strong>dayViewRow1</strong>.findViewById(R.id.<em>day3</em>) <strong>as </strong>TextView<br>day3.<em>text </em>= selectedCalender.get(Calendar.<em>DATE</em>).toString()<br>selectedCalender = addSingleDay(selectedCalender)<br><br>...<br><br><strong>val </strong>day6 = <strong>dayViewRow1</strong>.findViewById(R.id.<em>day6</em>) <strong>as </strong>TextView<br>day6.<em>text </em>= selectedCalender.get(Calendar.<em>DATE</em>).toString()<br>selectedCalender = addSingleDay(selectedCalender)</pre><pre>//will take 7th day date as max. date of this week<br><strong>val </strong>maxDate = selectedCalender.<em>time<br><br></em><strong>val </strong>day7 = <strong>dayViewRow1</strong>.findViewById(R.id.<em>day7</em>) <strong>as </strong>TextView<br>day7.<em>text </em>= selectedCalender.get(Calendar.<em>DATE</em>).toString()<br>selectedCalender = addSingleDay(selectedCalender)</pre><p>Using above code, we’ve set dates fallen in this 1st week. Create<strong> <em>addSingleDay(), isDateInBetween(), isSameDay(), getDaysBetween()</em> </strong>function to add single day, and calculate dates and gap between dates in a calender in <strong>CalenderView </strong>class file.</p><pre><strong>private fun </strong>addSingleDay(calendar: Calendar): Calendar {<br>    <strong>val </strong>currentDayOfMonth = calendar.get(Calendar.<em>DAY_OF_MONTH</em>)<br>    <strong>val </strong>currentDayOfYear = calendar.get(Calendar.<em>DAY_OF_YEAR</em>)<br><br>    <strong>val </strong>copyCalender: Calendar = calendar.clone() <strong>as </strong>Calendar<br>    copyCalender.set(Calendar.<em>DATE</em>, 1)<br><br>    <strong>val </strong>maxDaysInMonth = copyCalender.getMaximum(Calendar.<em>DAY_OF_MONTH</em>)<br><br>    <strong>if </strong>(currentDayOfMonth == maxDaysInMonth) {<br>        <strong>if </strong>(currentDayOfYear == calendar.getActualMaximum(Calendar.<em>DAY_OF_YEAR</em>)) {<br>            calendar.add(Calendar.<em>YEAR</em>, 1)<br>            calendar.set(Calendar.<em>MONTH</em>, Calendar.<em>JANUARY</em>)<br>            calendar.set(Calendar.<em>DAY_OF_YEAR</em>, 1)<br>        } <strong>else </strong>{<br>            calendar.add(Calendar.<em>MONTH</em>, 1)<br>            calendar.set(Calendar.<em>DATE</em>, 1)<br>        }<br>    } <strong>else </strong>{<br>        calendar.add(Calendar.<em>DAY_OF_YEAR</em>, 1)<br>    }<br><br>    <strong>return </strong>calendar<br>}</pre><pre><strong>private fun </strong>isDateInBetween(date: Date, minDate: Date, maxDate: Date): Boolean {<br>    date.<em>hours </em>= 0; date.<em>minutes </em>= 0; date.<em>seconds </em>= 0<br>    minDate.<em>hours </em>= 0; minDate.<em>minutes </em>= 0; minDate.<em>seconds </em>= 0<br>    maxDate.<em>hours </em>= 0; maxDate.<em>minutes </em>= 0; maxDate.<em>seconds </em>= 0<br>    <strong>if </strong>(isSameDay(date, minDate) || isSameDay(date, maxDate)) {<br>        <strong>return true<br>    </strong>}<br>    <strong>if </strong>(date.compareTo(minDate) &gt;= 0 &amp;&amp; date.compareTo(maxDate) &lt;= 0) {<br>        <strong>return true<br>    </strong>}<br>    <strong>return false<br></strong>}<br><br><strong>private fun </strong>isSameDay(date1: Date, date2: Date): Boolean {<br>    <strong>val </strong>calender1 = Calendar.getInstance()<br>    <strong>val </strong>calender2 = Calendar.getInstance()<br>    calender1.<em>time </em>= date1; calender2.<em>time </em>= date2<br>    <strong>return </strong>calender1.get(Calendar.<em>YEAR</em>) == calender2.get(Calendar.<em>YEAR</em>) &amp;&amp; calender1.get(Calendar.<em>DAY_OF_YEAR</em>) == calender2.get(Calendar.<em>DAY_OF_YEAR</em>)<br>}<br><br><strong>private fun </strong>getDaysBetween(startDate: Date, endDate: Date): Int {<br>    startDate.<em>hours </em>= 0; startDate.<em>minutes </em>= 0; startDate.<em>seconds </em>= 0<br>    endDate.<em>hours </em>= 0; endDate.<em>minutes </em>= 0; endDate.<em>seconds </em>= 0<br><br>    <strong>val </strong>diff = endDate.<em>time </em>- startDate.<em>time<br><br>    </em><strong>return </strong>TimeUnit.<strong>DAYS</strong>.convert(diff, TimeUnit.<strong>MILLISECONDS</strong>).toInt()</pre><pre>}</pre><p>Now, Create layout named <strong><em>layout_event_line.xml </em></strong>to draw event view on week days.</p><pre>&lt;<strong>android.support.v7.widget.CardView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    app:cardElevation=&quot;3dp&quot;<br>    app:cardCornerRadius=&quot;2dp&quot;</strong>&gt;<br><br>    &lt;<strong>TextView<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:layout_marginBottom=&quot;2dp&quot;<br>        android:layout_marginTop=&quot;2dp&quot;<br>        android:background=&quot;@color/colorAccent&quot;<br>        android:gravity=&quot;center&quot;<br>        android:id=&quot;@+id/txt_eventTitle&quot;<br>        android:singleLine=&quot;true&quot;<br>        android:textColor=&quot;@color/white&quot;<br>        android:textSize=&quot;14sp&quot; </strong>/&gt;<br><br>&lt;/<strong>android.support.v7.widget.CardView</strong>&gt;</pre><p>Now, Continuing on the 1st week row’s code as this will add event views on the dates.</p><pre><strong>if </strong>(!firstTime) {<br>    day1.postDelayed(<strong>{<br>        </strong>widthOfText = day1.<em>measuredWidth<br>        </em><strong>var </strong>eventAddCount = 0</pre><pre>        <em>//this will check for all events falls in this week</em><strong><br>        for </strong>(j <strong>in </strong>0 <em>until </em><strong>eventList</strong>.<strong>size</strong>) {<br>            <strong>val </strong>startDate = dateFormatter.parse(<strong>eventList</strong>.get(j).<strong>start</strong>)<br>            <strong>val </strong>endDate = dateFormatter.parse(<strong>eventList</strong>.get(j).<strong>end</strong>)<br><br>            <strong>if </strong>(eventAddCount == <strong>MAX_EVENT</strong>) {<br>                <strong>break<br>            </strong>}<br><br>            startDate.<em>hours </em>= 0; startDate.<em>minutes </em>= 0; startDate.<em>seconds </em>= 0<br>            endDate.<em>hours </em>= 0; endDate.<em>minutes </em>= 0; endDate.<em>seconds </em>= 0<br><br>  <strong><em>//At this position of code, we&#39;ll do step by step as shown below</em></strong><br>        <br>        }<br>    <strong>}</strong>, <strong>POST_TIME</strong>)<br>}</pre><p>Now, doing step by step code at the position shown above, 1st we’ll fetch event line view and set background color and title for event with click listener. We’ll implement event click listener later.</p><pre><strong>val </strong>eventTrip = <strong>layoutInflater</strong>.inflate(R.layout.<em>layout_event_line</em>, <strong>null</strong>) <strong>as </strong>CardView<br><strong>val </strong>eventTitle = eventTrip.findViewById(R.id.<em>txt_eventTitle</em>) <strong>as </strong>TextView<br>eventTrip.setCardBackgroundColor(Color.parseColor(<strong>eventList</strong>.get(j).<strong>color</strong>))<br>eventTitle.setBackgroundColor(Color.parseColor(<strong>eventList</strong>.get(j).<strong>color</strong>))<br>eventTitle.<em>text </em>= <strong>eventList</strong>.get(j).<strong>title<br></strong>eventTitle.setOnClickListener <strong>{<br>    eventClickListener</strong>?.onEventClick(<strong>eventList</strong>[j])<br><strong>}</strong></pre><p>Now, Check if start date of an event falls in between of min date of a week and ax date of a week, on base of that this will draw an event line<strong><em> from starting date to end of a week.</em></strong></p><pre><strong>if </strong>(isDateInBetween(startDate, minDate, maxDate)) {<br><br>    <strong>val </strong>daysBetween = <strong>if </strong>(isSameDay(minDate, startDate)) 0 <strong>else </strong>getDaysBetween(minDate, startDate) + 1<br><br>    <strong>val </strong>startMarginDays = (widthOfText * daysBetween) + <strong>EXTRA_MARGIN<br>    var </strong>endMarginDays = 0<br>    <br>    <strong><em>//Below condition will check for end days is a week&#39;s last day or in between of start day and max day</em></strong></pre><pre>    <strong>if </strong>(isDateInBetween(endDate, minDate, maxDate)) {<br>        endMarginDays = (widthOfText * getDaysBetween(endDate, maxDate)) + <strong>EXTRA_MARGIN<br>    </strong>}<br><br>    <strong>val </strong>params = LinearLayout.LayoutParams(LayoutParams.<em>MATCH_PARENT</em>, LayoutParams.<em>WRAP_CONTENT</em>)<br>    params.setMargins(startMarginDays, 5, endMarginDays, 5)<br>    layout_tripEvents.addView(eventTrip, params)<br><br>    eventAddCount++<br><br>}</pre><p>If above condition will <strong>false</strong> to check whether start date of an event is not in between min &amp; max dates of a week, then it will check for end date of event comes in between of min &amp; max dates of a week.</p><pre><strong>else if </strong>(isDateInBetween(endDate, minDate, maxDate)) {<br>    <strong>val </strong>startMarginDays = 0<br>    <strong>val </strong>endMarginDays = (widthOfText * getDaysBetween(endDate, maxDate)) + <strong>EXTRA_MARGIN<br><br>    val </strong>params = LinearLayout.LayoutParams(LayoutParams.<em>MATCH_PARENT</em>, LayoutParams.<em>WRAP_CONTENT</em>)<br>    params.setMargins(startMarginDays, 5, endMarginDays, 5)<br>    layout_tripEvents.addView(eventTrip, params)<br>    eventAddCount++<br>}</pre><p>If above condition gets <strong>true, </strong>it will draw event line from week start day to the end date comes on the week day. Even if this condition gets <strong>false, </strong>it will check for last condition which checks if <em>min. date of week falls under start date of an event and max date of a week</em> <strong>AND </strong><em>max. date of week falls under start date of an event and end date of event.</em></p><pre><strong>else if </strong>(isDateInBetween(minDate, startDate, maxDate) &amp;&amp; isDateInBetween(maxDate, startDate, endDate)) {<br><br>    <strong>val </strong>startMarginDays = 0<br>    <strong>val </strong>endMarginDays = 0<br><br>    <strong>val </strong>params = LinearLayout.LayoutParams(LayoutParams.<em>MATCH_PARENT</em>, LayoutParams.<em>WRAP_CONTENT</em>)<br>    params.setMargins(startMarginDays, 5, endMarginDays, 5)<br>    layout_tripEvents.addView(eventTrip, params)<br>    eventAddCount++<br>}</pre><p>If above condition will executed, then it will draw line on whole week.</p><p>Now, create a function through we can add events and show it on the calender. So create functions in <strong>CalenderView </strong>class file.</p><pre><strong>fun </strong>addEventList(eventListItems: ArrayList&lt;EventItem&gt;, clearOldData: Boolean = <strong>false</strong>) {<br>    <strong>if </strong>(clearOldData) <strong>eventList</strong>.clear()<br>    (<strong>dayViewRow1</strong>.findViewById(R.id.<em>layout_tripEvents</em>) <strong>as </strong>LinearLayout).removeAllViews()<br>    (<strong>dayViewRow2</strong>.findViewById(R.id.<em>layout_tripEvents</em>) <strong>as </strong>LinearLayout).removeAllViews()<br>    ...<br>    (<strong>dayViewRow6</strong>.findViewById(R.id.<em>layout_tripEvents</em>) <strong>as </strong>LinearLayout).removeAllViews()<br><br>    <strong>val </strong>sortedList = eventListItems.<em>sortedWith</em>(<em>compareBy </em><strong>{ it</strong>.getStartDateToSort() <strong>}</strong>)<br><br>    <strong>eventList</strong>.clear()<br>    <strong>eventList</strong>.addAll(sortedList)<br>    updateEventView()<br>}</pre><p>This code will draw events with different provided colors and titles on it.</p><p>Now, we’ve successfully created the custom Event Calender but not implemented yet in our application. So to view, how to implement this view with it’s different methods, We’ll go through <strong>Part 3</strong>.</p><p>Thank you for your time to view this code and read article. If you liked this, Please give 1, 2, 5 or 50… claps for this article. Thank you for your support.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae38f85ce854" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-kotlin/creating-material-designed-event-calender-part-2-ae38f85ce854">Creating Material Designed Event Calender Part-2</a> was originally published in <a href="https://medium.com/android-kotlin">Kotlindroid</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating Material Designed Event Calender Part-1]]></title>
            <link>https://medium.com/android-kotlin/creating-material-designed-event-calender-part-1-fa7e6677d586?source=rss----6d1f56f450fb---4</link>
            <guid isPermaLink="false">https://medium.com/p/fa7e6677d586</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[calendar]]></category>
            <category><![CDATA[material-design]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Chintan Patel]]></dc:creator>
            <pubDate>Mon, 26 Feb 2018 06:20:56 GMT</pubDate>
            <atom:updated>2018-02-26T12:06:39.692Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*ild2RzT7UfIEK2BMFPLmDQ.png" /></figure><p>This new Material designed Event Calender which I created after too many research. I was creating a project in my organization and I was required to use calender which can show user’s event on dates, so I searched for third-party library too much but I didn’t found and I decided to create custom one.</p><p>I have created this Event Calender using <strong>Kotlin </strong>language<strong> </strong>in Android. To use Kotlin in your android studio project, define below code in <strong>Project </strong>level <strong>build.gradle</strong> file where 1.X.X is the latest version of kotlin.</p><pre>buildscript {<br>    ext.kotlin_version = <strong>&#39;1.X.X&#39;<br>    ...</strong><br>    dependencies {<br>        classpath <strong>&#39;com.android.tools.build:gradle:2.3.3&#39;<br>        </strong>classpath <strong>&quot;org.jetbrains.kotlin:kotlin-gradle-plugin:</strong>$kotlin_version<strong>&quot;</strong><em><br>    </em>}<br>}</pre><p>and in <strong>app </strong>level <strong>build.gradle</strong> file implement below library and <strong>Sync gradle</strong>.</p><pre>dependencies {<br>    implementation <strong>&quot;org.jetbrains.kotlin:kotlin-stdlib-jre7:</strong>$kotlin_version<strong>&quot;<br></strong>}</pre><p>Now, Create a Kotlin class named as <strong>CalenderView.kt </strong>in <strong>app </strong>module.</p><pre><strong>class </strong>CalenderView @JvmOverloads <strong>constructor</strong>(context: Context?, attrs: AttributeSet? = <strong>null</strong>, defStyleAttr: Int = 0) : LinearLayout(context, attrs, defStyleAttr) {<br><strong>    <br>    init </strong>{<br>        <em>orientation </em>= LinearLayout.VERTICAL<br>    }<br>}</pre><p>I have used super class as <strong>LinearLayout, </strong>So all the day headers and days can be displayed linearly vertically by defining its orientation in <strong>init</strong> method.</p><p>Now, Create views which is to be displayed in Calender. So Let’s create header view for Calender which shows <strong>Month &amp; Year</strong> with <strong>Next </strong>and <strong>Previous </strong>arrows to change month and <strong>Week days</strong> (i.e. Sun, Mon, etc…). Creating Layout as named <strong><em>layout_day_header_view.xml</em></strong></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 xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    android:orientation=&quot;vertical&quot;</strong>&gt;</pre><pre>        &lt;<strong>LinearLayout<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:gravity=&quot;center&quot;<br>            android:orientation=&quot;horizontal&quot;</strong>&gt;</pre><pre>            &lt;<strong>ImageView<br>                android:id=&quot;@+id/btn_previous&quot;<br>                android:layout_width=&quot;wrap_content&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_margin=&quot;5dp&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:src=&quot;@drawable/ic_previous_month&quot;<br>                android:tint=&quot;@color/button_background&quot; </strong>/&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/txt_monthTitle&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:gravity=&quot;center&quot;<br>                android:padding=&quot;10dp&quot;<br>                android:text=&quot;Month and Year&quot;<br>                android:textColor=&quot;@color/dark_gray&quot;<br>                android:textSize=&quot;20sp&quot;<br>                android:textStyle=&quot;bold&quot; </strong>/&gt;</pre><pre>            &lt;<strong>ImageView<br>                android:id=&quot;@+id/btn_next&quot;<br>                android:layout_width=&quot;wrap_content&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_margin=&quot;5dp&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:src=&quot;@drawable/ic_next_month&quot;<br>                android:tint=&quot;@color/button_background&quot; </strong>/&gt;</pre><pre>        &lt;/<strong>LinearLayout</strong>&gt;</pre><pre>        &lt;<strong>LinearLayout<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:orientation=&quot;horizontal&quot;</strong>&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/txt_sun&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:gravity=&quot;center&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;SUN&quot;<br>                android:textColor=&quot;@color/dark_gray&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/txt_mon&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:gravity=&quot;center&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;MON&quot;<br>                android:textColor=&quot;@color/dark_gray&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/txt_tue&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:gravity=&quot;center&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;TUE&quot;<br>                android:textColor=&quot;@color/dark_gray&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/txt_wed&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:gravity=&quot;center&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;WED&quot;<br>                android:textColor=&quot;@color/dark_gray&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/txt_thu&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:gravity=&quot;center&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;THU&quot;<br>                android:textColor=&quot;@color/dark_gray&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/txt_fri&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:gravity=&quot;center&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;FRI&quot;<br>                android:textColor=&quot;@color/dark_gray&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/txt_sat&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:gravity=&quot;center&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;SAT&quot;<br>                android:textColor=&quot;@color/dark_gray&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>        &lt;/<strong>LinearLayout</strong>&gt;</pre><pre>&lt;/<strong>LinearLayout</strong>&gt;</pre><p>Now, Create a single week view named as <strong><em>layout_day_date_view.xml </em></strong>where I have used <strong>CardView </strong>to give elevation and Z depth effect and <strong>FrameLayout </strong>used so Event can be put on Week View.</p><pre><em>&lt;?</em><strong>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</strong><em>?&gt;<br></em>&lt;<strong>FrameLayout 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:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    android:orientation=&quot;horizontal&quot;</strong>&gt;</pre><pre>    &lt;<strong>LinearLayout<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>        android:minHeight=&quot;50dp&quot;<br>        android:orientation=&quot;horizontal&quot;</strong>&gt;</pre><pre>        &lt;<strong>android.support.v7.widget.CardView<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            android:layout_weight=&quot;1&quot;<br>            app:cardCornerRadius=&quot;2dp&quot;<br>            app:cardElevation=&quot;5dp&quot;</strong>&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/day1&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;match_parent&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:background=&quot;@drawable/day_view_bg&quot;<br>                android:gravity=&quot;top|end&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;1&quot;<br>                android:textColor=&quot;#000000&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>        &lt;/<strong>android.support.v7.widget.CardView</strong>&gt;</pre><pre>        &lt;<strong>android.support.v7.widget.CardView<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            android:layout_weight=&quot;1&quot;<br>            app:cardCornerRadius=&quot;2dp&quot;<br>            app:cardElevation=&quot;5dp&quot;</strong>&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/day2&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;match_parent&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:background=&quot;@drawable/day_view_bg&quot;<br>                android:gravity=&quot;top|end&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;2&quot;<br>                android:textColor=&quot;#000000&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>        &lt;/<strong>android.support.v7.widget.CardView</strong>&gt;</pre><pre>        &lt;<strong>android.support.v7.widget.CardView<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            android:layout_weight=&quot;1&quot;<br>            app:cardCornerRadius=&quot;2dp&quot;<br>            app:cardElevation=&quot;5dp&quot;</strong>&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/day3&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;match_parent&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:background=&quot;@drawable/day_view_bg&quot;<br>                android:gravity=&quot;top|end&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;3&quot;<br>                android:textColor=&quot;#000000&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>        &lt;/<strong>android.support.v7.widget.CardView</strong>&gt;</pre><pre>        &lt;<strong>android.support.v7.widget.CardView<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            android:layout_weight=&quot;1&quot;<br>            app:cardCornerRadius=&quot;2dp&quot;<br>            app:cardElevation=&quot;5dp&quot;</strong>&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/day4&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;match_parent&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:background=&quot;@drawable/day_view_bg&quot;<br>                android:gravity=&quot;top|end&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;4&quot;<br>                android:textColor=&quot;#000000&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>        &lt;/<strong>android.support.v7.widget.CardView</strong>&gt;</pre><pre>        &lt;<strong>android.support.v7.widget.CardView<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            android:layout_weight=&quot;1&quot;<br>            app:cardCornerRadius=&quot;2dp&quot;<br>            app:cardElevation=&quot;5dp&quot;</strong>&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/day5&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;match_parent&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:background=&quot;@drawable/day_view_bg&quot;<br>                android:gravity=&quot;top|end&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;5&quot;<br>                android:textColor=&quot;#000000&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>        &lt;/<strong>android.support.v7.widget.CardView</strong>&gt;</pre><pre>        &lt;<strong>android.support.v7.widget.CardView<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            android:layout_weight=&quot;1&quot;<br>            app:cardCornerRadius=&quot;2dp&quot;<br>            app:cardElevation=&quot;5dp&quot;</strong>&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/day6&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;match_parent&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:background=&quot;@drawable/day_view_bg&quot;<br>                android:gravity=&quot;top|end&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;6&quot;<br>                android:textColor=&quot;#000000&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>        &lt;/<strong>android.support.v7.widget.CardView</strong>&gt;</pre><pre>        &lt;<strong>android.support.v7.widget.CardView<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            android:layout_weight=&quot;1&quot;<br>            app:cardCornerRadius=&quot;2dp&quot;<br>            app:cardElevation=&quot;5dp&quot;</strong>&gt;</pre><pre>            &lt;<strong>TextView<br>                android:id=&quot;@+id/day7&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;match_parent&quot;<br>                android:layout_weight=&quot;1&quot;<br>                android:background=&quot;@drawable/day_view_bg&quot;<br>                android:gravity=&quot;top|end&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:text=&quot;7&quot;<br>                android:textColor=&quot;#000000&quot;<br>                android:textSize=&quot;16sp&quot; </strong>/&gt;</pre><pre>        &lt;/<strong>android.support.v7.widget.CardView</strong>&gt;</pre><pre>    &lt;/<strong>LinearLayout</strong>&gt;</pre><pre>    &lt;<strong>LinearLayout<br>        android:id=&quot;@+id/layout_tripEvents&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:orientation=&quot;vertical&quot;<br>        android:layout_marginBottom=&quot;5dp&quot;<br>        android:layout_marginTop=&quot;25dp&quot;<br>        android:animateLayoutChanges=&quot;true&quot;</strong>&gt;</pre><pre>    &lt;/<strong>LinearLayout</strong>&gt;</pre><pre>&lt;/<strong>FrameLayout</strong>&gt;</pre><p>Now, define constants and view objects so we can use it later. We are defining 6 Week objects as there can be max 6 week in month with previous month’s days and next month’s days. Default I’ve defined Max events to show on Calender is 3 but we can change it by programmatic by creating function.</p><pre><strong>class </strong>CalenderView ... {<br>    <strong>var MAX_EVENT </strong>= 3<br>    <br>    <strong>companion object </strong>{<br>        <strong>const val monthYearFormat </strong>= <strong>&quot;MMM yyyy&quot;<br>        const val dateFormat </strong>= <strong>&quot;dd-MM-yyyy&quot;<br>        const val dateTimeFormat </strong>= <strong>&quot;dd-MM-yyyy hh:mm:ss&quot;<br>        const val EXTRA_MARGIN </strong>= 15<br>        <strong>const val POST_TIME</strong>: Long = 100<br>    }</pre><pre><strong>private var eventList</strong>: ArrayList&lt;EventItem&gt; = ArrayList()<strong>    <br>    private var calender</strong>: Calendar = Calendar.getInstance()<br>    <strong>private var layoutInflater</strong>: LayoutInflater = context?.getSystemService(Context.<em>LAYOUT_INFLATER_SERVICE</em>) <strong>as </strong>LayoutInflater<br></pre><pre>    <strong>private var dayViewHeader</strong>: View = <strong>layoutInflater</strong>.inflate(R.layout.<em>layout_day_header_view</em>, <strong>this</strong>, <strong>false</strong>)</pre><pre><strong>private var dayViewRow1</strong>: FrameLayout = <strong>layoutInflater</strong>.inflate(R.layout.<em>layout_day_date_view</em>, <strong>this</strong>, <strong>false</strong>) <strong>as </strong>FrameLayout</pre><pre><strong>private var dayViewRow2</strong>: FrameLayout = <strong>layoutInflater</strong>.inflate(R.layout.<em>layout_day_date_view</em>, <strong>this</strong>, <strong>false</strong>) <strong>as </strong>FrameLayout</pre><pre><strong>private var dayViewRow3</strong>: FrameLayout = <strong>layoutInflater</strong>.inflate(R.layout.<em>layout_day_date_view</em>, <strong>this</strong>, <strong>false</strong>) <strong>as </strong>FrameLayout</pre><pre><strong>private var dayViewRow4</strong>: FrameLayout = <strong>layoutInflater</strong>.inflate(R.layout.<em>layout_day_date_view</em>, <strong>this</strong>, <strong>false</strong>) <strong>as </strong>FrameLayout</pre><pre><strong>private var dayViewRow5</strong>: FrameLayout = <strong>layoutInflater</strong>.inflate(R.layout.<em>layout_day_date_view</em>, <strong>this</strong>, <strong>false</strong>) <strong>as </strong>FrameLayout</pre><pre><strong>private var dayViewRow6</strong>: FrameLayout = <strong>layoutInflater</strong>.inflate(R.layout.<em>layout_day_date_view</em>, <strong>this</strong>, <strong>false</strong>) <strong>as </strong>FrameLayout</pre><pre><strong>private var monthTitle</strong>: TextView? = <strong>dayViewHeader</strong>.findViewById(R.id.<em>txt_monthTitle</em>) <strong>as </strong>TextView</pre><pre><strong>private var btn_next</strong>: ImageView? = <strong>dayViewHeader</strong>.findViewById(R.id.<em>btn_next</em>) <strong>as </strong>ImageView<br>    <strong>private var btn_previous</strong>: ImageView? = <strong>dayViewHeader</strong>.findViewById(R.id.<em>btn_previous</em>) <strong>as </strong>ImageView</pre><pre>}</pre><p>Now, Create function to remove all created event views at before time and refresh the views with selected month events using <strong>init </strong>function.</p><pre><strong>init </strong>{<br>    removeAllViews()  //In-built function in LinearLayout<br>    <em>orientation </em>= LinearLayout.<em>VERTICAL<br>    </em>addView(<strong>dayViewHeader</strong>)<br>    addView(<strong>dayViewRow1</strong>)<br>    addView(<strong>dayViewRow2</strong>)<br>    addView(<strong>dayViewRow3</strong>)<br>    addView(<strong>dayViewRow4</strong>)<br>    addView(<strong>dayViewRow5</strong>)<br>    addView(<strong>dayViewRow6</strong>)</pre><pre>    <strong>monthTitle</strong>?.<em>text </em>= SimpleDateFormat(<strong>monthYearFormat</strong>, Locale.getDefault()).format(<strong>calender</strong>.<em>time</em>)<br>    <strong>btn_next</strong>?.setOnClickListener <strong>{ </strong>nextMonth() <strong>}<br>    btn_previous</strong>?.setOnClickListener <strong>{ </strong>previousMonth() <strong>}</strong></pre><pre><strong>    </strong>setOnTouchListener(<strong>object </strong>: OnCalendarSwipeListener(context) {<br>        <strong>override fun </strong>onSwipeLeft() {<br>            nextMonth()<br>        }</pre><pre>        <strong>override fun </strong>onSwipeRight() {<br>            previousMonth()<br>        }<br>    })<br>    updateEventView(<strong>true</strong>)<br>}</pre><pre><strong>fun </strong>nextMonth() {<br>    <strong>calender</strong>.add(Calendar.<em>MONTH</em>, 1)<br>    <strong>monthTitle</strong>?.<em>text </em>= SimpleDateFormat(<strong>monthYearFormat</strong>, Locale.getDefault()).format(<strong>calender</strong>.<em>time</em>)<br>    updateEventView()<br>}</pre><pre><strong>fun </strong>previousMonth() {<br>    <strong>calender</strong>.add(Calendar.<em>MONTH</em>, -1)<br>    <strong>monthTitle</strong>?.<em>text </em>= SimpleDateFormat(<strong>monthYearFormat</strong>, Locale.getDefault()).format(<strong>calender</strong>.<em>time</em>)<br>    updateEventView()<br>}</pre><pre><strong>private fun </strong>updateEventView(firstTime: Boolean = <strong>false</strong>) {<br>    <strong>//Will implement this in next Part, Part-2<br></strong>}</pre><p>Now, Create <strong>OnCalenderSwipeListener </strong>interface to use swipe gesture in Calender, So we can change month by swiping it. This interface is created named as <strong>OnCalendarSwipeListener.java</strong></p><pre><strong>public class </strong>OnCalendarSwipeListener <strong>implements </strong>View.OnTouchListener {</pre><pre>    <strong>private final </strong>GestureDetector <strong>gestureDetector</strong>;</pre><pre>    <strong>public </strong>OnCalendarSwipeListener(Context ctx) {<br>        <strong>gestureDetector </strong>= <strong>new </strong>GestureDetector(ctx, <strong>new </strong>GestureListener());<br>    }</pre><pre>    @Override<br>    <strong>public boolean </strong>onTouch(View v, MotionEvent event) {<br>        <strong>return gestureDetector</strong>.onTouchEvent(event);<br>    }</pre><pre>    <strong>public void </strong>onSwipeRight() {</pre><pre>    }</pre><pre>    <strong>public void </strong>onSwipeLeft() {</pre><pre>    }</pre><pre>    <strong>private void </strong>onSwipeTop() {</pre><pre>    }</pre><pre>    <strong>private void </strong>onSwipeBottom() {</pre><pre>    }</pre><pre>    <strong>private final class </strong>GestureListener <strong>extends </strong>GestureDetector.SimpleOnGestureListener {</pre><pre>        <strong>private static final int <em>SWIPE_THRESHOLD </em></strong>= 100;<br>        <strong>private static final int <em>SWIPE_VELOCITY_THRESHOLD </em></strong>= 100;</pre><pre>        @Override<br>        <strong>public boolean </strong>onDown(MotionEvent e) {<br>            <strong>return true</strong>;<br>        }</pre><pre>        @Override<br>        <strong>public boolean </strong>onFling(MotionEvent e1, MotionEvent e2, <strong>float </strong>velocityX, <strong>float </strong>velocityY) {<br>            <strong>boolean </strong>result = <strong>false</strong>;<br>            <strong>try </strong>{<br>                <strong>float </strong>diffY = e2.getY() - e1.getY();<br>                <strong>float </strong>diffX = e2.getX() - e1.getX();<br>                <strong>if </strong>(Math.<em>abs</em>(diffX) &gt; Math.<em>abs</em>(diffY)) {<br>                    <strong>if </strong>(Math.<em>abs</em>(diffX) &gt; <strong><em>SWIPE_THRESHOLD </em></strong>&amp;&amp; Math.<em>abs</em>(velocityX) &gt; <strong><em>SWIPE_VELOCITY_THRESHOLD</em></strong>) {<br>                        <strong>if </strong>(diffX &gt; 0) {<br>                            onSwipeRight();<br>                        } <strong>else </strong>{<br>                            onSwipeLeft();<br>                        }<br>                        result = <strong>true</strong>;<br>                    }<br>                } <strong>else if </strong>(Math.<em>abs</em>(diffY) &gt; <strong><em>SWIPE_THRESHOLD </em></strong>&amp;&amp; Math.<em>abs</em>(velocityY) &gt; <strong><em>SWIPE_VELOCITY_THRESHOLD</em></strong>) {<br>                    <strong>if </strong>(diffY &gt; 0) {<br>                        onSwipeBottom();<br>                    } <strong>else </strong>{<br>                        onSwipeTop();<br>                    }<br>                    result = <strong>true</strong>;<br>                }<br>            } <strong>catch </strong>(Exception exception) {<br>                exception.printStackTrace();<br>            }<br>            <strong>return </strong>result;<br>        }<br>    }<br>}</pre><p>and create <strong>EventItem </strong>as data model to keep data of event in Calender named as <strong>EventItem.kt</strong></p><pre><strong>data class </strong>EventItem(<strong>var start</strong>: String, <strong>var end</strong>: String, <strong>var title</strong>: String = <strong>&quot;My Event&quot;</strong>, <strong>var color</strong>: String = <strong>&quot;#00FFFF&quot;</strong>, <strong>var id</strong>: String = <strong>&quot;&quot;</strong>) {<br>    <strong>fun </strong>getStartDateToSort(): Date {<br>        <strong>val </strong>sdf = SimpleDateFormat(<strong>&quot;dd-MM-yyyy&quot;</strong>, Locale.getDefault())<br>        <strong>return </strong>sdf.parse(<strong>start</strong>)<br>    }<br>}</pre><p>Now, the main part of the code, which shows the dates and events on the day or days, we’ll see in the next part.</p><p>Thank you for reading this article. If you liked this, you can make 1, 2, 5 or 50 claps for this. Your Support will make me more enthusiastic and excited to write many more such articles.</p><p>Thanks again…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa7e6677d586" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-kotlin/creating-material-designed-event-calender-part-1-fa7e6677d586">Creating Material Designed Event Calender Part-1</a> was originally published in <a href="https://medium.com/android-kotlin">Kotlindroid</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>