Android Floating Action Button Using Kotlin With Example

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

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

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

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

Output

Tutorialwing Kotlin Floating Action Button Using Kotlin Tutorial With Example

Floating Action Button Using Kotlin Output

Getting Started

We can define android Floating Action Button widget as below –

Floating action button is a circular button that floats above the UI and triggers primary action in android app’s user interface. It also have special motion behaviours related to morphing, launching and the transferring anchor point.

Now, how do we use Floating Action Button 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 FloatingActionButton. 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 Floating Action Button 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 Floating Action Button in Kotlin

Follow steps below to use Floating Action Button in newly created project –

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">FloatingActionButton</string>
        <string name="message">Click on Floating Action Button</string>
        <string name="email">Email</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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/message"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="16dp"
            android:contentDescription="@string/email"
            android:src="@android:drawable/ic_dialog_email"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.floatingactionbutton
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.Toast
    import com.tutorialwing.floatingactionbutton.databinding.ActivityMainBinding
    
    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)
    
    		setupFloatingActionButton()
    	}
    
    	private fun setupFloatingActionButton() {
    		binding.fab.setOnClickListener {
    			// We are showing only toast message. However, you can do anything you need.
    			Toast.makeText(
    				applicationContext,
    				"You clicked Floating Action Button",
    				Toast.LENGTH_SHORT
    			).show()
    		}
    	}
    }
    

    In this MainActivity.kt file, we have accessed floating action button and added a click listener to show a toast message whenever it is clicked. However, you can perform any action here.

Now, run the application. We will get output as below –

Tutorialwing Kotlin Floating Action Button Using Kotlin Tutorial With Example

Floating Action Button Using Kotlin Output

Different Attributes of Floating Action Button in XML

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

Set Id of Floating Action Button

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 Floating Action Button using android:id attribute like below –

    <FloatingActionButton
        android:id="@+id/floatingActionButton_ID"
        />

Here, we have set id of Floating Action Button as floatingActionButton_ID using android:id=”” attribute. So, if we need to reference this Floating Action Button, we need to use this id – floatingActionButton_ID.
Learn to Set ID of Floating Action Button Dynamically

Set Width of Floating Action Button

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

    <FloatingActionButton
        android:id="@+id/floatingActionButton_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of Floating Action Button Dynamically

Set Height of Floating Action Button

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

    <FloatingActionButton
        android:id="@+id/floatingActionButton_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of Floating Action Button Dynamically

Set Padding of Floating Action Button

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

    <FloatingActionButton
        android:id="@+id/floatingActionButton_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in Floating Action Button using android:padding=”” attribute.
Learn to Set Padding of Floating Action Button Dynamically

Set Margin of Floating Action Button

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

    <FloatingActionButton
        android:id="@+id/floatingActionButton_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in Floating Action Button using android:layout_margin=”” attribute.
Learn to Set Margin of Floating Action Button Dynamically

Set Background of Floating Action Button

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

    <FloatingActionButton
        android:id="@+id/floatingActionButton_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in Floating Action Button using android:background=”” attribute.
Learn to Set Background of Floating Action Button Dynamically

Set Visibility of Floating Action Button

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

    <FloatingActionButton
        android:id="@+id/floatingActionButton_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of Floating Action Button using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of Floating Action Button Dynamically

Till now, we have see how to use android Floating Action Button using Kotlin. We have also gone through different attributes of Floating Action Button to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android Floating Action Button Widget

Below are the various attributes that are used to customise android Floating Action Button Widget. However, you can check the complete list of attributes of Floating Action Button in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –

Some of the popular attributes of android floating action button widget are –

Sr. XML Attributes Description
1. app:fabCustomSize It specifies the size of the button in pixels. If it is set to NO_CUSTOM_SIZE, custom size will not be used and size will be calculated based on the fabSize attribute.
2. app:fabSize It specifies the size of the button. There are three options available – mini, auto and normal. auto will choose size based on the screen size.
3. app:elevation Defines the elevation of the button.
4. app:rippleColor Sets the color of the ripple effect
5. app:useCompatPadding Sets inner padding of the button. It works on platform lollipop and after.

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

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

Leave a Reply