Android Flow Using Kotlin With Example

In this article, we will learn about android Flow using Kotlin. We will go through various example that demonstrates how to use different attributes of Flow. For example,

In this article, we will get answer to questions like –

  • What is Flow?
  • Why should we consider Flow while designing ui for any app?
  • What are possibilities using Flow while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Kotlin Android Flow Using Kotlin With Example

Getting Started

We can define android Flow widget as below –

Android Flow is virtual layout that allows positioning of referenced widgets horizontally or vertically. Using constraint_referenced_ids attribute, we virtually group views and apply common constraints to all grouped views.

Now, how do we use Flow in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as Flow. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android Flow Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using Flow in Kotlin

Follow steps below to use Flow in newly created project –

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">Flow</string>
        <string name="textview_1">[TextView 1]</string>
        <string name="textview_2">[TextView 2]</string>
        <string name="textview_3">[TextView 3]</string>
        <string name="textview_4">[TextView 4]</string>
        <string name="textview_5">[TextView 5]</string>
    </resources>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textview_1"
            tools:ignore="MissingConstraints" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textview_2"
            tools:ignore="MissingConstraints" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textview_3"
            tools:ignore="MissingConstraints" />
    
        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textview_4"
            tools:ignore="MissingConstraints" />
    
        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textview_5"
            tools:ignore="MissingConstraints" />
    
        <androidx.constraintlayout.helper.widget.Flow
            android:id="@+id/flow1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="20dp"
            app:constraint_referenced_ids="textView1,textView2,textView3,textView4,textView5"
            app:flow_horizontalBias="20"
            app:flow_horizontalGap="20dp"
            app:flow_horizontalStyle="packed"
            app:flow_verticalBias="10"
            app:flow_wrapMode="aligned"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    In this xml file, we have used some TextView and Flow (that controls some ui conditions on referenced views).
    Here,

    1. app:constraint_referenced_ids=””: This attribute is used to reference views using their id. if there is more than 1 view to reference, write them as comma separated. Here, we have referenced 5 TextViews by their ids.
    2. app:flow_horizontalBias=””: Sets horizontal bias applied to the chain.
    3. app:flow_horizontalGap=””: This attribute sets horizontal gap between elements.
    4. app:flow_horizontalStyle=””: This attribute sets horizontal chain style.
    5. app:flow_verticalBias=””: Sets vertical bias applied to the chain.
    6. app:flow_wrapMode=””: Sets wrap mode for the layout.
  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.flow
    
    import android.os.Bundle
    import androidx.appcompat.app.AppCompatActivity
    import com.tutorialwing.flow.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		setContentView(binding.root)
    
                    // We can access Flow using binding.FLOW_ID and perform any action.
    	}
    }
    

Now, run the application. We will get output as below –
Tutorialwing Kotlin Android Flow Using Kotlin With Example

Different Attributes of Flow in XML

Now, we will see how to use different attributes of Android Flow using Kotlin to customise it –

Set Id of Flow

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of Flow using android:id attribute like below –

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow_ID"
        />

Here, we have set id of Flow as flow_ID using android:id=”” attribute. So, if we need to reference this Flow, we need to use this id – flow_ID.

Set Width of Flow

We use android:layout_width=”” attribute to set width of Flow.
We can do it as below –

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).

Set Height of Flow

We use android:layout_height=”” attribute to set height of Flow.
We can do it as below –

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.

Set Padding of Flow

We use android:padding=”” attribute to set padding of Flow.
We can do it as below –

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in Flow using android:padding=”” attribute.

Set Margin of Flow

We use android:layout_margin=”” attribute to set margin of Flow.
We can do it as below –

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in Flow using android:layout_margin=”” attribute.

Set Background of Flow

We use android:background=”” attribute to set background of Flow.
We can do it as below –

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in Flow using android:background=”” attribute.

Set Visibility of Flow

We use android:visibility=”” attribute to set visibility of Flow.
We can do it as below –

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of Flow using android:visibility=”” attribute. Visibility can be of three types – gone, visible and invisible

We have seen different attributes of Flow and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is Flow, how can we use android Flow using Kotlin ? etc. We also went through different attributes of android Flow.

Leave a Reply