Skip to content Skip to sidebar Skip to footer

How To Start Another Activity When Click On Item From Spinner Items

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 Copy

Solution 2:

public class Main2Activity extends Activity {

Toolbar mytoolbar;
Spinner mySpinner;

@OverrideprotectedvoidonCreate(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=newArrayAdapter<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(newAdapterView.OnItemSelectedListener() {

        publicvoidonClick(View v) {
            // TODO Auto-generated method stub

        }

        @OverridepublicvoidonItemSelected(AdapterView<?> arg0, View view,
                                   int position, long row_id)
        {
            final Intent intent;
            switch(position)
            {
                case1:
                    intent = newIntent(Main2Activity.this, MainhomeActivity.class);
                    startActivity(intent);
                    break;
                case2:
                    intent = newIntent(Main2Activity.this, DateActivity.class);
                    startActivity(intent);
                    break;

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

            }

        }

        @OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

    });


}

}

Solution 3:

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

Solution 4:

String  selection ;
acTV1.setAdapter(arrayAdapter);
acTV1.setCursorVisible(false);
acTV1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    publicvoidonItemClick(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);
        }
    });

Post a Comment for "How To Start Another Activity When Click On Item From Spinner Items"