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






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 = (Spinner) findViewById(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 = (Button) findViewById(R.id.enableButton);
    changeButton.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        changeOption(spinner);
      }
    });
    final Button changeButton2 = (Button) findViewById(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