Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get switch value in Android?

Tags:

android

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?

like image 437
gegobyte Avatar asked Oct 31 '14 13:10

gegobyte


People also ask

How do you find the value of Switch?

To access the value of the switch, you need to do the following: ((Switch) findViewById(R. id. switch_id)).

How to set Switch value in Android?

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.

How to check Switch Android Studio?

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.

What is the use of the switch button in Android?

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.

How to get the state of switch controls in Android application?

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.

How to use switch element in Android apps?

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.

How to add a switch to an Android application layout?

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.


1 Answers

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);
like image 70
JstnPwll Avatar answered Sep 17 '22 15:09

JstnPwll