<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Inspire Coding</title>
    <description>The latest articles on Forem by Inspire Coding (@inspire_coding).</description>
    <link>https://forem.com/inspire_coding</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F451847%2F9e18ed97-93f6-4a15-9feb-32b6257de86b.jpg</url>
      <title>Forem: Inspire Coding</title>
      <link>https://forem.com/inspire_coding</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/inspire_coding"/>
    <language>en</language>
    <item>
      <title>Android - Custom Toast with Kotlin extension function</title>
      <dc:creator>Inspire Coding</dc:creator>
      <pubDate>Tue, 17 Nov 2020 14:41:01 +0000</pubDate>
      <link>https://forem.com/inspire_coding/android-custom-toast-with-kotlin-extension-function-23pj</link>
      <guid>https://forem.com/inspire_coding/android-custom-toast-with-kotlin-extension-function-23pj</guid>
      <description>&lt;p&gt;&lt;strong&gt;In this short tutorial I'm going to show you how you can customize your Toast messages using Kotlin extension function.&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 1: Create a new project
&lt;/h1&gt;

&lt;p&gt;First of all, create a new Android Studio project.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 2: Create a layout
&lt;/h1&gt;

&lt;p&gt;The second step is to create a very simple layout. In our case it will contain only an &lt;code&gt;EditText&lt;/code&gt; and a &lt;code&gt;Button&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Copy and paste the below code into the &lt;code&gt;activity_main.xml&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;activity_main.xml

&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"&amp;gt;

    &amp;lt;EditText
        android:id="@+id/et_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_margin="24dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_bias="0.3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" /&amp;gt;

    &amp;lt;Button
        android:id="@+id/btn_customToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Custom Toast"
        app:layout_constraintTop_toBottomOf="@+id/btn_defaultToast"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/&amp;gt;

&amp;lt;/androidx.constraintlayout.widget.ConstraintLayout&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 3: Create an icon
&lt;/h1&gt;

&lt;p&gt;Our &lt;code&gt;Toast&lt;/code&gt; message will have an icon as well. So, create a checkmark with the below detailes into the drawable folder.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search for done to find the icon&lt;/li&gt;
&lt;li&gt;Create a color in the &lt;code&gt;colors.xml&lt;/code&gt; file with the name of green: &lt;code&gt;#78C257&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Step 4: Create a shape
&lt;/h1&gt;

&lt;p&gt;The custom Toast message will have&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a green border&lt;/li&gt;
&lt;li&gt;a light green background&lt;/li&gt;
&lt;li&gt;and rounded corners&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, create a new shape in the &lt;code&gt;drawable&lt;/code&gt; folder and paste into it the below lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shape_roundedcorners

&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;shape
    xmlns:android="http://schemas.android.com/apk/res/android"&amp;gt;

    &amp;lt;solid
        android:color="@color/light_green"/&amp;gt;
    &amp;lt;corners
        android:radius="18dp"/&amp;gt;
    &amp;lt;stroke
        android:color="@color/green"
        android:width="3dp"/&amp;gt;

&amp;lt;/shape&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Hex-code for the &lt;code&gt;light_green&lt;/code&gt; color: &lt;code&gt;#B9D7AC&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 5: Create the layout
&lt;/h1&gt;

&lt;p&gt;The next is to create the custom layout for the &lt;code&gt;Toast&lt;/code&gt; message. So, create a new layout file in the &lt;code&gt;res &amp;gt; layout&lt;/code&gt; folder with the name of &lt;code&gt;custom_toast&lt;/code&gt;. Then paste into it the below lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;custom_toast

&amp;lt;LinearLayout
    android:id="@+id/cl_customToastContainer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="12dp"
    android:orientation="horizontal"
    android:gravity="center"
    android:background="@drawable/shape_roundedcorners"&amp;gt;

    &amp;lt;ImageView
        android:id="@+id/iv_done"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_done_green" /&amp;gt;

    &amp;lt;TextView
        android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mission Completed"
        android:textColor="@color/green"
        android:layout_marginStart="6dp" /&amp;gt;

&amp;lt;/LinearLayout&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 6: Create the extension function
&lt;/h1&gt;

&lt;p&gt;In this step first, we have to create a new package and file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New Package: &lt;code&gt;util&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;New file: &lt;code&gt;ViewExt.kt&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Copy and paste the below code into the new &lt;code&gt;ViewExt.kt&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Extension function of custom Toast 

