How can I change the background color of a Main Activity by using spinner while clicking a button in the...
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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
add a comment |
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
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
add a comment |
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
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
java android android-studio
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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();
}
}
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
}
}
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
add a comment |
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();
}
}
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
add a comment |
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();
}
}
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();
}
}
answered Nov 24 '18 at 0:19
iFanieiFanie
1868
1868
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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