0

I basically have a spinner displaying an three items. I want it so that if Click on one of these three items and then click a button it will open up a new activity.

 String[] count;
Spinner s;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button view = (Button) findViewById(R.id.button);
    s = (Spinner)findViewById(R.id.spinner);
    count = getResources().getStringArray(R.array.country_array);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,count);
    s.setAdapter(adapter);
    view.setEnabled(false); // initially disable the button
    s.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position,
                                   long id) {
            view.setEnabled(true); // enable when user selects any item
        }
        // Listen to button click
        view.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                //start activity
                startActivity();
            }
        }
    });
    public void startActivity() {
        Intent intent = new Intent(YourCurrentActivity.this, NewActivity.class);
        startActivity(intent);
    }
Anon
  • 1
  • 5

1 Answers1

0
view.setEnabled(false); // initially disable the button
s.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {
         view.setEnabled(true); // enable when user selects any item
    }

See how to listen to Button click: Android: how to handle button click

// Listen to button click
view.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                //start activity
                startActivity();
            }
        });


public void startActivity() {
    Intent intent = new Intent(YourCurrentActivity.this, NewActivity.class);
    startActivity(intent);
}
Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • hmm i understand how to use the button but im not sure where to place the code you gave me – Anon Nov 16 '14 at 02:55
  • Except the `startActivity` function place it in `onCreate()` after `.setAdapter`. – Shobhit Puri Nov 16 '14 at 03:15
  • ok so I did that already and I was getting a bunch of errors. – Anon Nov 16 '14 at 03:20
  • put `statActivity()` function definition outside the `onCreate()` function. You are defining one function inside other. I think going over basic would be better first than. Hope it helps. – Shobhit Puri Nov 16 '14 at 03:37