In Android Programming we have AlertDialogs the way we have HTML. We can have OK and CANCEL buttons with and Title and text messages/info.

1. Create an object of AlertDialog.Builder from android.app.AlertDialog package.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
2. Set the Title for the Alert Dialog using setTitle() method.
alertDialog.setTitle("Android Alert Message");
3. Set the Message for the Alert Dialog using setTitle() method.
alertDialog.setMessage("Put your description text here!");
4. Now if you want to add some other views you can add them to a custom Layout without the Alert Dialog. So lets create a LinearLayout and set its orientation as Vertical.
LinearLayout diagLayout = new LinearLayout(this);
diagLayout.setOrientation(LinearLayout.VERTICAL);
5. Now lets create a TextView and set its text using setText() and add attributes like padding, gravity and setTextSIze using setPadding(), setGravity() and setTextView() respectively.
TextView text = new TextView(this); text.setText("Another text view");
text.setPadding(10, 10, 10, 10);
text.setGravity(Gravity.CENTER);
text.setTextSize(20);
6. Set the Title for the Alert Dialog using setTitle() method.
diagLayout.addView(text);
alertDialog.setView(diagLayout);
7. Add two buttons Positive and Negative using setNegativeButton() and SetPositiveButton(). Also, add listers OnClickListener to both the buttons and display a Toast message!.
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(), "OK Button pressed!",
Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
alertDialog.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(),
"CANCEL button pressed!!", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
8. Finally, we display the Alert Dialog message using show() method.
alertDialog.show();
Complete Code :package com.example.drawing;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class AlertDialogExample extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Android Alert Message");
alertDialog.setMessage("Put your description text here!");
LinearLayout diagLayout = new LinearLayout(this);
diagLayout.setOrientation(LinearLayout.VERTICAL);
final TextView text = new TextView(this);
text.setText("Another text view");
text.setPadding(10, 10, 10, 10);
text.setGravity(Gravity.CENTER);
text.setTextSize(20);
diagLayout.addView(text);
alertDialog.setView(diagLayout);
alertDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(),
"OK Button pressed!", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
alertDialog.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(),
"CANCEL button pressed!!", Toast.LENGTH_LONG)
.show();
dialog.dismiss();
}
});
alertDialog.show();
}
}
- Android Error Unexpected cast to Button: layout tag was FrameLayout
- ADT quit unexpectedly error on Mac OSX Android Eclipse SDK
- Parsing Data for android-21 failed unsupported major.minor version 51.0
- Android Studio Ctrl Shift o auto import not working
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
- Android : How to make TextView Scrollable
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Integrating Android Facebook SDK 3.17.2 Tutorial
- Android R Cannot Be Resolved To A Variable
- Android : Exception raised during rendering: action_bar API 22
- How to take screenshot on Android
- Read Text file from SD Card : Android Programming
- How to make Android EditText not editable
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later.
- The declared package does not match the expected package Eclipse
- Can't Run SDK Manager find_java.bat issue
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- Android Emulator Soft Back button action using Computer keyboard
- Multiline EditText in Android Example
- Use 5G Network on Android Emulator
- Make Android TextView Clickable like Buttons
- How to empty trash in Android Device
- Android : Execute some code after back button is pressed
- Disable Fading Edges Scroll Effect Android Views
- How To Disable Landscape Mode in Android Application
- [fix] zsh: illegal hardware instruction python TensorFlow M1/M2 Mac - zsh
- How to connect Relational and NoSQL databases with Spring Boot - HowTos
- Error code - 7: There's a more permanent way to sign in to Microsoft Teams - Teams
- How to Sync Microsoft Teams Calendar with Mac Calendar - Microsoft
- Eclipse Error : The Eclipse executable launcher was unable to locate its companion shared library. - Eclipse
- How to ignore files in git using .gitignore file - Git
- Notepad++ Replace space with Dot - NotepadPlusPlus
- java: unclosed string literal [Error] - Java