I have placed a switch widget in Main Activity, I also have a second activity that extends BroadcastReceiver. I want to get the boolean state of switch widget in second activity.
If I type
Switch s = (Switch) findViewById(R.id.switch1);
it says findViewById is undefined for the type SecondActivity. The problem is Android doesn't allow me to get the value of switch in a class that extends Broadcast Receiver.
I want to know the state of switch, i.e, whether switch is on or off, but in second activity. How can I achieve it?
To access the value of the switch, you need to do the following: ((Switch) findViewById(R. id. switch_id)).
The setChecked(value) method of the Switch works totally fine but you're calling it inside the onCheckedChanged(...) method which is unnecessary. So in order to set the Switch to the latest value you should load the Preferences and set the checked state outside of the setOnCheckedChangeListener listener.
Important Note: We can check the current state of a Switch programmatically by using isChecked() method. This method returns a Boolean value means true or false. If a Switch is checked then it returns true otherwise it returns false. Below is an example code in which we checked the current state of a Switch.
It is used to display checked and unchecked state of a button providing slider control to user. Switch is a subclass of CompoundButton. It is basically an off/on button which indicate the current state of Switch. It is commonly used in selecting on/off in Sound, Bluetooth, WiFi etc.
It’s drawable to be drawn to the left of the text. Following is the example of defining a two Switch controls and one Button control in RelativeLayout to get the state of Switch controls when we click on Button control in the android application. Create a new android application using android studio and give names as SwitchExample.
The Switch element is useful for the users to change the settings between two states either ON or OFF. We can add a Switch to our application layout by using Switch object. Following is the pictorial representation of using Switch in android applications. By default, the android Switch will be in the OFF ( Unchecked) state.
We can add a Switch to our application layout by using Switch object. Following is the pictorial representation of using Switch in android applications. By default, the android Switch will be in the OFF ( Unchecked) state. We can change the default state of Switch by using android:checked attribute.
Calling findViewById()
from an Activity can only access Views that are a part of the layout of that Activity. You cannot use it to search the layout of any other Activity.
Depending on your app design, you can use one of these:
1) Send the value of the Switch to SecondActivity via an Intent extra
In Activity:
Intent mIntent = new Intent(this, SecondActivity.class);
mIntent.putExtra("switch", s.isChecked());
startActivity(mIntent);
In SecondActivity:
boolean isChecked = getIntent().getBooleanExtra("switch", false);
2) Save the value to a preference on change, and read preferences in SecondActivity
In Activity:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor e = settings.edit();
e.putBoolean("switch", s.isChecked());
e.commit();
In SecondActivity:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
boolean isChecked = settings.getBoolean("switch", false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With