In the previous article (Basic Controls II) of Android developer tutorial we saw different types of image and text controls. In the next article we will do a review of the various controls available for android to select information.
In this post we will focus on the next user interface controls:
- CheckBox
- RadioButton
Control CheckBox
A checkbox control is often used to check or uncheck options in an application and Android is represented by the class of the same name, CheckBox. The way to define it in our interface and methods available to manipulate from our code are similar to those already mentioned for the ToggleButton control.
Thus, to define a control of this type in our layout we can use the following code, which defines a checkbox with the text “Mark me”:
< ImageView android:id= "@+id/ChkExample" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Mark me"/>
As for the customization of the control we can say that it extends [indirectly] TextView control, so all the formatting options already discussed in previous articles are also valid for this control.
In the application code we can use the methods isChecked () for the control state and setChecked ( state ) to establish a state to control.
if (checkBox.isChecked()){
checkBox.setChecked(false);
}
As for the possible events that can throw this control, the most interesting is undoubtedly the reporting that has changed the status of control, which is called OnCheckedChanged. To implement the actions of this event could therefore use the following logic:
final CheckBox cb = (CheckBox)findViewById(R.id.chkExample);
cb.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if(isChecked)
{
cb.setText("Checkbox marcked!");
}else{
cb.setText("Checkbox unmarcked!");
}
}
});
For more information you can visite the next page.
Control RadioButton
As controls checkbox, a radiobutton can be marked or unmarked, but in this case are often used within a group of options to one, and only one of them must be marked mandatory, meaning that if you mark one of they will automatically be unchecked which was inactive. Android, a radiobutton group is defined by RadioGroup element, which in turn contains all elements necessary RadioButton. An example of how to define a group of two radiobutton in our interface:
< RadioGroup android:id="@+id/gruporb"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
< RadioButton android:id= "@+id/ChkExample1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button A"/>
< RadioButton android:id= "@+id/ChkExample2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button B"/>
</RadioGroup >
First, here’s how we define the control group indicating their orientation (vertical or horizontal) as was the case for example with a LinearLayout. After that, add all objects needed RadioButton ID indicating your property by android: id and text using android: text.
Having defined the interface we can manipulate the control from our java code using different control methods RadioGroup, the most important: check (id) to make a choice determined by its ID, clearCheck () to deselect all options ygetCheckedRadioButtonId () which as its name suggests returns the ID of the highlighted option (or the value -1 if no checked). Here’s an example:
final RadioGroup rg = (RadioGroup)findViewById(R.id.gruporb);
rg.clearCheck();
rg.check(R.id.radio1);
int idSeleccionado = rg.getCheckedRadioButtonId();
As for the events sent, as in the case of checkboxes, the more important the reporting of changes in the selected element, also called in this case onCheckedChange. We see how to handle this event RadioGroup object:
final RadioGroup rg = (RadioGroup)findViewById(R.id.gruporb);
rg.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
lblMensaje.setText("ID opcion seleccionada: " + checkedid);
}
}
)
For more information you can visite the next page.