Open new activity on spinner selected item in android

How to start new activity screen programmatically after selecting spinner item text.

In this tutorial we are creating a simple spinner with three text items values. Now we are adding setOnItemSelectedListener function on spinner to get selected item of spinner and transfer the spinner selected item value to string variable. Now after that we are adding setOnClickListener method on button and inside that button we are implementing switch case and matching string variable to all the switch cases and open the condition match activity. So here is the complete step by step tutorial for How to open new activity on spinner selected item in android.

android-project-download-code-button

List of Activity inside this project:

  1. MainActivity,java
  2. Android_Activity.java
  3. BLOGGER_Activity.java
  4. PHP_Activity.java

List of xml layout files inside this project.

  1. activity_main.xml
  2. activity_android.xml
  3. activity_blogger.xml
  4. activity_php.xml

How to open new activity on spinner selected item in android.

Code for MainActivity.java file.

 package com.newactivityonspinner_android_examples.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;


public class MainActivity extends Activity {

 Spinner spinner;
 String[] SPINNERVALUES = {"ANDROID","PHP","BLOGGER"};
 String SpinnerValue;
 Button GOTO;
 Intent intent;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 spinner =(Spinner)findViewById(R.id.spinner1);
 
 GOTO = (Button)findViewById(R.id.button1);
 
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, SPINNERVALUES);
 
 spinner.setAdapter(adapter);
 
 //Adding setOnItemSelectedListener method on spinner.
 spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
 
 @Override
 public void onItemSelected(AdapterView<?> parent, View view,
 int position, long id) {
 
 SpinnerValue = (String)spinner.getSelectedItem(); 
 }
 
 @Override
 public void onNothingSelected(AdapterView<?> parent) {
 // TODO Auto-generated method stub
 
 }
 });
 
 GOTO.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 switch(SpinnerValue){
 
 case "ANDROID":
 intent = new Intent(MainActivity.this, Android_Activity.class);
 startActivity(intent);
 break;
 
 case "PHP":
 intent = new Intent(MainActivity.this, PHP_Activity.class);
 startActivity(intent);
 break;
 
 case "BLOGGER":
 intent = new Intent(MainActivity.this, BLOGGER_Activity.class);
 startActivity(intent);
 break;
 
 
 } 
 }
 });
 
 }
 }

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.newactivityonspinner_android_examples.com.MainActivity" >

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/spinner1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="107dp"
 android:text="CLICK HERE TO GO ON SPINNER SELECTED ITEM ACTIVITY" />

</RelativeLayout>

Code for Android_Activity.java file.

package com.newactivityonspinner_android_examples.com;
import android.app.Activity;
import android.os.Bundle;

public class Android_Activity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_android);
 }

}

Code for activity_android.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.newactivityonspinner_android_examples.com.Android_Activity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="THIS IS ANDROID ACTIVITY"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Code for BLOGGER_Activity file.

package com.newactivityonspinner_android_examples.com;
import android.app.Activity;
import android.os.Bundle;

public class BLOGGER_Activity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_blogger);
 }
}

Code for activity_blogger.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.newactivityonspinner_android_examples.com.BLOGGER_Activity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="THIS IS BLOGGER ACTIVITY"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Code for PHP_Activity.java file.

package com.newactivityonspinner_android_examples.com;
import android.app.Activity;
import android.os.Bundle;

public class PHP_Activity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_php);
 }
}

Code for activity_php.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.newactivityonspinner_android_examples.com.PHP_Activity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="THIS IS PHP ACTIVITY"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshots:

Click here to download Open new activity on spinner selected item in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *