0% found this document useful (0 votes)
51 views

Alert Dialog Box in Android

Uploaded by

The RockyFF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Alert Dialog Box in Android

Uploaded by

The RockyFF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Alert Dialogbox

Android Programming
• A dialog is a small window that prompts the user to make a decision or enter
additional information. A dialog doesn't fill the screen and is normally used for
modal events that require users to take an action before they can proceed.
• The Dialog class is the base class for dialogs, but don't instantiate Dialog
directly. Instead, use one of the following subclasses:

• AlertDialog
• A dialog that can show a title, up to three buttons, a list of selectable items, or
a custom layout.
• DatePickerDialog or TimePickerDialog
• A dialog with a predefined UI that lets the user select a date or time.
Android AlertDialog
• Android AlertDialog can be used to display the dialog message with
OK and Cancel buttons. It can be used to interrupt and ask the user
about his/her choice to continue or discontinue.
• Android AlertDialog is composed of three regions: title, content area
and action buttons.
• Android AlertDialog is the subclass of Dialog class.
• The AlertDialog class lets you build a variety of dialog designs and is often the
only dialog class you need. As shown in the following figure, there are three
regions of an alert dialog:

• Title: this is optional and only used when the content area is occupied by a
detailed message, list, or custom layout. If you need to state a simple
message or question, you don't need a title.
• Content area: this can display a message, list, or other custom layout.
• Action buttons: there can be up to three action buttons in a dialog.
• The AlertDialog.Builder class provides APIs that let you create an AlertDialog
with these kinds of content, including a custom layout.
• There are three action buttons you can add:

• Positive: use this to accept and continue with the action (the "OK" action).
• Negative: use this to cancel the action.
• Neutral: use this when the user might not want to proceed with the action
but doesn't necessarily want to cancel. It appears between the positive
and negative buttons. For example, the action might be "Remind me later."
• You can add only one of each button type to an AlertDialog. For example,
you can't have more than one "positive" button.
Add a list
• There are three kinds of lists available with the AlertDialog APIs:

• A traditional single-choice list.


• A persistent single-choice list (radio buttons).
• A persistent multiple-choice list (checkboxes).
Single-choice List
persistent multiple-choice
A single-choice alert dialog
• AlertDialog.Builder builder;
• builder = new AlertDialog.Builder(this);
public class MainActivity extends AppCompatActivity {

TextView tv1;
AlertDialog.Builder alert1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alert1 =new AlertDialog.Builder(this);
alert1.setTitle("Length is more than 10");
alert1.setMessage("Enter less than 10 characters");
alert1.setPositiveButton("ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id) {
finish();
Toast.makeText(getApplicationContext(),"you choose yes action for alertbox",
Toast.LENGTH_SHORT).show();
}
});

alert1.setNegativeButton("cancel",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
finish();
Toast.makeText(getApplicationContext(),"you choose No action for alertbox",
Toast.LENGTH_SHORT).show();
}
});
tv1=findViewById(R.id.tv1);
String str = tv1.getText().toString();
if(str.length() >10)
{
alert1.show();
//alert1.setOnItemSelectedListene
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_marginLeft="200px"
android:layout_marginTop="100px"
android:textSize="15pt"

/>

You might also like