Showing posts with label controls. Show all posts
Showing posts with label controls. Show all posts

Selection Controls 2 The spinner Control

| 0 comments |
The spinner controls is similar to the ComboBox in C#. it displays a list to select from in a popup window so ot may become a better choice over ListView if you want to save space.

It works in a similar way to that of the the listView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android_orientation="vertical"
android_layout_width="fill_parent"
android_layout_height="fill_parent"
>
<Spinner
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_id="@+id/Spinner"
/>
</LinearLayout>




when you click on the spinner it popups like this:
To handle the selected item you can use do it like this:
final String [] items=new String[]{"Item1","Item2","Item3","Item4"};
ArrayAdapter ad=new ArrayAdapter(this,android.R.layout.simple_spinner_item,items);
ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spin=(Spinner)findViewById(R.id.Spinner);
spin.setAdapter(ad);
spin.setOnItemSelectedListener(new OnItemSelectedListener()
{

public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {
TextView txt=(TextView)findViewById(R.id.txt);
TextView temp=(TextView)arg1;
txt.setText(temp.getText());

}

public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub

}

});

The above code displays the selected item text in the textview.

The parameters of the OnItemClick method are:


Arg0:the Spinner, notice that it is of type AdapterView.

Arg1: the view that represents the selected item, in this example it will be a TextView

Arg2: the position of the selected item.

Arg3: the id of the selected item.

and thats it for the Spinner Control.
Read More..

Selection Controls 3 CheckBox and RadioButton

| 0 comments |
CheckBox:

The check box has two states: Checked and UnChecked. It inherits from TextView so it has all of its properties:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android_orientation="vertical"
android_layout_width="fill_parent"
android_layout_height="fill_parent"
>
<TextView
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_id="@+id/txt"
/>
<CheckBox
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_id="@+id/Chk"
android_text="This is a check box"
android_checked="false"

/>
</LinearLayout>



and to handle the check/uncheck event:
CheckBox chk=(CheckBox)findViewById(R.id.Chk);
chk.setOnCheckedChangeListener(new OnCheckedChangeListener()
{

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
TextView txt=(TextView)findViewById(R.id.txt);
if (arg1)
txt.setText("checked");
else
txt.setText("Unchecked");

}

}
);


RadioButton:
Android provides Radio button control. You create a RadioGroup and add RadioButtons inside it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android_orientation="vertical"
android_layout_width="fill_parent"
android_layout_height="fill_parent"
>
<TextView
android_id="@+id/txt"
android_layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<RadioGroup
android_id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Radio Group"
>
<RadioButton android_id="@+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item1"
android:checked="true"
/>
<RadioButton android_id="@+id/item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item2" />
<RadioButton android_id="@+id/item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item3" />
</RadioGroup>
</LinearLayout>




You can get the checked item from code like this:
RadioGroup rg=(RadioGroup)findViewById(R.id.group);
rg.setOnCheckedChangeListener(new android.widget.RadioGroup.OnCheckedChangeListener()
{

public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
TextView txt=(TextView)findViewById(R.id.txt);
RadioButton rb=(RadioButton)findViewById(arg1);
txt.setText("You selected "+rb.getText());

}

}
);

Read More..