<?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 Muhammad Sayed on Medium]]></title>
        <description><![CDATA[Stories by Muhammad Sayed on Medium]]></description>
        <link>https://medium.com/@abualgaitad?source=rss-9e62956947c3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MWKdnC80NfQFJLT0FTIlrQ.png</url>
            <title>Stories by Muhammad Sayed on Medium</title>
            <link>https://medium.com/@abualgaitad?source=rss-9e62956947c3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 11:13:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abualgaitad/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[Creating a Fancy Progress Bar in Jetpack Compose: A Step-by-Step Guide]]></title>
            <link>https://medium.com/@abualgaitad/creating-a-fancy-progress-bar-in-jetpack-compose-a-step-by-step-guide-269d82d07b4f?source=rss-9e62956947c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/269d82d07b4f</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[compose]]></category>
            <category><![CDATA[gesture-control]]></category>
            <dc:creator><![CDATA[Muhammad Sayed]]></dc:creator>
            <pubDate>Sun, 25 Feb 2024 10:25:07 GMT</pubDate>
            <atom:updated>2024-02-25T12:13:27.491Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KTSYaXUgbbSPNMdZ" /><figcaption>Photo by <a href="https://unsplash.com/@tma?utm_source=medium&amp;utm_medium=referral">Tianyi Ma</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>Introduction:</strong></p><p>Progress bars are essential UI components in many applications, providing visual feedback on the completion of tasks. In this article, we’ll walk through the process of creating a fancy progress bar using Jetpack Compose, Google’s modern toolkit for building native Android UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*klRMP_szPL6OIVFuHIz5og.gif" /><figcaption>End result of FancyProgressBar</figcaption></figure><p><strong>Step 1: Set Up Your Project</strong></p><p>Before we dive into coding, ensure you have a basic understanding of Jetpack Compose and have set up a Compose-enabled project in Android Studio. and you have this dependency:</p><pre> implementation(&quot;androidx.constraintlayout:constraintlayout-compose:1.0.1&quot;)</pre><p><strong>Step 2: Define the FancyProgressBar Composable</strong></p><p>First, let’s define the FancyProgressBar composable function. This function takes the progressBarWidthInPx parameter, representing the width of the progress bar in pixels.</p><pre>@Composable<br>fun ContainerView(){<br>Box(modifier = Modifier<br>    .fillMaxSize()<br>    .background(Color(0xFF121113)),<br>    contentAlignment = Alignment.Center) {<br>       val progressBarWidth = 250<br>       with(LocalDensity.current) {<br>          FancyProgressBar(progressBarWidth.dp.toPx())<br>       }<br>    }<br>}<br><br>@Composable<br>fun FancyProgressBar(progressBarWidthInPx: Float) {<br>    // Composable implementation goes here<br>}</pre><p><strong>Step 3: Track Progress with State</strong></p><p>To animate the progress indicator and track its position, we’ll use state variables provided by Compose.</p><pre>var offsetX by remember { mutableStateOf(0f) }<br><br>val guidelinePercentage by remember {<br>    derivedStateOf {<br>        offsetX / progressBarWidthInPx<br>    }<br>}</pre><p><strong>Step 4: Define Animation Properties</strong></p><p>We’ll use animations to make the progress indicator interactive and visually appealing.</p><pre>val isAnimatePercentageUp by remember {<br>    derivedStateOf {<br>        guidelinePercentage &lt; 0.2f || guidelinePercentage &gt; 0.8f<br>    }<br>}<br><br>val animation = animateDpAsState(<br>    targetValue = if (isAnimatePercentageUp) -Dp(35f) else Dp(0f),<br>    label = &quot;Text Animation&quot;<br>)</pre><p><strong>Step 5: Create the Layout</strong></p><pre>Box(modifier = Modifier<br>    .layoutId(&quot;indicator&quot;)<br>    .offset { IntOffset(offsetX.roundToInt(), 0) }<br>    .width(5.dp)<br>    .background(shape = RoundedCornerShape(5.dp), color = Color.White)<br>    .pointerInput(Unit) {<br>        detectDragGestures { change, dragAmount -&gt;<br>            change.consume()<br>            offsetX = (offsetX + dragAmount.x)<br>                .coerceIn(0f, progressBarWidthInPx)<br>        }<br>    }<br>)</pre><p><strong>Step 6: Implement Drag Gesture Detection</strong></p><p>Allow users to interact with the progress bar by dragging the indicator.</p><pre>Box(modifier = Modifier<br>    .layoutId(&quot;indicator&quot;)<br>    .offset { IntOffset(offsetX.roundToInt(), 0) }<br>    .width(5.dp)<br>    .background(shape = RoundedCornerShape(5.dp), color = Color.White)<br>    .pointerInput(Unit) {<br>        detectDragGestures { change, dragAmount -&gt;<br>            change.consume()<br>            offsetX = (offsetX + dragAmount.x)<br>                .coerceIn(0f, progressBarWidthInPx)<br>        }<br>    }<br>)</pre><p><strong>Step 7: Display Progress Text</strong></p><p>Show the progress percentage on both sides of the indicator.</p><pre>Text(<br>    text = &quot;${String.format(&quot;%.0f&quot;, guidelinePercentage * 100)}%&quot;,<br>    modifier = Modifier<br>        .layoutId(&quot;leftPercentage&quot;)<br>        .offset(y = animation.value),<br>    color = Color.White.copy(alpha = 0.7f)<br>)<br><br>Text(<br>    text = &quot;${String.format(&quot;%.0f&quot;, (1 - guidelinePercentage) * 100)}%&quot;,<br>    modifier = Modifier<br>        .layoutId(&quot;rightPercentage&quot;)<br>        .offset(y = animation.value),<br>    color = Color.White.copy(alpha = 0.7f)<br>)</pre><p><strong>Conclusion</strong></p><p>Congratulations! You’ve successfully created a fancy progress bar in Jetpack Compose. Experiment with different shapes, colors, and animations to customize it further according to your app’s design.</p><p>Jetpack Compose simplifies the process of building rich and interactive UIs in Android apps. By following this guide, you’ve learned how to create a stylish progress bar with drag interaction and animation effects. Keep exploring Compose’s features to create even more engaging user experiences in your applications. Happy coding!</p><p><a href="https://github.com/abualgait/FancyProgressBar">GitHub - abualgait/FancyProgressBar: A custom progress bar component developed using Jetpack Compose.</a></p><blockquote>Please clap if you like this article, follow me for more, and let’s connect via <a href="https://www.linkedin.com/in/abualgait/">linkedin</a>.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=269d82d07b4f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Navigating Kotlin Compose: With Code Samples]]></title>
            <link>https://medium.com/@abualgaitad/navigating-kotlin-compose-with-code-samples-0c96371238b4?source=rss-9e62956947c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/0c96371238b4</guid>
            <category><![CDATA[compose-ui]]></category>
            <category><![CDATA[declarative-programming]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Muhammad Sayed]]></dc:creator>
            <pubDate>Mon, 12 Feb 2024 14:32:01 GMT</pubDate>
            <atom:updated>2024-02-12T14:32:01.543Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HUXiMwZQMlOE7cws" /><figcaption>Photo by <a href="https://unsplash.com/@fazurrehman?utm_source=medium&amp;utm_medium=referral">Faizur Rehman</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>Introduction:</strong></p><p>Kotlin Compose has emerged as a game-changer in Android UI development, offering a modern, declarative approach to building user interfaces. However, as with any new technology, developers may encounter challenges and pitfalls along the way. In this comprehensive guide, we’ll explore best practices for Kotlin Compose development, accompanied by code samples to illustrate each principle. By adhering to these guidelines, developers can streamline their development process, enhance code quality, and create more maintainable and efficient UIs for their Android applications.</p><p><strong>Understanding the Composable Function:</strong></p><p>The cornerstone of Kotlin Compose is the Composable function, which defines UI components. Let’s start with a simple example:</p><pre>import androidx.compose.runtime.Composable<br>import androidx.compose.foundation.Text<br><br>@Composable<br>fun Greeting(name: String) {<br>    Text(text = &quot;Hello, $name!&quot;)<br>}</pre><p>In this example, the Greeting function is annotated with @Composable, indicating that it is a Composable function. It takes a name parameter and displays a greeting message using the Text composable.</p><p><strong>State Management:</strong></p><p>Effective state management is crucial for building dynamic UIs in Kotlin Compose. Consider the following example of a counter:</p><pre>import androidx.compose.runtime.getValue<br>import androidx.compose.runtime.mutableStateOf<br>import androidx.compose.runtime.setValue<br><br>@Composable<br>fun Counter() {<br>    var count by remember { mutableStateOf(0) }<br>    Button(onClick = { count++ }) {<br>        Text(text = &quot;Count: $count&quot;)<br>    }<br>}</pre><p>In this example, we use the mutableStateOf function to create a mutable state variable count. The Button composable increments the count when clicked, and the Text composable displays the current count.</p><p><strong>Composition Over Inheritance:</strong></p><p>Kotlin Compose encourages composition over inheritance, promoting the creation of small, reusable components. Here’s an example of a custom button:</p><pre>import androidx.compose.runtime.Composable<br>import androidx.compose.material.Button<br>import androidx.compose.foundation.Text<br><br>@Composable<br>fun CustomButton(text: String) {<br>    Button(onClick = { /* Handle button click */ }) {<br>        Text(text = text)<br>    }<br>}</pre><p>Instead of subclassing existing components, we compose them together to create a custom button with the desired functionality and appearance.</p><p><strong>Handling Side Effects:</strong></p><p>Avoid performing side effects directly inside Composables, as it can lead to unpredictable behavior and hinder code readability. Instead, use coroutines to handle side effects asynchronously, much better delegating ViewModel for handling network calls:</p><pre>import androidx.compose.runtime.Composable<br>import androidx.compose.runtime.remember<br>import kotlinx.coroutines.launch<br>import kotlinx.coroutines.Dispatchers<br>import kotlinx.coroutines.withContext<br><br>@Composable<br>fun LoadData() {<br>    val data = remember { mutableStateOf(&quot;&quot;) }<br><br>    LaunchedEffect(true) {<br>        withContext(Dispatchers.IO) {<br>            val result = fetchDataFromNetwork()<br>            data.value = result<br>        }<br>    }<br>}</pre><p>In this example, we use LaunchedEffect to execute a coroutine that fetches data from the network asynchronously. This ensures that the UI remains responsive while the data is being loaded.</p><p>Additionally, here is a great <a href="https://www.youtube.com/watch?v=gxWcfz3V2QE">video</a> by <a href="https://medium.com/u/e856b68b3836">Philipp Lackner</a> about side effects that wraps them up.</p><p><strong>Testing:</strong></p><p>Testing is essential for ensuring the correctness and reliability of UI components. Kotlin Compose provides testing utilities for writing UI tests. Here’s an example of a UI test for the Greeting function:</p><pre>import androidx.compose.ui.test.junit4.createComposeRule<br>import androidx.compose.ui.test.onNodeWithTag<br>import org.junit.Rule<br>import org.junit.Test<br>import org.junit.runner.RunWith<br>import org.junit.runners.JUnit4<br><br>@RunWith(JUnit4::class)<br>class ExampleUnitTest {<br><br>    @get:Rule<br>    val composeTestRule = createComposeRule()<br><br>    @Test<br>    fun testGreetingText() {<br>        composeTestRule.setContent {<br>            Greeting(name = &quot;World&quot;)<br>        }<br><br>        composeTestRule.onNodeWithTag(&quot;greeting_text&quot;).assertTextEquals(&quot;Hello, World!&quot;)<br>    }<br>}</pre><p>In this test, we use createComposeRule to create a test rule for composing UI elements. We then set the content to the Greeting function with the name &quot;World&quot; and assert that the text matches the expected greeting message.</p><p><strong>Conclusion:</strong></p><p>Kotlin Compose offers a powerful and intuitive way to build UIs for Android apps. By following best practices and leveraging the provided code samples, developers can navigate the Kotlin Compose landscape more effectively, avoiding common pitfalls and creating robust, maintainable UIs. As the ecosystem continues to evolve, staying informed about best practices and incorporating them into the development process will be crucial for success in Kotlin Compose development.</p><p>clap if you like this article, follow me for more, and let’s connect via <a href="https://www.linkedin.com/in/abualgait/">linkedin</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0c96371238b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>