Dialog boxes allow us to ask questions to users and get their response via buttons where each button is a different answer. In this tutorial you will learn to make a dialog box that could be used in a terms of service agreement.
The App’s Interface:
The app’s interface will be a button that when pushed will pop up a dialog box.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/buttonDialogAlert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Activate App" /> </LinearLayout>
This is how the app is going to look.

The button used to trigger the dialog box
Buttons Listener:
The following snippets pertain to the class DialogsActivity.
We need to know when the user clicks the button so we are going to make a listener. Inside the onClick function of the listener we will build and show our dialog. Our dialog will have a title, message and two buttons, the user will not be able to cancel the interface in anyway (clicking outside, or using the back button).
View.OnClickListener listenerDialogAlert = new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Builder builder = new AlertDialog.Builder(DialogsActivity.this); AlertDialog dialog = builder.create(); dialog.setTitle("Do you accept our terms of service?"); dialog.setMessage("You cannout use this app unless you accept."); dialog.setButton("Yes",listenerAccept); dialog.setButton2("No", listenerDoesNotAccept); dialog.setCancelable(false); dialog.show(); } };
This is how our dialog looks.

the pop up dialog
The Two buttons inside the dialog also need their own listener each, we named this listeners in the setButton and seButton2 functions. Notice that these onclick listners are not of the the class View but of the class DialogInterface.
DialogInterface.OnClickListener listenerAccept = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(DialogsActivity.this, "Great! Welcome.", Toast.LENGTH_SHORT).show(); } }; DialogInterface.OnClickListener listenerDoesNotAccept = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(DialogsActivity.this, "You can't use this app then.", Toast.LENGTH_SHORT).show(); } };
What will happen when the user pushes "Yes":

Toast alert after pushing the yes button
What will happen when the user pushes "No":

Toast alert after pushing the no button
Full Activity Code:
This is the full activity code.
package com.yoursite.dialogs; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class DialogsActivity extends Activity { private Button buttonDialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // get button view buttonDialog = (Button)findViewById(R.id.buttonDialogAlert); // set button listener buttonDialog.setOnClickListener(listenerDialogAlert); } View.OnClickListener listenerDialogAlert = new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Builder builder = new AlertDialog.Builder(DialogsActivity.this); AlertDialog dialog = builder.create(); dialog.setTitle("Do you accept our terms of service?"); dialog.setMessage("You cannout use this app unless you accept."); dialog.setButton("Yes",listenerAccept); dialog.setButton2("No", listenerDoesNotAccept); dialog.setCancelable(false); dialog.show(); } }; DialogInterface.OnClickListener listenerAccept = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(DialogsActivity.this, "Great! Welcome.", Toast.LENGTH_SHORT).show(); } }; DialogInterface.OnClickListener listenerDoesNotAccept = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(DialogsActivity.this, "You can't use this app then.", Toast.LENGTH_SHORT).show(); } }; }
Related posts: