title: Launch different activities depending on spinner selection
body: <p>I've got and activity with two spinners and a button. The values of the second spinner change depending on the item selected in the first one.</p>
  
  <p>Now, what I want to do is to launch an activity or another depending on the selection in the second spinner after pressing an "OK" button.</p>
  
  <p>Here is the code of the main activity:</p>
  
  <pre><code>public class UniversityActivity extends Activity implements OnItemSelectedListener {
  
  Spinner s1, s2;
  Button acceptbutton;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  
      getActionBar().setDisplayHomeAsUpEnabled(true);
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.university_layout);
      addListenerOnButton();
  
      s1 = (Spinner) findViewById(R.id.spinner1);
      s2 = (Spinner) findViewById(R.id.spinner2);
      s1.setOnItemSelectedListener(this);
  }
  
  public void addListenerOnButton() {
      final Context context = this;
  
      acceptbutton = (Button) findViewById(R.id.accept);
      acceptbutton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View arg0) {
              Intent intent = new Intent(context, DegreeActivity.class);
              startActivity(intent);
              overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
          }
      });
  
  }
  
  @Override
  public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
          long arg3) {
      String sp1= String.valueOf(s1.getSelectedItem());
      if(sp1.contentEquals("Universidad Politécnica de Madrid")) {
          List&lt;String&gt; list = new ArrayList&lt;String&gt;();
          list.add("Grado en Ingeniería Aeronáutica");
          list.add("Grado en Ingeniería Geológica");
          list.add("Grado en Ingeniería Informática");
          ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this,
              android.R.layout.simple_spinner_item, list);
          dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          dataAdapter.notifyDataSetChanged();
          s2.setAdapter(dataAdapter);
      }
      if(sp1.contentEquals("Universidad Autónoma de Madrid")) {
          List&lt;String&gt; list = new ArrayList&lt;String&gt;();
          list.add("Grado en Historia");
          list.add("Grado en Ingeniería Química");
          list.add("Grado en Ingeniería en Tecnologías Industriales");
          ArrayAdapter&lt;String&gt; dataAdapter2 = new ArrayAdapter&lt;String&gt;(this,
              android.R.layout.simple_spinner_item, list);
                dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          dataAdapter2.notifyDataSetChanged();
          s2.setAdapter(dataAdapter2);
      }
  
  }
  
  @Override
  public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
      // TODO Auto-generated method stub
  
  }
  
  }
  </code></pre>
  
  <p>And this is its layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" &gt;
  
  &lt;!-- Select university --&gt;
  &lt;TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:padding="8dp"
      android:text="@string/selectuniv"
      android:textColor="@color/yellow" /&gt;
  
  &lt;!-- Spinner 1 Dropdown --&gt;
  &lt;Spinner
      android:id="@+id/spinner1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="8dp"
      android:layout_marginRight="8dp"
      android:layout_marginTop="5dp"
      android:entries="@array/univ" /&gt;
  
  &lt;!-- Select degree --&gt;
  &lt;TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:padding="8dip"
      android:text="@string/selectdeg"
      android:textColor="@color/yellow" /&gt;
  
  &lt;!-- Spinner 2 Dropdown --&gt;
  &lt;Spinner
      android:id="@+id/spinner2"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="10dp"
      android:layout_marginLeft="8dp"
      android:layout_marginRight="8dp" /&gt;
  
  &lt;!-- Accept button --&gt;
  &lt;Button
      android:id="@+id/accept"
      android:layout_width="250dp"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:layout_marginTop="10dp"
      android:textSize="13sp"
      android:textColor="#ffffff"
      android:text="@string/accept"
      android:background="@drawable/custombutton2" /&gt;
  
   &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I hope you can help me. Thank you very much.</p>