<?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 Jeffrey Rajan on Medium]]></title>
        <description><![CDATA[Stories by Jeffrey Rajan on Medium]]></description>
        <link>https://medium.com/@jeffreyrajan?source=rss-8ec9871fbd3c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*-hUQpUCKmrAApvI3V1IOwA.jpeg</url>
            <title>Stories by Jeffrey Rajan on Medium</title>
            <link>https://medium.com/@jeffreyrajan?source=rss-8ec9871fbd3c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 16:43:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jeffreyrajan/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[Canvas Made Easy In Jetpack Compose]]></title>
            <link>https://jeffreyrajan.medium.com/canvas-made-easy-in-jetpack-compose-2c632518f8bf?source=rss-8ec9871fbd3c------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c632518f8bf</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-canvas]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[jetpack]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Jeffrey Rajan]]></dc:creator>
            <pubDate>Wed, 05 May 2021 05:25:42 GMT</pubDate>
            <atom:updated>2021-05-05T05:25:42.696Z</atom:updated>
            <content:encoded><![CDATA[<p>Canvas in Android, which is something I always hesitant to try because it requires several boilerplate codes to be setup and that made it very difficult to understand. However, when I started using the Jetpack Compose, I realised designing a screen has become a lot easier than ever. This motivated me further to revisit Canvas again. This time it changed my idea about Canvas. If you are also like me who hesitated to work on Canvas before, then let me convince you to try it again with three simple examples.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*ylt8wSudErxGg8kPuWUoWA.gif" /></figure><p>What we will be creating from this article?</p><ul><li>Right angled triangle progress bar.</li><li>Circular progress bar.</li><li>Gauge progress bar with its current progress.</li></ul><p><strong>Right Angled Triangle</strong></p><p>Right angled triangle can be created in three steps; First, we will be creating the triangle, then we will be having the overlay over the triangle and then the overlay will be changed dynamically. Before starting to create, we need to understand, how these canvas object will be placed on the screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*A2jSpMMmnqhxapLH2YU84Q.png" /></figure><p>All our screens are filled with pixels. In simple words, when zoomed in the screen, we will be able to see millions of boxes. Each box is considered as a pixel. Basic idea of Canvas is, when those pixels are filled with colors, which will in return shows an image on the screen.</p><p>By default, Canvas starts drawing from first pixel (x = 0, y = 0). Take for example, we will use Path to draw the right angled triangle.</p><pre>Canvas(modifier = Modifier<br>    .<em>width</em>(300.<em>dp</em>)<br>    .<em>height</em>(150.<em>dp</em>)<br>    .<em>padding</em>(16.<em>dp</em>)<br>) <strong>{<br>    </strong>val path = Path()<br>    path.moveTo(size.width, 0f)<br>    path.lineTo(size.width, size.height)<br>    path.lineTo(0f, size.height)<br><br>    drawPath(<br>        path = path,<br>        brush = SolidColor(Color.LightGray)<br>    )<br><strong>}</strong></pre><p>Before we look into the above code, let’s see how this get rendered in the screen.</p><p>As we saw earlier, all our canvas will start from (0, 0), meaning our curser will start at the position of x=0 and y=0, which is the top-left of our screen. First, we need to move our cursor to the position of 300dp which was the width of our View. To move we will be using the path.moveTo() method with the position of x = 300dp and y = 0dp. Once it is moved, we need to draw a line 90° to the bottom at the position of x = 300dp and y = 150dp, where we will take the width and height of our Canvas view. Again, draw a line of 180° to the position of x = 0dp and y = 150dp.</p><p>Once the path is done, pass it to the drawPath method. This will fill out the remaining line and make it as a right angled triangle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*OGplk3HLOnwhRu-RHDAsFw.png" /></figure><p>We have drawn a right angled triangle as shown in the above image. Now we need to fill the triangle to half with green color, to make it lookalike a progress of 50%. To replicate the progress of 50%, we will be using drawRect method.</p><pre><em>Canvas</em>(modifier = Modifier<br>    .width(300.dp)<br>    .height(150.dp)<br>    .padding(16.dp)<br>) {<br>    val path = Path()<br>    path.moveTo(size.width, 0f)<br>    path.lineTo(size.width, size.height)<br>    path.lineTo(0f, size.height)<br>    drawPath(<br>        path = path,<br>        brush = SolidColor(Color.LightGray)<br>    )<br><br>    drawRect(<br>        SolidColor(Color.Green),<br>        size = Size(<br>            0.5f * size.width,<br>            size.height<br>        )<br>    )<br><strong>}</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*LMiuscYa1qiqin4D4VOS2w.png" /></figure><p>Hmm… That’s interesting! But the reality is different from what we expect. Let’s fix this by clipping the unwanted space.</p><pre><em>Canvas</em>(modifier = Modifier<br>    .width(300.dp)<br>    .height(150.dp)<br>    .padding(16.dp)<br>) {<br>    val path = Path()<br>    path.moveTo(size.width, 0f)<br>    path.lineTo(size.width, size.height)<br>    path.lineTo(0f, size.height)<br>    clipPath(<br>        path = path,<br>        clipOp = ClipOp.Intersect<br>    ) {<br>        drawPath(<br>            path = path,<br>            brush = SolidColor(Color.LightGray)<br>        )<br><br>        drawRect(<br>            SolidColor(Color.Green),<br>            size = Size(<br>                0.5f * size.width,<br>                size.height<br>            )<br>        )<br>    }<br><strong>}</strong></pre><p>We can use clipPath method by passing the right angled triangle path. Next, we need to specify the type of clipOp that we intent to use (ClipOp.Intersect or ClipOp.Difference). If we try using the ClipOp as ClipOp.Difference, this will make the given path as transparent. But we wanted the opposite of it, so let’s use ClipOp.Intersect instead. This will allow us to draw only on the given path. Not to mention, remaining path will remain transparent and restrict us from drawing further. The above code will produce the below Canvas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*W4g3Tf4Ru6T1gv68pQQqmA.png" /></figure><p>The above progress can be made dynamic by modifying the value 0.5f from the drawRect() method, but you will want to make sure to pass the value in a state and the value must be between 0f to 1.0f.</p><pre>val sliderValue by <em>remember </em><strong>{ </strong><em>mutableStateOf</em>(0.5f) <strong>}<br></strong>…<br>drawRect(<br>    SolidColor(Color.Green),<br>    size = Size(<br>        sliderValue * size.width,<br>        size.height<br>    )<br>)</pre><p><strong>Circular Progress Bar</strong></p><p>Creating a circular progress bar will be so much simpler than creating a right angled triangle. To create a circular progress, we will be following three steps; creating a circle, creating a progress using an arc and having a state to make the progress as dynamic. First, let’s create a circle</p><pre><em>Canvas</em>(<br>    modifier = Modifier<br>        .<em>size</em>(250.<em>dp</em>)<br>        .<em>padding</em>(16.<em>dp</em>)<br>) <strong>{<br>    </strong>drawCircle(<br>        SolidColor(Color.LightGray),<br>        size.width / 2,<br>        style = Stroke(35f)<br>    )<br><strong>}</strong></pre><p>The above code will create a circle with a radius of 125, where the radius comes from the Canvas width of 250dp. In drawCircle method, we have used three parameters: Brush, radius and style. Brush will be used to provide a different kind of shaders to the paint like SolidColor, RadialGradient, LinearGradient etc. In style, we have two types of DrawStyle: Fill and Stroke. In our case we will be using the Stroke with a width of 35f.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZVACJu68OrU2ObQuAnT0JQ.png" /></figure><p>Now, we will create a progress bar using drawArc(). Before getting into the drawArc, we need to understand, how a sweep and a start angle works? Start Angle starts from 3 in our clock and the series of angle will be created in a clockwise manner. Whereas in sweep angle, all the angles are created in an anti-clockwise manner.</p><p>Along with the arc we will complete our example with a dynamic progress value using a mutableState.</p><pre>val sliderValue by <em>remember </em><strong>{ </strong><em>mutableStateOf</em>(0.5f) <strong>}<br><br></strong><em>Canvas</em>(<br>    modifier = Modifier<br>        .<em>size</em>(250.<em>dp</em>)<br>        .<em>padding</em>(16.<em>dp</em>)<br>) <strong>{<br>    </strong>drawCircle(<br>        SolidColor(Color.LightGray),<br>        size.width / 2,<br>        style = Stroke(35f)<br>    )<br>    val convertedValue = sliderValue * 360<br>    drawArc(<br>        brush = SolidColor(Color.Black),<br>        startAngle = -90f,<br>        sweepAngle = convertedValue,<br>        useCenter = false,<br>        style = Stroke(35f)<br>    )<br><strong>}</strong></pre><p>The above code will create a UI like below. If you notice the startAngle from the drawArc() method, I have used the value as -90f instead of 90f. The reason is, I need my progress to start from the 90° and should progress in a clockwise manner. If I provided a positive 90°, the progress will be moving in an anti-clockwise way. In sweepAngle we will be multiplying float value(0f to 1.0f) with 360° to replicate progress.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*aquHXoWwsKo4WwGfY5_fcA.png" /></figure><p><strong>Gauge Circle</strong></p><p>So far, we have created a right angled triangle and a circular progress bar. Now we will create a Gauge progress bar with a current progress in the centre of the View. To create a gauge, we will be using drawArc() method.</p><pre><em>Canvas</em>(<br>    modifier = Modifier<br>        .<em>width</em>(250.<em>dp</em>)<br>        .<em>height</em>(250.<em>dp</em>)<br>        .<em>padding</em>(16.<em>dp</em>)<br>) <strong>{<br>    </strong>drawArc(<br>        brush = SolidColor(Color.LightGray),<br>        startAngle = 120f,<br>        sweepAngle = 300f,<br>        useCenter = false,<br>        style = Stroke(35f, cap = StrokeCap.<em>Round</em>)<br>    )<br><strong>}</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*-559OjRTjMWMGQxXH0rZyg.png" /></figure><p>We need to revisit the sweep and start angle image again to understand why we have given the startAngle as 120f and sweepAngle as 300f.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JQVhXrYjNeZmbmgcjOGUBA.png" /></figure><p>As we saw above, the startAngle will draw in a clockwise and sweepAngle will draw the arc in an anti-clockwise manner. To create a perfect Gauge View, we need to provide the sweep and the start angle value as 300f and 120f respectively. Those values are marked in the above image to show you a clear picture of sweep and start angle values. To provide a style we have used Stroke, in Stroke we have introduced a new parameter cap. If there is no cap was provided, then we will get the boxed corner at each edge of Gauge View. To make it as a rounded corner we have used StrokeCap.Round.</p><p>Next, let’s create another arc to make it look like a progress.</p><pre>val sliderValue by <em>remember </em><strong>{ </strong><em>mutableStateOf</em>(0.5f) <strong>}<br><br></strong><em>Canvas</em>(<br>    modifier = Modifier<br>        .<em>width</em>(250.<em>dp</em>)<br>        .<em>height</em>(250.<em>dp</em>)<br>        .<em>padding</em>(16.<em>dp</em>)<br>) <strong>{<br>    </strong>drawArc(<br>        brush = SolidColor(Color.LightGray),<br>        startAngle = 120f,<br>        sweepAngle = 300f,<br>        useCenter = false,<br>        style = Stroke(35f, cap = StrokeCap.<em>Round</em>)<br>    )<br><br>    val convertedValue = sliderValue * 300<br><br>    drawArc(<br>        brush = SolidColor(Color.Cyan),<br>        startAngle = 120f,<br>        sweepAngle = convertedValue,<br>        useCenter = false,<br>        style = Stroke(35f, cap = StrokeCap.<em>Round</em>)<br>    )<br><strong>}</strong></pre><p>In the above code snippet we have multiplied the sliderValue with 300. The reason for using this value is, if we look into our first arc the sweep angle was 300f which is the max value of the Gauge View. So, we need to multiply the float value from the state with 300 to achieve the percentage of the progress. The same approach was followed in right angled triangle and the circular progress bar.</p><p>Now, let’s add the progress in percentage to the centre of the View.</p><pre>Canvas(…) <strong>{<br>    </strong>drawArc(…)<br><br>    val convertedValue = sliderValue * 300<br><br>    drawArc(…)<br><br>    drawIntoCanvas <strong>{<br>        </strong>val paint = <em>Paint</em>().asFrameworkPaint()<br>        paint.<em>apply </em><strong>{<br>            </strong><em>isAntiAlias </em>= true<br>            <em>textSize </em>= 55f<br>            <em>textAlign </em>= android.graphics.Paint.Align.<em>CENTER<br>        </em><strong>}<br>        </strong>it.nativeCanvas.drawText(<br>            &quot;${<em>round</em>(sliderValue * 100).toInt()}%&quot;,<br>            size.width / 2,<br>            size.height / 2,<br>            paint<br>        )<br>    <strong>}<br>}</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*C6ronMft1cJevpvJKzV0sA.png" /></figure><p>While writing this article, not all options are available in the Compose library. For some options we still need to depend on the android.graphics Canvas, which is a native canvas. Henceforth, we have used the native drawText to create a text in Canvas.</p><p>In all our examples we never used invalidate(), the reason is, recomposition will happen only when the state changes. Thus it is not necessary to use the invalidate() in Jetpack Compose.</p><p><a href="https://github.com/jeffreyrajanofficial/Canvas-Loader"><strong>Github</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c632518f8bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Jetpack Compose — Getting Started]]></title>
            <link>https://jeffreyrajan.medium.com/jetpack-compose-getting-started-983f89d79823?source=rss-8ec9871fbd3c------2</link>
            <guid isPermaLink="false">https://medium.com/p/983f89d79823</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[jetpack]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[declarative-ui]]></category>
            <dc:creator><![CDATA[Jeffrey Rajan]]></dc:creator>
            <pubDate>Mon, 01 Mar 2021 19:27:07 GMT</pubDate>
            <atom:updated>2021-03-01T19:32:46.341Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Jetpack Compose — Getting Started</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hEDtdq8SwieAbYvnvcD42Q.png" /></figure><p>Requirements</p><ul><li>Android Studio 4.2 Preview or higher.</li></ul><p>Declarative UI has become a trend in mobile application development like React Native, Flutter, and SwiftUI. In the same way, now Android has bought the Composable UI in the form of Jetpack compose.</p><p>Let’s dive in and see how we can write a Composable UI.</p><ol><li>Add the below configuration in your app build.gradle</li></ol><pre>android <strong>{</strong></pre><pre>kotlinOptions <strong>{<br>    </strong>jvmTarget = &#39;1.8&#39;<br>    useIR = true<br>  <strong>}<br>  </strong>buildFeatures <strong>{<br>    </strong>compose true<br>  <strong>}<br>  </strong>composeOptions <strong>{<br>    </strong>kotlinCompilerExtensionVersion &#39;1.0.0-beta01&#39;<br>    kotlinCompilerVersion &#39;1.4.30&#39;<br>  <strong>}</strong></pre><pre><strong>}</strong></pre><p>2. Add the below dependencies</p><pre>implementation “androidx.compose.ui:ui:1.0.0-beta01”<br>implementation “androidx.compose.material:material:1.0.0-beta01”<br>implementation “androidx.ui:ui-tooling:1.0.0-beta01”</pre><p>The configuration part is done. Let’s start with our coding after the gradle sync is done. Allow me to begin with the basic Hello World example.</p><pre>@Preview(name = &quot;Hello world&quot;, showBackground = true)<br>@Composable<br>fun Greetings() {<br>   Text(text = &quot;Hello World!&quot;)<br>}</pre><p>In the above example, we are using two annotations for a method to create a Composable UI. To make a method composable, you need to use the Composable annotation (@Composable), and to preview the composable UI in a design layout, you need to provide the annotation as @Preview.</p><p>We have used two parameters in the @Preview annotation, one is name, and another one is showBackground. When you want to provide some name to the view, you can use the name parameter. This name parameter will help you identify the UI when many composable methods were created in the same file.</p><p>Like our traditional layouts, the Composable UI also has a transparent background. To have a clear view, we can use showBackground with the value as true. This will create a white background for your view, but don’t worry; this background will not be reflected on your screen when you run the application on a real device or emulator.</p><p>Inside the Greetings method, we have a Text which is the same as we have TextView in the ViewGroups.</p><p>We all mostly use LinearLayout and RelativeLayout to create our layouts. Likewise in Jetpack Compose, we have Column(Vertical), Row(Horizontal), and Box.</p><p>To create a ViewGroups horizontally, you need to use the Row method in Jetpack Compose.</p><pre>@Preview(name = &quot;Row Example&quot;, showBackground = true)<br>@Composable<br>fun RowContent() {<br>  Row(<br>     modifier = Modifier.<em>fillMaxWidth</em>(),<br>     horizontalArrangement = Arrangement.SpaceBetween<br>) {<br>     <em>Text</em>(text = &quot;text1&quot;)<br>     <em>Text</em>(text = &quot;text2&quot;)<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*ac_3QQw6fLgfq_L4USTOOw.png" /></figure><p>We have used two new parameters(modifier and horizontalArrangement) for the Row method in the above example. The modifiers are used to create a style for the views. In our case, we need only the fillMaxWidth method to fill our width, which is the same as that we use in the XML as android:layout_width=”match_parent”. In order to have a space between the Views inside the Row block, we need to use horizontalArrangement value as Arrangement.SpaceBetween.</p><p>Now let’s create a view vertically.</p><pre>@Preview(&quot;Column Example&quot;)<br>@Composable<br>fun ColumnContent() {<br>   <em>Column</em>(modifier =<br>       Modifier.<em>fillMaxSize</em>().<em>background</em>(Color.LightGray)) <strong>{<br>      </strong>Text(text = &quot;Title&quot;)<br>      <em>Text</em>(text = &quot;Content&quot;)<br>   <strong>}<br></strong>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*KpH3NvJ0iWJbUnW10EvkHw.png" /></figure><p>The Column and Row composable method work precisely like the LinearLayout with the orientation as Vertical/Horizontal. At this point, you would have already guessed the Box composable method should work like a RelativeLayout. If you have thought that, then you are right, the Box method works similar to RelativeLayout.</p><pre>@Preview(&quot;Box Example&quot;, showBackground = true)<br>@Composable<br>fun BoxContent() {<br> Box(<br>    modifier = Modifier.<em>fillMaxWidth</em>(),<br>    alignment = Alignment.Center<br>) {<br>   Box(modifier = Modifier<br>                   .<em>preferredSize</em>(50.<em>dp</em>)<br>                   .<em>clip</em>(<em>CircleShape</em>)<br>                   .<em>background</em>(Color.Blue)<br>      )<br>      IconButton(onClick = {}) {<br>         <em>Icon</em>(Icons.Filled.<em>Favorite</em>)<br>      }<br>   }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OB6gciGQJMeorrIs9mI7mg.png" /></figure><p>ViewGroups will have ImageView and ImageButton, but in Jetpack Compose, we need to use Image(works like an ImageView) and IconButton (ImageButton).</p><p>Unlike ViewGroups layout, all the UI in the Composable UI renders in a flat hierarchy. Thus enclosing your Composable UI with multiple UI components will not create a large hierarchy tree.</p><p>The other advantage of using the Jetpack Compose is, you can reuse the same Composable UI for Kotlin Native, which was used to create a cross-platform app for Android, iOS, macOS, and Windows.</p><p><a href="https://github.com/jeffreyrajanofficial/Getting-started-with-Jetpack-Compose/tree/master"><strong>Github</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=983f89d79823" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>