2

I have spinner which including many items and want when I click on one item from those items it open another activity

here is the spinner in layout

<Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:prompt="@string/title" android:entries="@array/items" /> 

here is the items in string

<string name="title">select</string> <string-array name="items"> <item>open activity one</item> <item>open activity two</item> </string-array> 

here is the code that i want to make it able to open another activity when i click on the items

Spinner Spinner = (Spinner) findViewById(R.id.spinner); Spinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onClick(View v) { // TODO Auto-generated method stub } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); 

4 Answers 4

4

try this:

Spinner Spinner = (Spinner) findViewById(R.id.spinner); Spinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onClick(View v) { // TODO Auto-generated method stub } @Override public void onItemSelected(AdapterView<?> arg0, View view, int position, long row_id) { final Intent intent; switch(position){ case 1: intent = new Intent(CurrentActivity.this, TargetActivity1.class); break; case 2: intent = new Intent(CurrentActivity.this, TargetActivity2.class); break; // and so on // ..... } startActivity(intent); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); 
Sign up to request clarification or add additional context in comments.

Comments

1

public class Main2Activity extends Activity {

Toolbar mytoolbar; Spinner mySpinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); mytoolbar=(Toolbar) findViewById(R.id.toolbar); mySpinner=(Spinner) findViewById(R.id.spinner); ArrayAdapter<String> myAdaptor=new ArrayAdapter<String>(Main2Activity.this, R.layout.custom_spinner_item, getResources().getStringArray(R.array.names)); myAdaptor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mySpinner.setAdapter(myAdaptor); mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onClick(View v) { // TODO Auto-generated method stub } @Override public void onItemSelected(AdapterView<?> arg0, View view, int position, long row_id) { final Intent intent; switch(position) { case 1: intent = new Intent(Main2Activity.this, MainhomeActivity.class); startActivity(intent); break; case 2: intent = new Intent(Main2Activity.this, DateActivity.class); startActivity(intent); break; 

// and so on // .....

 } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } 

}

Comments

0

you already have the onItemSelected method. So just put a switch-case statement in there with arg2 as argument. arg2 is the position of the item. so just put your intent to open another activity in the case you need. cheers

Comments

0
String selection ; acTV1.setAdapter(arrayAdapter); acTV1.setCursorVisible(false); acTV1.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { acTV1.showDropDown(); String selection = (String) parent.getItemAtPosition(position); Toast.makeText(getApplicationContext(), selection, Toast.LENGTH_SHORT); if(selection.equals("Delete")) { intent = new Intent(ImageAttachmentActivity.this, DeleteEmployeeActivity.class); startActivity(intent); } }); 

1 Comment

Can you please elaborate more on how this solves OP's question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.