Home  >  Blog  >   Android  > 

Adding buttons & adding a list in Android

Rating: 4
  
 
2059

 

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.

If you would like to Enrich your career with an Android certified professional, then visit Mindmajix - A Global online training platform:  “Android training”  Course.  This course will help you to achieve excellence in this domain.

It is a Push-button that 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.
 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) {
MyActivity.this.finish();
}
})
.setNegativeButton ("No", new DialogInterface.OnClickListener() { public void on Click (DialogInterface dialog, int id) {
dialog.cancel();
}
});
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.

MindMajix Youtube Channel

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.

Checkout:-Android Services

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.

Frequently Asked Android Interview Questions & Answers
 

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.

Explore Android Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download Now!

Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Android TrainingJun 28 to Jul 13
Android TrainingJul 02 to Jul 17
Android TrainingJul 05 to Jul 20
Android TrainingJul 09 to Jul 24
Last updated: 27 June 2022
About Author
Ravindra Savaram

Ravindra Savaram is a Content Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.

Recommended Courses

1 /15