Friday, January 4, 2013

Android beginner tutorial: Part 41 - AlertDialog single choice list

In this tutorial we'll learn some tips about creating AlertWindows, as well as displaying lists in them.

In the previous tutorial, all the code related to the AlertDialog instance is in the onClick() function. We can optimize our code and make it more reusable by building the AlertDialog outside of onClick(), just in the onCreate() function. We'll need to declare the variable in the class first:

private AlertDialog myDialog;

Then build it in the onCreate() function and just show() it in onClick() function:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button)findViewById(R.id.testButton);

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Exit application");
builder.setMessage("Do you want to exit?");
builder.setIcon(R.drawable.snowflake);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
}
});

builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

builder.setCancelable(false);
myDialog = builder.create();

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
myDialog.show();
}
});
}

As you can see, I also set an icon for the dialog using setIcon() method.

The application looks like this:



As I said earlier, it is possible to display lists in AlertDialogs. They replace the text message, however. There are 3 possible lists to be displayed - a single choice list of buttons, a single choice list with radio buttons and a multiple choice list with checkboxes.

Today we'll take a look at the single choice list. It is basically a column of buttons, that the user can use to select one of the provided items.

Let's pretend we're making an RPG. Declare an array that has the names of available classes:

private String[] items = {"Warrior","Archer","Wizard"};

We can now use the setItems() method of the builder to apply the array and set a click listener. The click listener is a DialogInterface.OnClickListener() function, which has an onClick() method. We can create a Toast notification to display the class that the user selected using the "which" parameter of the onClick() method, that represents the index of the selected item in the array.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button)findViewById(R.id.testButton);

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose your class");
builder.setIcon(R.drawable.snowflake);
builder.setItems(items, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = Toast.makeText(getApplicationContext(), "Selected: "+items[which], Toast.LENGTH_SHORT);
toast.show();
}
});


builder.setCancelable(false);
myDialog = builder.create();

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
myDialog.show();
}
});
}

The results:



Full class code:

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity{

private AlertDialog myDialog;
private String[] items = {"Warrior","Archer","Wizard"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button)findViewById(R.id.testButton);

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose your class");
builder.setIcon(R.drawable.snowflake);
builder.setItems(items, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = Toast.makeText(getApplicationContext(), "Selected: "+items[which], Toast.LENGTH_SHORT);
toast.show();
}
});


builder.setCancelable(false);
myDialog = builder.create();

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
myDialog.show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

That is all for today.

Thanks for reading!

No comments:

Post a Comment