Open In App

How to Create Balloon Toast Message in Android?

Last Updated : 18 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to creating a balloon Toast. This Library is one of the popular features that is commonly used in most Android Apps. We can get to see this feature in most of the shopping and messaging apps. With the help of this feature, you can get a hint about what to do next in any app. Here in the output, we can see what we are going to do in this article.

Step by Step Implementation

Step 1: Create a new Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language. 

Step 2: Add dependency and JitPack Repository

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.   

implementation ‘com.github.BeppiMenozzi:BalloonPopup:0.2.8’

Add the JitPack repository to your build file. Add it to your root build.gradle at the end of repositories inside the allprojects{ } section.

allprojects {

 repositories {

   …

   maven { url “https://jitpack.io” }

     }

}

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/samplegeeks"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="GeeksForGeeks"
        android:textColor="#E91E63"
        android:textSize="32sp"
        android:textStyle="bold" />
      
</LinearLayout>


Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import it.beppi.balloonpopuplibrary.BalloonPopup
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val bp = BalloonPopup.Builder(applicationContext, findViewById(R.id.samplegeeks))
                .text("Showing Balloon Toast")    // set the text displayed (String or resource)
                .timeToLive(5000)                 // Milliseconds before closing the popup. 0 = persistent
                .animation(BalloonPopup.BalloonAnimation.fade_and_scale)    // animation style used. Available:
                // pop, scale, fade, fade75
                // and all the possible combinations.
                // When fade75 is used (up to alpha .75) the view is slightly transparent
                .shape(BalloonPopup.BalloonShape.rounded_square)      // Circle (oval) or rounded square
                .bgColor(Color.CYAN)        // unused yet
                .fgColor(Color.RED)         // text color
                .textSize(20)               // text size
                .offsetX(10)                // offsets to move the position accordingly
                .offsetY(15)
                .positionOffset(510, 815)
                .drawable(R.drawable.bg_circle) // custom background drawable
                .show();
    }
}


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads