1

I have a problem. I have 3 activities (MainActivity, DetailsActivity, SettingsActivity) and in SettingsActivity I have a Togglebutton "Nightmode". What I want is, when the button is changed, change background of all three activities on gray color.

public class SettingsActivity extends AppCompatActivity {
//This is SettingsActivity(not Main one)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    TextView SettingsTitle = (TextView) findViewById(R.id.SettingsTitle);
    TextView NightText = (TextView) findViewById(R.id.NightmodeText);
    ToggleButton toggleNightMode = (ToggleButton) findViewById(R.id.toggleNightmode);
    final RelativeLayout NightBG = (RelativeLayout) findViewById(R.id.NightBG);
    final LinearLayout DetailsBG = (LinearLayout) findViewById(R.id.mainBG);
    final LinearLayout HomeBG = (LinearLayout) findViewById(R.id.HomeBG);

    toggleNightMode.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NightBG.setBackgroundColor(Color.parseColor("#545657"));
            HomeBG.setBackgroundColor(Color.parseColor("#545657"));
            DetailsBG.setBackgroundColor(Color.parseColor("#545657"));

        }
    });

NightBG is in the same activity as that java file (SettingsActivity). But HomeBG is in MainActivity and DetailsBG is in the DetailsActivity. Everytime I start the app, and press on that button, app craches. If I delete HomeBG and DetailsBG from this file, it works just fine with changing current layout's color to gray. Please help me.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • It seems that the Home, and Details views are not present in settings activity. I imagine this is causing a null reference exception on the nightBG object. I think you will have to find another way to access the other activities. Not sure how to do cross activity communication at the moment. – Ay Rue Oct 13 '16 at 22:06
  • That's what I'm thinking about yeah. The problem is that I'm a total beginner at programming at java. I hope we figure out something :P. Thanks anyway! :) –  Oct 13 '16 at 22:12
  • I submitted an answer that is the best I can do at the moment, it will give you another option at the very least. – Ay Rue Oct 13 '16 at 22:19

4 Answers4

2

One easy way to store little settings like this across multiple activities that may not be open/active at the time of the button click would be to use SharedPreferences.

It might be a little overkill for such a simple piece of code but you can always give it a try if you don't find anything else.

Your code could look something like this:

toggleNightMode.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Set the color of this activity
        int color = Color.parseColor("#545657")
        View view = SettingsActivity.this.getWindow().getDecorView();
        view.setBackgroundColor(color);
        // Save color preference
        SharedPreferences sharedPref = SettingsActivity.this.getSharedPreferences("bgColorFile",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt("color", color);
        editor.apply();    
    }
});

And then when you open your activities you place something like this in the onStart() or onCreate() method of your activity:

// Get the color preference
SharedPreferences sharedPref = getSharedPreferences("bgColorFile",Context.MODE_PRIVATE);
int colorValue = sharedPref.getInt("color", 0);
View view = this.getWindow().getDecorView();
view.setBackgroundColor(colorValue);

So what you're actually doing is storing the background color as persistent data and fetching it once you reopen/open the activity that you want to have the color on. The benefit of this method is that whenever you close your app the preferred background color will be remembered. I hope this helps.

Reilem
  • 345
  • 4
  • 10
  • http://i.imgur.com/xZeiwKC.png It says that it cannot resolve some methods –  Oct 14 '16 at 13:44
  • Yeah my bad, if you're calling from within an activity you don't have to call getActivity(). I changed my example code to match this. However I'm not quite sure why you are getting an error on getWindow(). Is your MainActvity subclass of AppCompatActivity or just Activity? But this is just a way to change the background color, if this method isn't working for you then there are plenty of other ways to do it. – Reilem Oct 14 '16 at 14:07
  • I updated the code in android studio, but still getting errors with ".getWindow()" and "= getActivity" Umm.. Anyway, its says "public class MainActivity extends AppCompatActivity" So I guess its a subclass.. Its the same in all 3 activities.. –  Oct 14 '16 at 15:24
  • Ah yes I forgot that when defining code blocks like OnClickListeners "this" is no longer a reference to the activity. I replaced "this" with "SettingsActivity.this", try to see if this works now? – Reilem Oct 14 '16 at 15:53
0

Change background for current activity in the same activity. Since DetailsActivity is not running, you can't do that, it gives you null pointer. Is kind of you are trying to eat 3 apples and you have just one. After current activity is started, change background.

Update:

You can do that in current activity and just in current activity:

findViewById(android.R.id.content).setBackground(getColor(R.color.your_color));

Don't try to call this in other activities that are not running.

setBackground()

or

setBackgroundColor()
Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
  • Can you post an example with a code here please?. I'm a total beginner at programming in Java. –  Oct 13 '16 at 22:10
  • Oh allright. But how to change background color of other activities then. Hm.. There must be a way! –  Oct 13 '16 at 22:20
  • 1
    You can't do that unless that activity is running. You can set in activity layout the color that you want. Is like trying to change color of you room, but you don't have a room. – Cătălin Florescu Oct 13 '16 at 22:21
0

If your other activities are open, you should send a message to the other activities by using an Intent.

How to send string from one activity to another?

When you receive the Intent you could then set the background of the activity.

If your other activities are not open yet, you will not be able to send an Intent to them. In this case you could have each Activity reference a static value in your main activity that could contain the current background color. You would want to reference that value on the other activities on create functions.

Here is an example on how to reference a variable from another activity.

How do I get a variable in another activity?

This might not be the most pretty way to handle it but it should work.

Community
  • 1
  • 1
Ay Rue
  • 228
  • 1
  • 3
  • 15
  • Okay thank you!! I will try this in 16hours(I'm going in bed now)! Will tell you if that works or not. –  Oct 13 '16 at 22:38
0

as Ay Rue said you have 2 options: use static variable for that button, and then in onResume of each activity, check the value of the static variable (true or false). or you can save a private variable nightMode and then pass this value in the intent when you need to move to the other two activities. don't set the background color if you already set before and have an updated background color.

T.S
  • 911
  • 8
  • 24