title: how to get switch button value and stored in database?
body: <p>i am making a registration page where subscribe to sms option is given with switch toggle button ..if customer switch that button the value (true or false) should be saved in a database..i am unable to find the solution</p>
  
  <p>here is my Registration.class:</p>
  
  <pre><code>public class RegistrationForm extends AppCompatActivity {
  
      EditText fn,ln,mb,em,pw,cpw,dob,gen;
      Switch sw;
      RadioGroup male,feml;
      Switch swth;
      private ProgressDialog pDialog;
      String status="";
  
  
      public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
              "[a-zA-Z0-9+._%-+]{1,256}" +
                      "@" +
                      "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" +
                      "(" +
                      "." +
                      "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" +
                      ")+"
      );
  
  
      private static String url_create_book = "http://cloud....com/broccoli/creatinfo.php";
  
      // JSON Node names
      // JSON Node names
      private static final String TAG_SUCCESS = "success";
  
  
  String rval;
  
      JSONParser jsonParser = new JSONParser();
      private int serverResponseCode = 0;
      Context c;
      int i=0;
  
  
      Button sub;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_registration_form);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
  
          fn=(EditText)findViewById(R.id.fnm) ;
          ln=(EditText)findViewById(R.id.lnm) ;
          mb=(EditText)findViewById(R.id.mobile) ;
  
  
  
  
          pw=(EditText)findViewById(R.id.pass) ;
          cpw=(EditText)findViewById(R.id.cpass) ;
  
          dob=(EditText)findViewById(R.id.dob);
  
         dob.setOnClickListener(new View.OnClickListener(){
                                    @Override
                                    public void onClick(View v) {
                                        int mYear, mMonth, mDay;
                                        final Calendar c = Calendar.getInstance();
                                        mYear = c.get(Calendar.YEAR);
                                        mMonth = c.get(Calendar.MONTH);
                                        mDay = c.get(Calendar.DAY_OF_MONTH);
                                        DatePickerDialog datePickerDialog = new DatePickerDialog(RegistrationForm.this,R.style.datepicker, new DatePickerDialog.OnDateSetListener() {
                                            @Override
                                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                                dob.setText(year + "/" + (month + 1) + "/" + dayOfMonth);
                                               // dob.setText(dayOfMonth + "/" + (month + 1) + "/" + );
                                            }
                                        }, mYear, mMonth, mDay);
                                        //forsetting minimum date for selection
                                       // datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
                                        datePickerDialog.show();
                                    }
                                });
  
  
         // RadioButton male=(RadioButton)findViewById(R.id.rgm) ;
  
         // RadioButton feml=(RadioButton)findViewById(R.id.rgf) ;
  
          Switch swth=(Switch)findViewById(R.id.mySwitch) ;
          String status="false";
  
  //////set the switch to ON
          swth.setChecked(false);
  
  //////attach a listener to check for changes in state
          swth.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  
                  if(isChecked){
                      status="true"; //edit here
                  }else{
                      status="false";
                  }
  
              }
          });
  
  
          RadioGroup rgrp=(RadioGroup)findViewById(R.id.rg);
  
          RadioButton radioButton;
  
  
  
  
  
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          sub=(Button)findViewById(R.id.sub2);
  
  
  
  
          sub.setOnClickListener(new View.OnClickListener() {
  
  
  
              @Override
              public void onClick(View v) {
                  RadioGroup rgrp = (RadioGroup) findViewById(R.id.rg);
                  em = (EditText) findViewById(R.id.email);
  
                  RadioButton radioButton;
  
                  int selectedId = rgrp.getCheckedRadioButtonId();
  
                  // find the radiobutton by returned id
                  radioButton = (RadioButton) findViewById(selectedId);
                  rval = radioButton.getText().toString();
                 // Toast.makeText(RegistrationForm.this, rval, Toast.LENGTH_SHORT).show();
  
  
                  String email = em.getText().toString();
  
  
                  if(checkEmail(email))
                      Toast.makeText(RegistrationForm.this,"Valid Email Addresss", Toast.LENGTH_SHORT).show();
                  else
                      Toast.makeText(RegistrationForm.this,"Invalid Email Addresss", Toast.LENGTH_SHORT).show();
                  new CreateNewProduct().execute();
  
  
  
                  // startActivity(new Intent(RegistrationForm.this, Home.class));
  
  
              }
              private boolean checkEmail(String email) {
                  return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
              }
          });
  
  
      }
  
  
  
  
  
  
  
      class CreateNewProduct extends AsyncTask&lt;String, String, String&gt; {
          private  String fname;
          private  String lname;
          private  String email;
          private  String passwrd;
          private  String cpasswrd;
          private String dobr;
  
  
  
          /**
           * Before starting background thread Show Progress Dialog
           * */
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              pDialog = new ProgressDialog(RegistrationForm.this);
              pDialog.setMessage("Creating books..");
              pDialog.setIndeterminate(false);
              pDialog.setCancelable(true);
              pDialog.show();
              fname = fn.getText().toString();
              lname = ln.getText().toString();
              email = em.getText().toString();
              passwrd = pw.getText().toString();
              cpasswrd = cpw.getText().toString();
              dobr = dob.getText().toString();
  
              //Toast.makeText(RegistrationForm.this,
              //dobr, Toast.LENGTH_SHORT).show();
          }
  
  
  
  
          protected String doInBackground(String... args) {
  
  
              // Building Parameters
              List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
              params.add(new BasicNameValuePair("First_Name", fname));
              params.add(new BasicNameValuePair("Last_Name",lname));
              params.add(new BasicNameValuePair("email", email));
              params.add(new BasicNameValuePair("Gender", rval));
              params.add(new BasicNameValuePair("password", passwrd));
              params.add(new BasicNameValuePair("confirmPasw",cpasswrd));
              params.add(new BasicNameValuePair("DOB",dobr));
              params.add(new BasicNameValuePair("sms_subscrb",status));
  
  
  
              // getting JSON Object
              // Note that create product url accepts POST method
              JSONObject json = jsonParser.makeHttpRequest(url_create_book,
                      "POST", params);
  
              // check log cat fro response
              Log.d("Create Response", json.toString());
  
              // check for success tag
              try {
                  int success = json.getInt(TAG_SUCCESS);
  
                  if (success == 1) {
                      // successfully created product
                      Intent i = new Intent(getApplicationContext(), Login.class);
                      startActivity(i);
  
                      // closing this screen
                      finish();
                  } else {
                      // failed to create product
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
              return null;
          }
  
          /**
           * After completing background task Dismiss the progress dialog
           * **/
          protected void onPostExecute(String file_url) {
              // dismiss the dialog once done
              pDialog.dismiss();
          }
  
      }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case android.R.id.home:
                  onBackPressed();
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
  }
  </code></pre>