title: change the state of toggle on correct password and dismiss the dialog
body: <p>In my application,I need to off the toggle if password is correct else it should be on always.
  I am showing a dialog box for asking password on setOnCheckedChangeListener,
  and change the state of toggle if  password is correct.</p>
  
  <p>My problem is 
  as soon as the toggle changes its state it shows the dialog again.
  dialog should dismiss if the password is correct ,same time the state of toggle should change to off.
  How to solve this.</p>
  
  <p>My code:</p>
  
  <pre><code>    onOffSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {          @Override           public void onCheckedChanged(CompoundButton buttonView,
                          boolean isChecked)              
  {
                                   if(isCorrectPin)
                  {
                      onOffSwitch.setChecked(false);
                  }
  
                      Log.v("Switch State=", ""+isChecked);
                      if(isChecked)
                      {  
                          //tv_switch_status.setText("");
  
                      }
                      else
                      {
  
                          displayPinAlert();
  
                      }           }       
  
              });
  
      private void displayPinAlert()
      {   
      onOffSwitch.setChecked(true);
  
  
          AlertDialog.Builder alert = new AlertDialog.Builder(activity.getParent());
  
  
          alert.setMessage("Enter Pin");
  
          // Set an EditText view to get user input   
         final EditText input = new EditText(activity.getParent());   
         alert.setView(input);
  
          alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  
              public void onClick(DialogInterface dialog, int whichButton)    
                   {          
                   String value = input.getText().toString();
                              //              
  
                    onOffSwitch.setChecked(false);            
                    // Do something with value!            
                    if(value.equals("1234"))          {
  
                      dialog.dismiss();
                      //onOffSwitch.setChecked(false);
                                  isCorrectPin =true;
  
                  }
                      }   });
  
          alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {       public void onClick(DialogInterface dialog, int whichButton) {          // Canceled.        }   });
  
          alert.show();
  
  
      }
  </code></pre>