An Alert Dialog is a window that appears on the screen to give notifications to the user, make some decisions for the user, or enter some information from the user. An Alert Dialog is generally not full screen, but in this article, we’ll be implementing something similar.
Building an Alert Dialog:
- Title: This area gives title to the alert dialog
- Description: This area gives the main message of the alert dialog
- Action button: This area deals with the decision of the user after reading the description
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with activity_main.xml
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" ?> < androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" android:background = "#00FF00" > < Button android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Open Gift" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toTopOf = "parent" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
Step 3: Working with the MainActivity File
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.
Java
import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.WindowManager; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // for changing the background color of title bar ActionBar aBar = getSupportActionBar(); ColorDrawable cd = new ColorDrawable(Color.parseColor( "#FF00FF00" )); if (aBar != null ) { aBar.setBackgroundDrawable(cd); } // using onClickListener on button findViewById(R.id.button).setOnClickListener(v -> { // builds alert dialog AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity. this ); // description of alert dialog builder.setMessage( "Welcome to GeeksForGeeks!" ); // Title of alert dialog builder.setTitle( "Gift !" ); builder.setCancelable( false ); // Action button of alert dialog for cancel dialog and come to homepage builder.setPositiveButton( "Great, see again!" , (dialog, which) -> dialog.cancel()); // Action button of alert dialog for exit app builder.setNegativeButton( "Exit" , (dialog, which) -> finish()); AlertDialog alertDialog = builder.create(); // show alert dialog alertDialog.show(); DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); layoutParams.copyFrom(alertDialog.getWindow().getAttributes()); // setting width to 90% of display layoutParams.width = ( int ) (displayMetrics.widthPixels * 0 .9f); // setting height to 90% of display layoutParams.height = ( int ) (displayMetrics.heightPixels * 0 .9f); alertDialog.getWindow().setAttributes(layoutParams); }); } } |
Kotlin
import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.util.DisplayMetrics import android.view.View import android.view.WindowManager import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // for changing the background color of title bar val aBar = supportActionBar val cd = ColorDrawable(Color.parseColor( "#FF00FF00" )) aBar?.setBackgroundDrawable(cd) // using onClickListener on button findViewById<View>(R.id.button).setOnClickListener { // builds alert dialog val builder = AlertDialog.Builder( this ) // description of alert dialog builder.setMessage( "Welcome to GeeksForGeeks!" ) // Title of alert dialog builder.setTitle( "Gift !" ) builder.setCancelable( false ) // Action button of alert dialog for cancel dialog and come to homepage builder.setPositiveButton( "Great, see again!" ) { dialog, which -> dialog.cancel() } // Action button of alert dialog for exit app builder.setNegativeButton( "Exit" ) { dialog, which -> finish() } val alertDialog = builder.create() // show alert dialog alertDialog.show() val displayMetrics = DisplayMetrics() windowManager.defaultDisplay.getMetrics(displayMetrics) val layoutParams = WindowManager.LayoutParams() layoutParams.copyFrom(alertDialog.window?.attributes) // setting width to 90% of display layoutParams.width = (displayMetrics.widthPixels * 0 .9f).toInt() // setting height to 90% of display layoutParams.height = (displayMetrics.heightPixels * 0 .9f).toInt() alertDialog.window?.attributes = layoutParams } } } |