Friday, July 4, 2014

Android Day 2 - Input Controls part 2

CheckBoxes

Application can create a Checkbox using CheckBox class. Like Button, the onClick attribute can specify the method. the code is something like below

public void onCheckbxClicked(View view)
{
boolean checked = ((CheckBox)view).isChecked();
switch (view.getId())
{
case R.id.checkbox_meat:
if(checked)
{
//do something
}
}
}

RadioButton and RadioGroup can be used for creating the RadioButton implementation. RadioButton needs to be placed inside a RadioGroup. The sample is like below

android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:orientation = "vertical">

android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text="@string/pirates"
android:onClick="onRadioButtonClicked"/>

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "@string/ninjas"
android:onClick="onRadioButtonClicked"/>

 

ToggleButton class can be used to create a Toggle button which can be used to switch between two states. Android 4.0 API Level 14 has introduced another kind of toggle button called Switch. These two classes are subclass of CompoundButton and function like in same manner. 

Programmatically an application can add the listener like in below code. 

ToggleButton tb = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.onCheckedChangeListener())
{
public void onCheckedChanged(CompoindButton buttonView, boolean isChecked)
{
}
}

Spinners allow to select a value from a list of values. Populating values to a Spinner is similar to how it is done for a list, i.e. using ArrayAdapter or a CursorAdapter
sample is like below.

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.planets_array,android.R.laout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

Picker 
Android provides picker as a control to provide picking of time or date as a ready-to-use dialogs. Picking date using these picker ensures that the user selects the date/time info correctly adjusted to the user locale and formatted properly. Key classes are DatePickerDialog, TimePickerDialog, DialogFragment

Android recommends to use the DialogFragment class to host the pickers. DialogFragment class provides a way to display the pciekr is different layout and configurations. For e.g. basic dialling on handsets or as an embedded part of the layout on large screens. 

The DialogFragment was added first in Api level 3.0 (API Level 11), even older version can have DialogFragment using the support library. 


No comments:

Post a Comment