Adding buttons & adding a list in Android

One of the most basic elements in any Graphical User Interface is the button. So in this post, we’ll talk about creating a button and adding it to our application.

Buttons in Android :

A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it.

It is a Push-button which can be pressed, or clicked, by the user to perform an action.

Every Button is styled using the system’s default button background, which is often different from one device to another and from one version of the platform to another. If you’re not satisfied with the default button style and want to customize it to match the design of your application, then you can replace the button’s background image with a state list drawable. A state list drawable is a drawable resource defined in XML that changes its image based on the current state of the button. Once you’ve defined a state list drawable in XML, you can apply it to your Button with the android:background attributes.
To CREATE AN ALERTDIALOG with side-by-side buttons like the one shown in the screenshot above, use the set…Button() methods:

AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage ("Are you sure you want to exit?")
.setCancelable (false)
.setPositiveButton ("Yes", new DialogInterface.OnClickListener() { public void onClick (DialogInterface dialog, int id) {
.setNegativeButton ("No", new DialogInterface.OnClickListener() { public void on Click (DialogInterface dialog, int id) {
AlertDialog alert = builder.create();

First, add a message for the dialog with setMessage (CharSequence). Then, begin method-chaining and set the dialog to be not cancelable (so the user cannot close the dialog with the back button) with setCancelable(boolean). For each button, use one of the set…Button() methods, such as setPositiveButton(), that accepts the name of the button and a DialogInterface. OnClickListener that defines the action to take when the user selects the button.

Note: You can only add one of each button type to the AlertDialog. That is, you cannot have more than one “positive” buttons. This limits the number of possible buttons to three : positive, neutral and negative. These names are technically irrelevant to the actual functionality of your buttons, but should help you keep track of which one does what.

Adding a list

The display of elements in a list is a very common pattern in mobile applications. LISTVIEW is a view group that displays a list of scrollable items. When a user sees a list of items he can seamlessly scroll through them. Typically the user interacts with the list via the toolbar, for example, via a button which refreshes the list. Individual list items are being selected. This selection can update the toolbar or can trigger a detailed screen for the selection.

The list items are automatically inserted into the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that’s placed into the list.

To create an AlertDialog with a list of selectable items like the one shown to the right, use the setItems() method:

final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder = new AlertDialog.Builder(this); builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
AlertDialog alert = builder.create();

First, add a title to the dialog with setTitle(CharSequence). Then, add a list of selectable items with setItems(), which accepts the array of items to display and a Dialog Interface. OnClickListener that defines the action to take when the user selects an item.


Get Updates on Tech posts, Interview & Certification questions and training schedules