1

I am very new to Java. I am doing a school project at the moment and I have my main activity, then I have a settings activity. I am trying to modify the xml from the main activity with the settings activity. I am able to modify the settings xml file with the settings.java, but I would like to modify the main activity xml with settings.java

public class Settings extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);


    // Get the Intent that started this activity and extract the string

    Switch switchButton;
    final RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.activity_settings);
    final RelativeLayout mRelativeLayoutMain = (RelativeLayout) findViewById(R.id.activity_main);

    switchButton = (Switch) findViewById(R.id.switch1);
    switchButton.setChecked(true);
    switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
            if (bChecked) {
                mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
                mRelativeLayout.setBackgroundColor(Color.GRAY);
            } else {
                mRelativeLayoutMain.setBackgroundColor(Color.WHITE);
                mRelativeLayout.setBackgroundColor(Color.WHITE);
            }
        }
    });

    if (switchButton.isChecked()) {
        mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
        mRelativeLayout.setBackgroundColor(Color.GRAY);
    } else {
        mRelativeLayoutMain.setBackgroundColor(Color.WHITE);
        mRelativeLayout.setBackgroundColor(Color.WHITE);
    }}


public void toast1(View view) {
    android.widget.Toast.makeText(this, "Created by Cody Walls and Tommy Serfas", android.widget.Toast.LENGTH_LONG).show();
}

/*public void switch1(View view) {
    ScrollView mScrollView = (ScrollView) findViewById(R.id.scrollView);
    mScrollView.setBackgroundColor(Color.GRAY);
}*/



 }

In the Code I am trying to change the background of the main activity xml with : mRelativeLayoutMain.setBackgroundColor(Color.GRAY); and when I run the app and click the intent it will crash with the error:

"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference"

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
  • Not quite sure what you are asking. Are you asking how you would permanently change the contents of the Main activity by changing settings values inside the Settings activity? – Carl Poole Mar 14 '17 at 21:37
  • You may want to add more detail. Maybe explain more about your problem. Give examples. Give code. – Rob Avery IV Mar 14 '17 at 21:46

2 Answers2

1

I think the easiest way is to create an PreferenceManager.SharedPreferences, in which I recommend you to store current app data. This will help you not to loose any changes in app after you exit the it. Here is short instructions:

  1. Create button in settings activity which will change something in main activity.
  2. Create onClickListener for your button.
  3. Use .SharedPreferences to store was you button clicked or not. (I recommend storing boolean variables, this way you can store was button clicked or not.)
  4. I both of your activities in onCreate method call .getSharedPreferences to read saved app values. (I mean to read was the button clicked or not.)
  5. Use app values you got from 4. to change any element in activity. (For example if you stored that button was clicked, then change some TextView text or etc.)

I hope you understood the idea.

Link to the Android developer tutorial about App key values storing & saving

Link to the StackOverflow much easier explanation & examples

Community
  • 1
  • 1
0

There are a couple of ways of doing this (Some of which depends on how you are switching back and forth from each activity). It also depends on what things you are changing.

From your settings page, as you are changing different settings, you'll save this content within Preferences. (You can see more how to use Preferences here: https://examples.javacodegeeks.com/android/core/ui/settings/android-settings-example/ or by just Googling it).

On you main activity, depending on how you come back to it (onStart most likely), you can setup the things you need to programmatically.

So, you may need to do a little research on the Android lifecycle and how each cycle works (https://developer.android.com/guide/components/activities/activity-lifecycle.html), how to program the UI programmatically through Java (http://startandroid.ru/en/lessons/220-lesson-16-creating-layout-programmatically-layoutparams.html), and the Preferences Android library to save certain settings.

The xml isn't meant to be "altered". You can change the UI programmatically. It's possible to build an Android app without any xml. When Android was first built, it didn't use the xml to create the UI. It was all done through Java. It was then added to use xml to create your activities or fragments or any UI component. This made things easier for more static activities or activities with very little dynamic content.

Rob Avery IV
  • 3,562
  • 10
  • 48
  • 72