Search This Blog

Monday, January 2, 2012

SPINNER ACTIVITY WITH ANDROID

First of all create the spinnerlayout.cml in android and put thr following code into it.

<linearlayout
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:orientation="vertical"
            xmlns:android="http://schemas.android.com/apk/res/android">

<textview android:id="@+id/TV1"
               android:layout_height="wrap_content"
               android:layout_width="fill_parent"
               android:text="Enter the class:">

<spinner
              android:drawselectorontop="true"
              android:entries="@array/items"
              android:id="@+id/spinner1"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:prompt="@string/prompt">
<spinner android:drawselectorontop="true"
              android:id="@+id/spinner2"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:prompt="@string/Face">

</spinner>
</spinner>
</textview>
</linearlayout>


NOW edit the string file and insert the following into it :


<string-array name="items">
    <item>ItemA</item>
    <item>ItemB</item>
    <item>ItemC</item>
    <item>ItemD</item>
    <item>ItemE</item>
    <item>ItemF</item>
    <item>ItemG</item>
    <item>ItemH</item>
    <item>ItemI</item>
    </string-array>


<string name="prompt">Select from categories</string>
<string name="Face">SELECT THIS</string>



Now create the java class and update the following in that java file....

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;






public class SpinnerDemoActivity extends Activity implements AdapterView.OnItemSelectedListener {




/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerlayout);
//TextView selection= (TextView)findViewById(R.id.TV1);
Spinner spin=(Spinner)findViewById(R.id.spinner2);


Toast.makeText(this, "Redirecting to the Fourth Tutorial", Toast.LENGTH_LONG).show();


ArrayAdapter aa=ArrayAdapter.createFromResource(this, R.array.types, android.R.layout.simple_spinner_dropdown_item);


spin.setAdapter(aa);
}
public void onItemSelected(AdapterView arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub


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


}
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.