Fill data to Spinner with ArrayAdapter : Spinner « UI « Android

Home
Android
1.2D Graphics
2.Animation
3.Core Class
4.Database
5.Date Type
6.Development
7.File
8.Game
9.Hardware
10.Media
11.Network
12.Security
13.UI
14.User Event
Android » UI » Spinner 
Fill data to Spinner with ArrayAdapter
  
package app.test;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Test extends Activity {
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    final Spinner spinner = (SpinnerfindViewById(R.id.testSpinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, Months);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    final Button changeButton = (ButtonfindViewById(R.id.enableButton);
    changeButton.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        changeOption(spinner);
      }
    });
    final Button changeButton2 = (ButtonfindViewById(R.id.backgroundColorButton);
    changeButton2.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        changeOption2(spinner);

      }
    });
  }

  static final String[] Months = new String[] { "January""February",
      "March""April""May""June""July""August""September",
      "October""November""December" };

  public void changeOption(Spinner spinner) {
    if (spinner.isEnabled()) {
      spinner.setEnabled(false);
    else {
      spinner.setEnabled(true);

    }
  }

  public void changeOption2(Spinner spinner) {
    spinner.setBackgroundColor(Color.RED);
  }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Spinner android:id="@+id/testSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/enableButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set isEnabled"/>
<Button android:id="@+id/backgroundColorButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Background Color"/>
</LinearLayout>

   
    
  
Related examples in the same category
1.Using Spinner
2.Spinner on selection listener
3.Disable and enable Spinner
4.Fill data from arrays.xml to Spinner
5.Fill data from arrays.xml to Spinner and ArrayAdapter
6.Spinner Test
7.Spinner Item Selected Listener
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.