Pages

Subscribe:

Wednesday, February 2, 2011

Android - Spinner Demo Code


Screen


Layout

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget42"
android:layout_width="wrap_content"
android:layout_height="20px"
android:text="Week Day No"
android:layout_x="27px"
android:layout_y="32px"
>
</TextView>
<Spinner
android:id="@+id/weekdayno"
android:layout_width="150px"
android:layout_height="wrap_content"
android:layout_x="126px"
android:layout_y="16px"
>
</Spinner>
<Button
android:id="@+id/btnGetDay"
android:layout_width="121px"
android:layout_height="wrap_content"
android:text="Get Day"
android:layout_x="93px"
android:layout_y="113px"
>
</Button>
<EditText
android:id="@+id/weekday"
android:layout_width="227px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="45px"
android:layout_y="194px"
>
</EditText>
</AbsoluteLayout>


Java Code

package com.spinner.day;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class SpinnerDemo extends Activity {
Spinner wdayno;
Button getDay;
EditText wDay;
private static String[] dayArray = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        wdayno = (Spinner) findViewById(R.id.weekdayno);
        getDay = (Button) findViewById(R.id.btnGetDay);
        wDay = (EditText) findViewById(R.id.weekday);
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dayArray);
        
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
        wdayno.setAdapter(adapter);
        
        getDay.setOnClickListener((OnClickListener) new clicker());
        
    }
    
    class clicker implements Button.OnClickListener{
    
     public void onClick(View v) {
     String s = wdayno.getSelectedItem().toString();
     wDay.setText(s);
     }

    }
}

0 comments:

Post a Comment