How to create popup message using Alerter Library in android

In this article, we learn about how to create a popup message with the help of Alerter Library. It is better to use Alerter than using Toast or Snackbar in cases if some alert messages are to be displayed to the user. We can add various onClickListners to our alerter message which makes it better and it also has nice appealing UI.


Approach:

  1. Add the support Library in build.gradle file and add dependency in the dependencies section. This library facilitates easy integration of Alert View in the app. The alert view is customizable and it is displayed over the ongoing activity in the app. This library is also compatible with AndroidX.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    dependencies {
        implementation 'com.tapadoo.android:alerter:2.0.4'
    }

    chevron_right

    
    

  2. Now add the following code in the activity_main.xml file. This codes add a button in the MainActivity and if the button is clicked then showAlerter function is invoked.

    activity_main.xml

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
      
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
      
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="showAlerter"
            android:text="Show Alerter"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    chevron_right

    
    

  3. Now add the following code in the MainActivity.java file. It defines the showAlerter function. This function creates the Alerter. Various methods are called to initialize different properties of the Alerter. setTitle sets the title, setText sets the text shown below the title, setIcon sets the icon, etc. Various onClickListeners are also attached so that you can do something in response of the users action.

    MainActivity.java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package org.geeksforgeeks.gfgAlerter;
      
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import com.tapadoo.alerter.Alerter;
    import com.tapadoo.alerter.OnHideAlertListener;
    import com.tapadoo.alerter.OnShowAlertListener;
      
    public class MainActivity extends AppCompatActivity {
      
        Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            button = findViewById(R.id.button);
        }
      
        public void showAlerter(View v)
        {
            Alerter.create(this)
                .setTitle("Alert Title")
                .setText("Alert Text")
                .setIcon(
                    R.drawable.ic_android_black_24dp)
                .setBackgroundColorRes(
                    R.color.colorAccent)
                .setDuration(3000)
                .setOnClickListener(
                    new View.OnClickListener() {
      
                        @Override
                        public void onClick(View v)
                        {
                            // do something when
                            // Alerter message was clicked
                        }
                    })
      
                .setOnShowListener(
                    new OnShowAlertListener() {
      
                        @Override
                        public void onShow()
                        {
                            // do something when
                            // Alerter message shows
                        }
                    })
      
                .setOnHideListener(
                    new OnHideAlertListener() {
      
                        @Override
                        public void onHide()
                        {
                            // do something when
                            // Alerter message hides
                        }
                    })
                .show();
        }
    }

    chevron_right

    
    

Output:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai

Article Tags :
Practice Tags :


4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.