fun Toast.showCustomToast(message: String, activity: Activity)
{
    val layout = activity.layoutInflater.inflate (
        R.layout.custom_toast,
        activity.findViewById(R.id.cl_customToastContainer)
    )

    val textView = layout.findViewById&amp;lt;TextView&amp;gt;(R.id.tv_message)
    textView.text = message

    this.apply {
        setGravity(Gravity.BOTTOM, 0, 40)
        duration = Toast.LENGTH_LONG
        view = layout
        show()
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notes&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using the &lt;code&gt;LayoutInflater&lt;/code&gt; we gonna make instance about the layout&lt;/li&gt;
&lt;li&gt;Set the text of the &lt;code&gt;TextView&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Then we can call the &lt;code&gt;apply&lt;/code&gt; scope function to set the gravity, the duration and the view.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Step 7: Set the click listener
&lt;/h1&gt;

&lt;p&gt;The last step is to show our custom &lt;code&gt;Toast&lt;/code&gt; message when we click on the &lt;code&gt;Button&lt;/code&gt;. So, open the &lt;code&gt;MainActivity&lt;/code&gt; and paste the below code into the &lt;code&gt;onCreate()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
btn_customToast 

btn_customToast.setOnClickListener {_button -&amp;gt;
    Toast(this).showCustomToast (
        et_message.text.toString().trim(),
        this
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Run the app
&lt;/h1&gt;

&lt;p&gt;Run and test the app. ;)&lt;/p&gt;




&lt;h1&gt;
  
  
  More detailes
&lt;/h1&gt;

&lt;p&gt;The whole tutorial is available on my website&lt;/p&gt;

&lt;p&gt;&lt;a href="https://inspirecoding.app/custom-toast/"&gt;Custom Toast - Inspire Coding - Android development in Kotlin&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And you can check the source code on GitHub as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/inspire-coding/CustomToastDemo"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;code&gt;Have a nice day :)&lt;/code&gt;
&lt;/h1&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;More Android tutorials&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you would like to do more Android tutorials like this, then visit my website:&lt;br&gt;
&lt;a href="https://inspirecoding.app/"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6Do4izlZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://secureservercdn.net/160.153.137.218/yv6.cb0.myftpupload.com/wp-content/uploads/2019/11/cropped-Header-of-the-website.png" alt="Inspire Coding"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>toast</category>
      <category>extensionfunction</category>
    </item>
    <item>
      <title>Android MotionLayout – Clearing weather</title>
      <dc:creator>Inspire Coding</dc:creator>
      <pubDate>Mon, 16 Nov 2020 16:51:56 +0000</pubDate>
      <link>https://forem.com/inspire_coding/android-motionlayout-clearing-weather-24ig</link>
      <guid>https://forem.com/inspire_coding/android-motionlayout-clearing-weather-24ig</guid>
      <description>&lt;p&gt;&lt;strong&gt;In this tutorial we are going to build up a simple clearing weather animation using MotionLayout.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This animation will contain a changing background color, and 2 moving clouds, which are going to move out to the left and right of the screen.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FclearingWeather.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FclearingWeather.gif" alt="Clearing weather"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are going to implement a click listener also. It means, the animation will start only when we click on it. &lt;/p&gt;

&lt;p&gt;When the animation is started and finished, then we gonna show a &lt;code&gt;Toast&lt;/code&gt; message. So, we gonna learn how to add events to the states of the Transition as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Android WeatherApp Course
&lt;/h3&gt;

&lt;p&gt;This tutorial is part of my WeatherApp course. In this course you can build up a whole application using the OpenWeatherMaps API.&lt;br&gt;
If you would like to do this course, then you can find it under the link: &lt;a href="https://inspirecoding.app/courses/weatherapp/" rel="noopener noreferrer"&gt;Android WeatherApp course&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  MotionLayout
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;MotionLayout&lt;/code&gt; is a layout type, where we can animate the views inside of a layout. Because &lt;code&gt;MotionLayout&lt;/code&gt; is the subclass of &lt;code&gt;ConstraintLayout&lt;/code&gt;, we can use all of its cool features.&lt;/p&gt;

&lt;p&gt;In a &lt;code&gt;MotionLayout&lt;/code&gt; we can animate not only the transitions between the layouts, we can either animate the layout properties as well.&lt;/p&gt;

&lt;p&gt;And one more awesome thing, that we can handle events like click and swipe also. Using this feature we can build up much better user interfaces to reach better user experiences.&lt;/p&gt;

&lt;p&gt;MotionLayout uses &lt;code&gt;XML&lt;/code&gt; to declare the transitions no matter how complex it is.&lt;/p&gt;
&lt;h3&gt;
  
  
  Tags
&lt;/h3&gt;

&lt;p&gt;Before we can start the implementation of the tutorial, we should clarify some tags, what we are going to use for this example animation.&lt;/p&gt;

&lt;p&gt;These tags are identified in an XML file, which describes the animation. For example the start and the end transitions, moreover the changes of the attributes.&lt;/p&gt;

&lt;p&gt;This XML file will be automatically generated by Android Studio when we will convert the &lt;code&gt;ConstraintLayout&lt;/code&gt; to MotionLayout.&lt;/p&gt;

&lt;p&gt;The name of this XML is &lt;code&gt;MotionScene&lt;/code&gt;, which describes the animation.&lt;/p&gt;

&lt;p&gt;There are many tags, but from them we gonnna use the below ones.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;Transition&amp;gt;&lt;/code&gt;: It specifies the animation duration, trigger and how should the views move.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;OnClick&amp;gt;&lt;/code&gt;: As we have talked about, we can add a click event to the transition…&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;KeyFrameSet&amp;gt;&lt;/code&gt;: Inside of this tag we can add some changes to the views and we can specify in which position of the transition should be done the attribute-change.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;ConstraintSet&amp;gt;&lt;/code&gt;: This tag will specify the start and the end constraints of the transition.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;Constraint&amp;gt;&lt;/code&gt;: With this attribute we can match views from the layout XML file to the corresponding ConstraintSet.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;Layout&amp;gt;&lt;/code&gt;: It will describe the above mentioned views.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;PropertySet&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;Transform&amp;gt;&lt;/code&gt;: With these attributes we can specify some properties of the above mentioned views.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It can be that after this description you feel yourself a bit lost, but now we will start to implement the sample transition and these attributes gonna be cleaner, I promise. 😎&lt;/p&gt;
&lt;h1&gt;
  
  
  Step 1 – Create new project
&lt;/h1&gt;

&lt;p&gt;Our first step is to create a whole new project. For this, launch Android Studio. If you see the &lt;strong&gt;“Welcome page”&lt;/strong&gt;, then click on the &lt;strong&gt;“Start a new Android Studio project”&lt;/strong&gt;. If you have an open project, then from the &lt;strong&gt;“File”&lt;/strong&gt; menu select &lt;strong&gt;“New”&lt;/strong&gt;, then &lt;strong&gt;“New Project”&lt;/strong&gt;. After thet, this window will popup.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F02%2Fcreate_new_android_studio_project.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F02%2Fcreate_new_android_studio_project.png" alt="Create a new project"&gt;&lt;/a&gt;&lt;br&gt;
Here select the &lt;strong&gt;“Empty Activity”&lt;/strong&gt; option. In the next window, we should provide some information about the newly created app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FAndroidStudio_createNewProject.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FAndroidStudio_createNewProject.png" alt="Create a new project"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This tutorial will be written in &lt;code&gt;Kotlin&lt;/code&gt;. So, select from the dropdown list the &lt;code&gt;Kotlin&lt;/code&gt; language.&lt;/p&gt;

&lt;p&gt;From the next list of the &lt;strong&gt;“Minimum SDK”&lt;/strong&gt; select API 21. In our case API 21 gonna be enough.&lt;/p&gt;

&lt;p&gt;If you are done, click on the &lt;strong&gt;“Finish”&lt;/strong&gt; button. The build can take few minutes. Be patient! 😉&lt;/p&gt;

&lt;p&gt;When the build has been finished, then you should see the open &lt;code&gt;MainActivity::class&lt;/code&gt; and next to this the &lt;code&gt;activity_main.xml&lt;/code&gt; files.&lt;/p&gt;
&lt;h1&gt;
  
  
  Step 2 - Update dependency
&lt;/h1&gt;

&lt;p&gt;We are going to start this chapter by updating the dependencies of ConstraintLayout for the app. First, open the Module build.gradle file from the left Project pane.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F06%2FAndroidStudio_openTheModuleBuildGradle.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F06%2FAndroidStudio_openTheModuleBuildGradle.png" alt="Module build.gradle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then in the &lt;code&gt;dependencies{}&lt;/code&gt; section find this line:&lt;br&gt;
&lt;code&gt;implementation ‘androidx.constraintlayout:constraintlayout:1.1.3’&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To start using &lt;code&gt;MotionLayout&lt;/code&gt; in our project, we have to update the version of &lt;code&gt;ConstraintLayout&lt;/code&gt; at least to version &lt;code&gt;2.0.0&lt;/code&gt;. You can find the highest version under the link: &lt;a href="https://developer.android.com/jetpack/androidx/releases/constraintlayout" rel="noopener noreferrer"&gt;&lt;code&gt;Constraintlayout&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When this tutorial was writen, this version was &lt;code&gt;2.0.0-rc1&lt;/code&gt;, so update the above implementation line to &lt;/p&gt;

&lt;p&gt;&lt;code&gt;implementation ‘androidx.constraintlayout:constraintlayout:2.0.0-rc1‘&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next sync the project by clicking on the &lt;code&gt;Sync now&lt;/code&gt; button at the top right corner of Android Studio.&lt;/p&gt;

&lt;p&gt;That’s all what we have to do to start using &lt;code&gt;MotionLayout&lt;/code&gt; in our project.&lt;/p&gt;
&lt;h1&gt;
  
  
  Step 3 - Convert to MotionLayout
&lt;/h1&gt;

&lt;p&gt;Next we will convert &lt;code&gt;ConstraintLayout&lt;/code&gt; of the &lt;code&gt;activity_main.xml&lt;/code&gt; file to &lt;code&gt;MotionLayout&lt;/code&gt;. To do this, open it from the &lt;code&gt;res &amp;gt; layout&lt;/code&gt; folders.&lt;/p&gt;

&lt;p&gt;Open the design view at the right side of Android Studio. Then go to the &lt;code&gt;Component Tree&lt;/code&gt; pane. Click on &lt;code&gt;ConstraintLayout&lt;/code&gt; with the right mouse button and select the &lt;strong&gt;“Convert to MotionLayout”&lt;/strong&gt; option.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FAndroidStudio_convertToMotionLayout-1024x859.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FAndroidStudio_convertToMotionLayout-1024x859.png" alt="Convert to MotionLayout"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the conversation you should have the below window which is the MotionScene of the activity_main.xml file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FAndroidStudio_motionScene-2-1024x623.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FAndroidStudio_motionScene-2-1024x623.png" alt="MotionScene"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This layout describes the starter position of the &lt;code&gt;ConstraintSet&lt;/code&gt;…&lt;/li&gt;
&lt;li&gt;… and this is the end.&lt;/li&gt;
&lt;li&gt;After the conversation, our layout got a new attribute also, which describes the &lt;code&gt;MotionScene&lt;/code&gt; of the layout. You can find this XML file in the &lt;code&gt;res &amp;gt; xml&lt;/code&gt; folder called &lt;code&gt;activity_main_scene.xml&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Using the first icon you can add new &lt;code&gt;ConstraintSets&lt;/code&gt;, which is a new state what you can start for example from the end of the current &lt;code&gt;MotionScene&lt;/code&gt;. With the second icon you can add transitions and the third icon is for adding click or swipe event to the transitions.&lt;/li&gt;
&lt;li&gt;This is called Transition. Here you can play with the animation and check what is going to happen with the layout during the animation.&lt;/li&gt;
&lt;/ol&gt;
&lt;h1&gt;
  
  
  Step 4 - The assets
&lt;/h1&gt;

&lt;p&gt;Before we can start to implement the layout and the MotionScene, we have to add the clouds, the sun and 2 new colors to the &lt;code&gt;colors.xml&lt;/code&gt; file. Because this step is not the main part of this clearing weather tutorial, we will just add them by downloading the below &lt;code&gt;ZIP&lt;/code&gt; file.&lt;br&gt;
&lt;a href="https://developer.android.com/jetpack/androidx/releases/constraintlayout" rel="noopener noreferrer"&gt;Clearing Weather assets&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After downloading, unzip the file into the &lt;code&gt;res&lt;/code&gt; folder of your project.&lt;/p&gt;
&lt;h1&gt;
  
  
  Step 5 - The layout
&lt;/h1&gt;

&lt;p&gt;The next step is the implementation of the starter layout. So, open the &lt;code&gt;activity_main.xml&lt;/code&gt; file from the &lt;code&gt;res &amp;gt; layout&lt;/code&gt; folders and paste into it the below xml code.&lt;/p&gt;

&lt;p&gt;The layout will contain 2 &lt;code&gt;View&lt;/code&gt;s, which will be the background and 3 &lt;code&gt;ImageViews&lt;/code&gt; representing the clouds and the sun.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;activity_main.xml
&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;androidx.constraintlayout.motion.widget.MotionLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_main_scene"
    tools:context=".MainActivity"&amp;gt;
    &amp;lt;View
        android:id="@+id/view_background_gray"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="1"
        android:background="@color/light_grey"/&amp;gt;
    &amp;lt;View
        android:id="@+id/view_background_blue"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0"
        android:background="@color/light_blue"/&amp;gt;
    &amp;lt;ImageView
        android:id="@+id/iv_sun"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_sun"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" /&amp;gt;
    &amp;lt;ImageView
        android:id="@+id/iv_cloud_white"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:elevation="1dp"
        android:src="@drawable/ic_cloud_white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.2"
        app:layout_constraintVertical_bias="0.45"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" /&amp;gt;
    &amp;lt;ImageView
        android:id="@+id/iv_cloud_grey"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/ic_cloud_gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintVertical_bias="0.55"
        app:layout_constraintHorizontal_bias="0.8"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" /&amp;gt;

&amp;lt;/androidx.constraintlayout.motion.widget.MotionLayout&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 6 - The clearing weather animation
&lt;/h1&gt;

&lt;p&gt;Finally in this step, we can start the implementation of the clearing weather animation.&lt;/p&gt;

&lt;p&gt;There are 2 ways to do thet. Before Android Studio &lt;code&gt;4.0&lt;/code&gt; you could do that only using the XML file. From version 4.0 the developers can use a graphical interface, which is the &lt;code&gt;MotionScene&lt;/code&gt;. About this we have talked already. You can open this screen when you open the &lt;code&gt;activity_main.xml&lt;/code&gt; file and switch to the design view using the icons at the top right corner of Android Studio.&lt;/p&gt;

&lt;p&gt;In this tutorial we gonna do that manually. So we gonna use the design view.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FAndroidStudio_theMotionScene-1024x420.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FAndroidStudio_theMotionScene-1024x420.png" alt="The MotionScene"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The start ConstraintSet
&lt;/h2&gt;

&lt;p&gt;As you can see, the starter &lt;code&gt;ConstraintSet&lt;/code&gt; is already done, because we have inserted the above layout. Note the alpha property of the &lt;code&gt;view_background_gray&lt;/code&gt; View.&lt;/p&gt;

&lt;h2&gt;
  
  
  The start ConstraintSet
&lt;/h2&gt;

&lt;p&gt;Now select the end ConstraintSet.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FAndroidStudio_endConstraintSet.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FAndroidStudio_endConstraintSet.png" alt="The end ConstraintSet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, we gonna change the transparency of the 2 views. We can do that by setting their &lt;code&gt;alpha&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;From the &lt;code&gt;ConstraintSet&lt;/code&gt; table, what you can find below of the above screens, select the &lt;code&gt;view_background_gray&lt;/code&gt; and from the Attributes pane change the alpha from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Similarly, change the alpha of &lt;code&gt;view_background_blue&lt;/code&gt; from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, select the &lt;code&gt;iv_cloud_white&lt;/code&gt; &lt;code&gt;ImageView&lt;/code&gt;. From its attributes remove the horizontal and the vertical bias lines. You can do it easyily in the right &lt;strong&gt;Attributes pane&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Then remove it’s right to right and the left ot left alignments.&lt;/p&gt;

&lt;p&gt;Finally add the left to right alignment with value of parent. You can do in the desing mode if you click in the &lt;strong&gt;Attributes pane&lt;/strong&gt; on the plus icon.&lt;/p&gt;

&lt;p&gt;The result is that, the &lt;code&gt;ImageView&lt;/code&gt; gonna be out from the screen at the right side.&lt;/p&gt;

&lt;p&gt;The last change on the end &lt;code&gt;ConstraintSet&lt;/code&gt; is to modify the attributes of the &lt;code&gt;iv_cloud_grey&lt;/code&gt; &lt;code&gt;ImageView&lt;/code&gt; as well. So, remove also the bias lines and the left to left – right to right constraints. Thenafter add the right to left constraint with the values of &lt;code&gt;parent&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Transition
&lt;/h2&gt;

&lt;p&gt;The last thing what we have to set before the first run of the app, is to set the click event. &lt;/p&gt;

&lt;p&gt;This click event will start the transition. We can add it to the &lt;code&gt;Transition&lt;/code&gt; by clicking on it. You can open it if you click on the arrow which &lt;code&gt;connects the start and the end ConstraintSets&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FAndroidStudio_transition.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FAndroidStudio_transition.png" alt="The MotionScene"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thenafter from the Attributes pane, in the &lt;code&gt;OnClick&lt;/code&gt; part click on the &lt;code&gt;+&lt;/code&gt; icon and add the below attributes with their values.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;clickActioin&lt;/code&gt; -&amp;gt; &lt;code&gt;toggle&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;targetId&lt;/code&gt; -&amp;gt; &lt;code&gt;@id/iv_cloud_grey&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The XML code of the MotionScene
&lt;/h2&gt;

&lt;p&gt;If you are stucked, then just copy the below code into the &lt;code&gt;activity_main_scene.xml&lt;/code&gt; file, what you can find in the &lt;code&gt;res &amp;gt; xml&lt;/code&gt; folders.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;MotionScene 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto"&amp;gt;

    &amp;lt;Transition
        motion:constraintSetEnd="@+id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="1000"&amp;gt;
       &amp;lt;KeyFrameSet&amp;gt;
       &amp;lt;/KeyFrameSet&amp;gt;
        &amp;lt;OnClick motion:clickAction="toggle"
            motion:targetId="@id/iv_cloud_grey" /&amp;gt;
    &amp;lt;/Transition&amp;gt;

    &amp;lt;ConstraintSet android:id="@+id/start"/&amp;gt;

    &amp;lt;ConstraintSet android:id="@+id/end"&amp;gt;
        &amp;lt;Constraint
            android:id="@+id/view_background_gray"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0" /&amp;gt;
        &amp;lt;Constraint
            android:id="@+id/view_background_blue"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="1" /&amp;gt;
        &amp;lt;Constraint
            android:id="@+id/iv_cloud_white"
            android:elevation="1dp"
            android:layout_width="200dp"
            android:layout_height="200dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintLeft_toRightOf="parent" /&amp;gt;
        &amp;lt;Constraint
            android:id="@+id/iv_cloud_grey"
            android:layout_width="150dp"
            android:layout_height="150dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintRight_toLeftOf="parent" /&amp;gt;
    &amp;lt;/ConstraintSet&amp;gt;
&amp;lt;/MotionScene&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Run the app
&lt;/h1&gt;

&lt;p&gt;Now its time to test the app. Connect your real device or start an emulator and run the app.&lt;/p&gt;

&lt;p&gt;When it is loaded, then click on the clouds. The animation needs to run as you can see on this picture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FclearingWeather.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2020%2F08%2FclearingWeather.gif" alt="The end ConstraintSet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 6 - The Transition listener
&lt;/h1&gt;

&lt;p&gt;In the last step of this tutorial we are going to implement the Transition listener. &lt;strong&gt;Using this listener we can add some actions to the start / end transitions and of course we can specify actions during the transition as well.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In our tutorial we are going to show a &lt;code&gt;Toast&lt;/code&gt; message when the transition has been started and when it is finished.&lt;/p&gt;

&lt;p&gt;First we have to add an id to the &lt;code&gt;MotionLayout&lt;/code&gt; viewgroup in the &lt;code&gt;activity_main.xml&lt;/code&gt; file. So open it from the &lt;code&gt;res &amp;gt; layout&lt;/code&gt; folders and add the below line to the &lt;code&gt;MotionLayout&lt;/code&gt; viewgroup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;android:id=“@+id/motionLayout”
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, open up the &lt;code&gt;MainActivity::class&lt;/code&gt; file from the main source set.&lt;/p&gt;

&lt;p&gt;We can use the &lt;code&gt;setTransitionListener()&lt;/code&gt; method to add actions to the transition.&lt;/p&gt;

&lt;p&gt;So, copy and paste the below line into the &lt;code&gt;onCreate()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val motionLayout = findViewById(R.id.motionLayout)

motionLayout.setTransitionListener(object : MotionLayout.TransitionListener {
    override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {
    }
    override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int)
    {
        Toast.makeText(this@MainActivity, "onTransitionStarted", Toast.LENGTH_SHORT).show()
    }
    override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {
    }
    override fun onTransitionCompleted(p0: MotionLayout?, p1: Int)
    {
        Toast.makeText(this@MainActivity, "onTransitionCompleted", Toast.LENGTH_SHORT).show()
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First in the code we have to find the view of the &lt;code&gt;MotionLayout&lt;/code&gt;. Thenafter we use its &lt;code&gt;setTransitionListener()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;As you can see, it has 4 methods. From them we are going to use only 2, So, we have added a Toast to the &lt;code&gt;onTransitionStarted()&lt;/code&gt; and &lt;code&gt;onTransitionCompleted()&lt;/code&gt; methods.&lt;/p&gt;

&lt;h1&gt;
  
  
  Run the app
&lt;/h1&gt;

&lt;p&gt;Run again the app. When you start the transition and when it is finished, then you will have a &lt;code&gt;Toast&lt;/code&gt; message on the screen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Congratulations! 😎
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You just created a cool animation, what maybe you can use in your next app. 😊&lt;/p&gt;

&lt;h1&gt;
  
  
  Run the app
&lt;/h1&gt;

&lt;p&gt;The source code is available on GitHub, check it out and download it using the below link.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/inspire-coding/ClearingWeather" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Android WeatherApp course
&lt;/h1&gt;

&lt;p&gt;Did you like this short tutorial, then check out my &lt;a href="https://inspirecoding.app/courses/weatherapp/" rel="noopener noreferrer"&gt;Android WeatherApp course&lt;/a&gt; as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;More Android tutorials&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you would like to do more Android tutorials like this, then visit my website:&lt;br&gt;
&lt;a href="https://inspirecoding.app/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2019%2F11%2Fcropped-Header-of-the-website.png" alt="Inspire Coding"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Questions
&lt;/h1&gt;

&lt;p&gt;I hope the description was understandable and clear. But, if you have still questions, then leave me comments below! 😉&lt;/p&gt;

&lt;p&gt;Have a nice a day! 🙂&lt;/p&gt;

</description>
      <category>android</category>
      <category>motionlayout</category>
      <category>animation</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>Android widgets – Update using Kotlin Flow, Room and Dagger-Hilt</title>
      <dc:creator>Inspire Coding</dc:creator>
      <pubDate>Sat, 05 Sep 2020 13:54:53 +0000</pubDate>
      <link>https://forem.com/inspire_coding/android-widgets-update-using-kotlin-flow-room-and-dagger-hilt-e0e</link>
      <guid>https://forem.com/inspire_coding/android-widgets-update-using-kotlin-flow-room-and-dagger-hilt-e0e</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Overview&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;In this Android tutorial I'm going to show you an approach which helps to update the app's widget realy easy using Kotlin Flow, Room and Dagger-Hilt.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;First of all, we have to talked in a few words about what does Koltin Flow, Room and Dagger-Hilt mean.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Kotlin Flow&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Kotlin Flow is developed by JetBrains, the owner of the Kotlin language and it is a new stream processing API. It implements the Reactive Stream specification, and its goal is ot provide a standard for asynchronous stream processing. Kotlin Flow is build on top of Kotlin Coroutines.&lt;br&gt;
Using Flow we handle streams of values, transform the data in a complex threaded way with only few lines of code.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Room&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The library helps you create a cache of your app's data on a device that's running your app. This cache, which serves as your app's single source of truth, allows users to view a consistent copy of key information within your app, regardless of whether users have an internet connection.&lt;/em&gt;&lt;br&gt;
&lt;a href="https://developer.android.com/topic/libraries/architecture/room?gclid=Cj0KCQjw7sz6BRDYARIsAPHzrNIuwKudcihbdyQ-syFtzFXjpTGKUX8nqeheNtD8i1kzrTS__1wlgBYaAlsNEALw_wcB&amp;amp;gclsrc=aw.ds" rel="noopener noreferrer"&gt;Source: Room Persistence Library&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Dagger-Hilt&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Hilt provides a standard way to use DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically. Hilt is built on top of the popular DI library Dagger to benefit from the compile-time correctness, runtime performance, scalability, and Android Studio support that Dagger provides.&lt;/em&gt;&lt;br&gt;
Source: &lt;a href="https://developer.android.com/training/dependency-injection/hilt-android" rel="noopener noreferrer"&gt;Dependency injection with Hilt&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;The sample app&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=tkpYq8OJNHM" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Finspire-coding%2FWidgetWithFlowRoomHilt%2Ffinished_version%2Fapp%2FThumbNail.png" alt="Sample app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, after a short indtroduction, let's start coding. :)&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Step 1 - Get the starter project&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;For this tutorial we are going to use a starter project, which is a ToDo app. This app contains Room and RecyclerView to show the created ToDo items.&lt;/p&gt;

&lt;p&gt;The tutorial for the starter project is available on Inspire Coding: &lt;a href="https://inspirecoding.app/room-basics-introduction/" rel="noopener noreferrer"&gt;Room basics – Introduction&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  GitHub
&lt;/h4&gt;

&lt;p&gt;If you don't want to do the starter tutorial, then you can get the starter project from GitHub as well: &lt;a href="https://github.com/inspire-coding/RoomBasics/tree/viewmodel" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clone or just download the project, and import it in Android Studio.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Step 2 - Add the widget&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The next step is to add the "widget" package to the main source set.&lt;br&gt;
If the package is created, then create into it a new widget with the below detailes&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Width: 4 cells&lt;/li&gt;
&lt;li&gt;Height: 1 cell&lt;/li&gt;
&lt;li&gt;No Configuration Screen needed&lt;/li&gt;
&lt;li&gt;Language: Kotlin&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Step 3 - Widget's layout&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The layout gonna be very simple.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TextView for the title&lt;/li&gt;
&lt;li&gt;TextView for the due date&lt;/li&gt;
&lt;li&gt;TextView for the description&lt;/li&gt;
&lt;li&gt;ImageView for the priority
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt; RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/widget_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shape_roundedcorners_white"&amp;gt;

    &amp;lt;TextView
        android:id="@+id/tv_widget_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Task 1"
        android:textColor="@color/colorPrimary"
        android:textSize="14sp" /&amp;gt;

    &amp;lt;TextView
        android:id="@+id/tv_widget_dueDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_widget_title"
        android:layout_marginStart="8dp"
        android:text="@string/duedate"
        android:textColor="@color/darkGrey"
        android:textSize="8sp" /&amp;gt;

    &amp;lt;TextView
        android:id="@+id/tv_widget_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_widget_dueDate"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@id/iv_widget_priority"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="@string/placeholder_text"
        android:textColor="@color/darkGrey"
        android:textSize="10sp" /&amp;gt;

    &amp;lt;ImageView
        android:id="@+id/iv_widget_priority"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="8dp"
        android:src="@drawable/prio_green" /&amp;gt;

&amp;lt;/ RelativeLayout&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;The shape of the widget&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Now create a new Drawable resource file with the name: shape_roundedcorners_white.xml and paste into it the below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;shape xmlns:android="http://schemas.android.com/apk/res/android"&amp;gt;

    &amp;lt;solid
        android:color="@color/white"/&amp;gt;
    &amp;lt;corners
        android:radius="12dp"/&amp;gt;

&amp;lt;/shape&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will be the background of the widget.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Step 4 - Implement Hilt&lt;/strong&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;@HiltAndroidApp&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Add the @HiltAndroidApp annotation to the MyApp::class&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;@RoomDatabaseModule&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Then create a Hilt module into a new package called "di". This module will get the name of RoomDatabaseModule.&lt;br&gt;
And the file looks like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@InstallIn(ApplicationComponent::class)
@Module
class RoomDatabaseModule
{
    @Singleton
    @Provides
    fun providesDatabase (application: Application) = ToDoRoomDatabase.getDatabase(application)

    @Singleton
    @Provides
    fun providesCurrentWeatherDao (database: ToDoRoomDatabase) = database.toDoDao()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;@AndroidEntryPoint&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Thenafter add the @AndroidEntryPoint annotation to the AppWidget::class.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;@Inject DAO&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Finally modify the ToDoRepository::class to use a constructor injection in the header of the class for the ToDoDAO.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ToDoRepository @Inject constructor (private val toDoDao: ToDoDao)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Step 5 – Add Flow to DAO&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In this step we are going to implement the needed methods to get the todo item from Room which has the todoId of 1.&lt;br&gt;
For this we are going to add the below method to the ToDoDAO::class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Query("SELECT * FROM ToDo WHERE toDoId = 1")
fun getFirstToDoItem() : Flow&amp;lt;ToDo&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the method return Flow which wraps a ToDo type.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Step 6 – Extend the repository&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Then add the below method to the ToDoRepository::class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val getFirstToDoItem : Flow&amp;lt;ToDo&amp;gt; = toDoDao.getFirstToDoItem()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The return value is the same like the same method in the ToDoDAO::class.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Step 7 – Update the AppWidget::class&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Thenafter we will add 3 member variables to the AppWidget::class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private val job = SupervisorJob()
val coroutineScope = CoroutineScope(Dispatchers.IO + job)
@Inject lateinit var toDoRepository: ToDoRepository
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because Flow is a suspend function, we can call it only from CoroutineScope. That's why we need the Job and the CoroutineScope.&lt;br&gt;
To get the item from Room is an IO operation, so we are going to add the Dispatchers.IO to the CoroutineScope.&lt;/p&gt;

&lt;p&gt;Next, remove the onUpdate() and the onEnabled() methods. We don't need them.&lt;/p&gt;

&lt;p&gt;Thenafter add the onReceive() method to the AppWidget::class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override fun onReceive(context: Context, intent: Intent?)
{
    super.onReceive(context, intent)

    coroutineScope.launch {
        toDoRepository.getFirstToDoItem.collect { _toDo -&amp;gt;
            val appWidgetManager = AppWidgetManager.getInstance(context)
            val man = AppWidgetManager.getInstance(context)
            val ids = man.getAppWidgetIds(ComponentName(context, AppWidget::class.java))

            if (_toDo != null)
            {
                for (appWidgetId in ids)
                {
                    updateAppWidget(
                        context, appWidgetManager, appWidgetId,
                        _toDo.title, _toDo.dueDate, _toDo.description, _toDo.priority
                    )
                }
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you should have an error, because we haven't extended the updateAppWidget() method. So, replace it with the below one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;internal fun updateAppWidget(
    context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int,
    title: String?, dueDate: String?, description: String?, priority: String?)
{
    // Construct the RemoteViews object
    val views = RemoteViews(context.packageName, R.layout.app_widget)

    if (title != null) {
        views.setTextViewText(R.id.tv_widget_title, title)
    } else {
        views.setTextViewText(R.id.tv_widget_title, "")
    }
    if (dueDate != null) {
        views.setTextViewText(R.id.tv_widget_dueDate, dueDate)
    } else {
        views.setTextViewText(R.id.tv_widget_dueDate, "")
    }
    if (description != null) {
        views.setTextViewText(R.id.tv_widget_description, description)
    } else {
        views.setTextViewText(R.id.tv_widget_title, "")
    }
    if (description != null) {
        when (priority)
        {
            Prioirities.LOW.name -&amp;gt; views.setImageViewResource(R.id.iv_widget_priority, R.drawable.prio_green)
            Prioirities.MEDIUM.name -&amp;gt; views.setImageViewResource(R.id.iv_widget_priority, R.drawable.prio_orange)
            Prioirities.HIGH.name -&amp;gt; views.setImageViewResource(R.id.iv_widget_priority, R.drawable.prio_red)
        }
    }

    views.setOnClickPendingIntent(R.id.widget_layout,
        getPendingIntentActivity(context))

    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And one more error. It is there, because we haven't implemented the getPendingIntentActivity() method yet. This method will create for the views a PendingIntent to update the views if the widget's instances.&lt;br&gt;
So, paste the below method at the end of the AppWidget.kt file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private fun getPendingIntentActivity(context: Context): PendingIntent
{
    // Construct an Intent which is pointing this class.
    val intent = Intent(context, MainActivity::class.java)
    // And this time we are sending a broadcast with getBroadcast
    return PendingIntent.getActivity(context, 0, intent, 0)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, we are almost done. One more thing left before we can test the app. When we delete an instance of the AppWidget, we have to cancel its Job as well. So add the below line to the onDisabled() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;job.cancel()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Run the app&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Finally its time to run the app.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;More Android tutorials&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you would like to do more Android tutorials like this, then visit my website:&lt;br&gt;
&lt;a href="https://inspirecoding.app/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsecureservercdn.net%2F160.153.137.218%2Fyv6.cb0.myftpupload.com%2Fwp-content%2Fuploads%2F2019%2F11%2Fcropped-Header-of-the-website.png" alt="Inspire Coding"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Questions&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I hope the description was understandable and clear. But, if you have still questions, then leave me comments below! 😉&lt;/p&gt;

&lt;p&gt;Have a nice a day! 🙂&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>hilt</category>
      <category>flow</category>
    </item>
  </channel>
</rss>
