Mr.AwfulAtProgramming Mr.AwfulAtProgramming - 1 month ago 8
Java Question

Passing string from spinner to second activity

I'm building an app in which the first activity contains a spinner with strings "Red", "Yellow", "Blue" and "Green". When the user selects a spinner item, the second activity's background will be that color. I'm having issues with passing that value from the MainActivity class to the Display class.

Answer

In case you wonder how to retrieve the spinner value :

String pickedColor = yourSpinner.getSelectedItem().toString();

In your first Activity, when building intent do something like :

Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
intent.putExtra("COLOR_KEY", pickedColor);
startActivity(intent);

Then in your second activity :

String color = getIntent().getStringExtra("COLOR_KEY");

Then do whatever you need :)