How can I change the background color of a Main Activity by using spinner while clicking a button in the...












2















I want to change the background color of Main activity by using spinner in second Activity. I have already created one button and it goes to second activity and in this second activity I have created spinner which consist of which color should be in the main activity. After choosing the color, the button I created will change the background color and will be back to first activity.










share|improve this question























  • Make a model of the colour class, create colour object, get and set the colour object accordingly. Therefore, in your second activity, you'll create a colour object and set the desired colour on setOnItemSelectedListener. When you get back to the MainActivity, get the object and set the colour accordingly.

    – Nero
    Nov 23 '18 at 21:22











  • Thank you so much!

    – Semih Altaş
    Nov 27 '18 at 21:22
















2















I want to change the background color of Main activity by using spinner in second Activity. I have already created one button and it goes to second activity and in this second activity I have created spinner which consist of which color should be in the main activity. After choosing the color, the button I created will change the background color and will be back to first activity.










share|improve this question























  • Make a model of the colour class, create colour object, get and set the colour object accordingly. Therefore, in your second activity, you'll create a colour object and set the desired colour on setOnItemSelectedListener. When you get back to the MainActivity, get the object and set the colour accordingly.

    – Nero
    Nov 23 '18 at 21:22











  • Thank you so much!

    – Semih Altaş
    Nov 27 '18 at 21:22














2












2








2








I want to change the background color of Main activity by using spinner in second Activity. I have already created one button and it goes to second activity and in this second activity I have created spinner which consist of which color should be in the main activity. After choosing the color, the button I created will change the background color and will be back to first activity.










share|improve this question














I want to change the background color of Main activity by using spinner in second Activity. I have already created one button and it goes to second activity and in this second activity I have created spinner which consist of which color should be in the main activity. After choosing the color, the button I created will change the background color and will be back to first activity.







java android android-studio






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 20:46









Semih AltaşSemih Altaş

111




111













  • Make a model of the colour class, create colour object, get and set the colour object accordingly. Therefore, in your second activity, you'll create a colour object and set the desired colour on setOnItemSelectedListener. When you get back to the MainActivity, get the object and set the colour accordingly.

    – Nero
    Nov 23 '18 at 21:22











  • Thank you so much!

    – Semih Altaş
    Nov 27 '18 at 21:22



















  • Make a model of the colour class, create colour object, get and set the colour object accordingly. Therefore, in your second activity, you'll create a colour object and set the desired colour on setOnItemSelectedListener. When you get back to the MainActivity, get the object and set the colour accordingly.

    – Nero
    Nov 23 '18 at 21:22











  • Thank you so much!

    – Semih Altaş
    Nov 27 '18 at 21:22

















Make a model of the colour class, create colour object, get and set the colour object accordingly. Therefore, in your second activity, you'll create a colour object and set the desired colour on setOnItemSelectedListener. When you get back to the MainActivity, get the object and set the colour accordingly.

– Nero
Nov 23 '18 at 21:22





Make a model of the colour class, create colour object, get and set the colour object accordingly. Therefore, in your second activity, you'll create a colour object and set the desired colour on setOnItemSelectedListener. When you get back to the MainActivity, get the object and set the colour accordingly.

– Nero
Nov 23 '18 at 21:22













Thank you so much!

– Semih Altaş
Nov 27 '18 at 21:22





Thank you so much!

– Semih Altaş
Nov 27 '18 at 21:22












1 Answer
1






active

oldest

votes


















0














From what I understand, you need the ActivityForResult behavior.




  • You use startActivityForResult to fire the Intent from your first activity to your second, along with a request code.

  • You use an Intent and setResult to send data from your second activity back to your first.

  • You override onActivityResult in your fist activity to get and use your data.


Sample code:



public class FirstActivity extends Activity {
private static final int PICK_COLOR_REQUEST = 1001;

...

private void pickColor() {
Intent pickColorIntent = new Intent(this, SecondActivity.class);
startActivityForResult(pickColorIntent, PICK_COLOR_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_COLOR_REQUEST && resultCode == Activity.RESULT_OK) {
int color = data.getIntExtra("color");
/* use the color */
}
}
}

public class SecondActivity extends Activity {
...

private void onColorPicked(int color) {
Intent dataIntent = new Intent();
dataIntent.putExtra("color", color);

setResult(Activity.RESULT_OK,returnIntent);
finish();
}
}





share|improve this answer
























  • Thank you so much! It helped a lot!

    – Semih Altaş
    Nov 27 '18 at 21:21











  • @SemihAltaş great to hear, if you want, mark my answer as correct

    – iFanie
    Nov 28 '18 at 19:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452769%2fhow-can-i-change-the-background-color-of-a-main-activity-by-using-spinner-while%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














From what I understand, you need the ActivityForResult behavior.




  • You use startActivityForResult to fire the Intent from your first activity to your second, along with a request code.

  • You use an Intent and setResult to send data from your second activity back to your first.

  • You override onActivityResult in your fist activity to get and use your data.


Sample code:



