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
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" )
.timeToLive( 5000 )
.animation(BalloonPopup.BalloonAnimation.fade_and_scale)
.shape(BalloonPopup.BalloonShape.rounded_square)
.bgColor(Color.CYAN)
.fgColor(Color.RED)
.textSize( 20 )
.offsetX( 10 )
.offsetY( 15 )
.positionOffset( 510 , 815 )
.drawable(R.drawable.bg_circle)
.show();
}
}
|
Output: