An AlertDialog is a subclass of a Dialog that can display one, two or three buttons.
Normally these dialogs don’t require any layout.
In this example we will look at a simple alert dialog with title and message.
Let’s go.
1. Create Project
In your android studio create an empty activity. If you are not sure how to do that check this tutorial.
In our generated project we will have one class: MainActivity and one layout activity_main.xml.
2. activity_main.xml
This is our layout. We add a button that when clicked will show us the alertdialog. We also have a TextView to act as our header label.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android_layout_width="match_parent" android_layout_height="match_parent" tools_context="info.camposha.mysimplealertdialog.MainActivity"> <TextView android_id="@+id/headerLabel" android_layout_width="wrap_content" android_layout_height="wrap_content" android_layout_alignParentTop="true" android_layout_centerHorizontal="true" android_fontFamily="casual" android_text="Android Simple AlertDialog" android_textAllCaps="true" android_textSize="24sp" android_textStyle="bold" /> <Button android_id="@+id/showAlertID" android_layout_width="wrap_content" android_layout_height="wrap_content" android_text="Show Alert Dialog" android_layout_centerHorizontal="true" android_layout_centerVertical="true" /> </RelativeLayout>
3. MainActivity.java
We have one class MainActivity.java.
package info.camposha.mysimplealertdialog; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.View; import android.widget.Button; /* Android AlertDialog Example */ public class MainActivity extends Activity { /* ONCREATE METHOD */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //REFERENCE BUTTON AND SHOW IT. Button showBtn=findViewById(R.id.showAlertID); showBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showAlert(); } }); } /* CREATE AN ALERT DIALOG AND SHOW IT */ private void showAlert() { //INSTANTIATE ALERTDIALOG BUILDER AlertDialog.Builder myBuilder=new AlertDialog.Builder(this); //SET PROPERTIES USING METHOD CHAINING myBuilder.setTitle(R.string.my_title).setMessage(R.string.my_message); //CREATE DIALOG AlertDialog myDialog=myBuilder.create(); //SHOW DIALOG myDialog.show(); } }
NOTES:
- First we specify the package name for our application.
- Then add our imports using the import keyword.
- Then create a class called MainActivity that inherits from
android.app.Activity
. - Then we create a method called
showAlert()
that will show our alert dialog.- In this method first we instantiate the AlertDialog.Builder class, passing it the context via the constructor.
- Then set the title of our dialog via the
setTitle()
method.Then set the message using thesetMessage()
method. - Then create our AlertDialog using the
create()
method defined in theAlertDialog.Builder
class. - Then show our Dialog via the
show()
method.
- Then we override the
onCreate()
method of our Activity, passing in a Bundle object. - We invoke the
setContentView()
to inflate ouractivity_main.xml
layout. - We reference our button, listen to its onclick event and invoke the
showAlert()
method.