0

Pretty much I have a Toggle filter on my main layout, and I have another class called settings, settings creates a different layout. I want to check if my filter is toggled on so that a button in my settings layout can have an if else statement on press.

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
         ToggleButton filterButton = findViewById(R.id.filter_button);
         TextView lightbulb = findViewById(R.id.lightbulb);
         if(filterButton.isChecked())
           lightbulb.setText("on");

in my settings class what I want to do is

public class SettingsActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        Button useLightbulb = findViewById(R.id.useLightbulb); 

        useLightbulb.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
              if super(filterButton.isChecked()))  
                 super.lightbulb.setText("1 Light Bulb burnt out");
             }
        }

any help is appreciated, I'm still a beginner so please dumb it down for me because I dont know what I'm doing

1 Answer 1

1

You don't do it like this. You don't access the views or layout of one screen from another- in fact they wouldn't exist. Instead, when the Toggle is pressed you would write the new value to some other place- a file, a shared preference, or even just a global variable. Then the other activity would check that value. Your UI shouldn't hold your state, your state should be held elsewhere and your view reflects it.

Sign up to request clarification or add additional context in comments.

2 Comments

OOOH that makes sense! how does one distinguish where the file you would be writing and reading to is located? from my experience its usually my name/documents or something also would I have to make some sort of command like on backpress refresh?
I'd probably just use a shared preference. A shared preference is a simple way of handling small data files of options like this. For that system, you just give it a name (whatever you want) and the shared preference system will do the storage for you (it writes it to a private data directory only your app can access). Then for reading it, just read it on the onResume of any Activity that needs it to get the current value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.