public class FirstActivity extends Activity {
private static final int PICK_COLOR_REQUEST = 1001;

...

private void pickColor() {
Intent pickColorIntent = new Intent(this, SecondActivity.class);
startActivityForResult(pickColorIntent, PICK_COLOR_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_COLOR_REQUEST && resultCode == Activity.RESULT_OK) {
int color = data.getIntExtra("color");
/* use the color */
}
}
}

public class SecondActivity extends Activity {
...

private void onColorPicked(int color) {
Intent dataIntent = new Intent();
dataIntent.putExtra("color", color);

setResult(Activity.RESULT_OK,returnIntent);
finish();
}
}





share|improve this answer
























  • Thank you so much! It helped a lot!

    – Semih Altaş
    Nov 27 '18 at 21:21











  • @SemihAltaş great to hear, if you want, mark my answer as correct

    – iFanie
    Nov 28 '18 at 19:14
















0














From what I understand, you need the ActivityForResult behavior.




  • You use startActivityForResult to fire the Intent from your first activity to your second, along with a request code.

  • You use an Intent and setResult to send data from your second activity back to your first.

  • You override onActivityResult in your fist activity to get and use your data.


Sample code:



public class FirstActivity extends Activity {
private static final int PICK_COLOR_REQUEST = 1001;

...

private void pickColor() {
Intent pickColorIntent = new Intent(this, SecondActivity.class);
startActivityForResult(pickColorIntent, PICK_COLOR_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_COLOR_REQUEST && resultCode == Activity.RESULT_OK) {
int color = data.getIntExtra("color");
/* use the color */
}
}
}

public class SecondActivity extends Activity {
...

private void onColorPicked(int color) {
Intent dataIntent = new Intent();
dataIntent.putExtra("color", color);

setResult(Activity.RESULT_OK,returnIntent);
finish();
}
}





share|improve this answer
























  • Thank you so much! It helped a lot!

    – Semih Altaş
    Nov 27 '18 at 21:21











  • @SemihAltaş great to hear, if you want, mark my answer as correct

    – iFanie
    Nov 28 '18 at 19:14














0












0








0







From what I understand, you need the ActivityForResult behavior.




  • You use startActivityForResult to fire the Intent from your first activity to your second, along with a request code.

  • You use an Intent and setResult to send data from your second activity back to your first.

  • You override onActivityResult in your fist activity to get and use your data.


Sample code:



public class FirstActivity extends Activity {
private static final int PICK_COLOR_REQUEST = 1001;

...

private void pickColor() {
Intent pickColorIntent = new Intent(this, SecondActivity.class);
startActivityForResult(pickColorIntent, PICK_COLOR_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_COLOR_REQUEST && resultCode == Activity.RESULT_OK) {
int color = data.getIntExtra("color");
/* use the color */
}
}
}

public class SecondActivity extends Activity {
...

private void onColorPicked(int color) {
Intent dataIntent = new Intent();
dataIntent.putExtra("color", color);

setResult(Activity.RESULT_OK,returnIntent);
finish();
}
}





share|improve this answer













From what I understand, you need the ActivityForResult behavior.




  • You use startActivityForResult to fire the Intent from your first activity to your second, along with a request code.

  • You use an Intent and setResult to send data from your second activity back to your first.

  • You override onActivityResult in your fist activity to get and use your data.


Sample code:



public class FirstActivity extends Activity {
private static final int PICK_COLOR_REQUEST = 1001;

...

private void pickColor() {
Intent pickColorIntent = new Intent(this, SecondActivity.class);
startActivityForResult(pickColorIntent, PICK_COLOR_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_COLOR_REQUEST && resultCode == Activity.RESULT_OK) {
int color = data.getIntExtra("color");
/* use the color */
}
}
}

public class SecondActivity extends Activity {
...

private void onColorPicked(int color) {
Intent dataIntent = new Intent();
dataIntent.putExtra("color", color);

setResult(Activity.RESULT_OK,returnIntent);
finish();
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 0:19









iFanieiFanie

1618




1618













  • Thank you so much! It helped a lot!

    – Semih Altaş
    Nov 27 '18 at 21:21











  • @SemihAltaş great to hear, if you want, mark my answer as correct

    – iFanie
    Nov 28 '18 at 19:14



















  • Thank you so much! It helped a lot!

    – Semih Altaş
    Nov 27 '18 at 21:21











  • @SemihAltaş great to hear, if you want, mark my answer as correct

    – iFanie
    Nov 28 '18 at 19:14

















Thank you so much! It helped a lot!

– Semih Altaş
Nov 27 '18 at 21:21





Thank you so much! It helped a lot!

– Semih Altaş
Nov 27 '18 at 21:21













@SemihAltaş great to hear, if you want, mark my answer as correct

– iFanie
Nov 28 '18 at 19:14





@SemihAltaş great to hear, if you want, mark my answer as correct

– iFanie
Nov 28 '18 at 19:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452769%2fhow-can-i-change-the-background-color-of-a-main-activity-by-using-spinner-while%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...