Methods | Description |
---|---|
setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) | This method sets a listener and it will be invoked when the negative button of the dialog is pressed. |
setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener) | This method sets a listener and it will be invoked when the neutral button of the dialog is pressed. |
setPositiveButton(CharSequence text, Dialognterface.OnClickListener listener) | This method sets a listener and it will be invoked when the positive button of the dialog is pressed. |
File Name: activity_main.xml
<LinearLayoutxmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical">
<Button
android:id="@+id/threeButtonDialog"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_marginTop = "50dp"
android:text = "ThreeButtonDialog"
android:textSize = "30dp"/>
</LinearLayout>
File Name: MainActivity.java
public class MainActivity extends Activity implements OnClickListener
{
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
button1 = (Button)findViewById(R.id.threeButtonDialog);
button1.setOnClickListener (this);
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
showThreeButtonDialog();
}
public void showThreeButtonDialog()
{
AlertDialog.Builder dialog = newAlertDialog.Builder(this);
dialog.setTitle("Save Confirm");
dialog.setMessage("Are You Sure to save");
dialog.setPositiveButton("OK",newDialogInterface.OnClickListener()
{
@Override
publicvoidonClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
dialog.setNegativeButton("NO", newDialogInterface.OnClickListener()
{
@Override
publicvoidonClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
}
});
dialog.setNeutralButton("Cancel", newDialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "You clicked on Cancel button", Toast.LENGTH_SHORT).show();
}
});
AlertDialog ad = dialog.create();
ad.show();
}
}