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();
}
}
- Increase Android Emulator Timeout time
- Android : Remove ListView Separator/divider programmatically or using xml property
- Error : Invalid key hash.The key hash does not match any stored key hashes
- How to Change Android Toast Position?
- Android Alert Dialog with Checkboxes example
- Android : No Launcher activity found! Error
- Android Development: Spinners with Example
- Failed to sync Gradle project Error:failed to find target android-23
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Disable Fading Edges Scroll Effect Android Views
- How to create Toast messages in Android?
- Channel 50 SMSes received every few minutes Android Phones
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Create Custom Android AlertDialog
- How To Disable Landscape Mode in Android Application
- Android Development - How to switch between two Activities
- incorrect line ending: found carriage return (\r) without corresponding newline (\n)
- Generate Facebook Android SDK keyhash using java code
- Android Error Generating Final Archive - Debug Certificate Expired
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Android RatingBar Example
- 11 Weeks of Android Online Sessions-15-Jun-to-28-Aug-2020
- Download interrupted: Unknown Host dl-ssl.google.com Error Android SDK Manager
- fill_parent vs match_parent vs wrap_content
- Android : Connection with adb was interrupted 0 attempts have been made to reconnect
- Division between two numbers - C-Program
- How to install Microsoft Teams on Mac using Terminal (Silently) - Teams
- How to lock Notepad++ tabs? - NotepadPlusPlus
- Add or remove users from sudo group - Ubuntu - Ubuntu
- Shortcut: How to delete a complete line in Nano Editor - Linux
- Java JDBC Connection with Database using SSL (https) URL - Java
- Json Serialization and Deserialization using Java Jackson - Java
- [Java Threads] Should we extend Thread Class or implement Runnable interface - Java