Toggle Button is not working properly on password dialog

  • Replies:0
Himanshu Sharma
  • Forum posts: 1

May 15, 2015, 1:56:48 PM via Website

Hi Sir/Ma'am,
I'm trying to Enable/Disable Bluetooth on Toggle Button and Also, there is a additional functionality and that is asking password on enable/disable. Toggle Button is taking Default state of Bluetooth at launch(Means if Bluetooth is Enable, then toggle will be set on and if Bluetooth is Disable, toggle will be off).

Also, it is working good on Enter Correct Password. But, Main Problem is When I enter Wrong Password , Toggle is not behave as it should.

What i mean, Suppose Bluetooth is On and I am trying to disable it by my app. When i click on Toggle, it ask me to enter password. Now, If i enter wrong password, Bluetooth doesn't turn off, but toggle button change it's state to Turn Off.

Here is What i tried so far:-

setContentView(R.layout.activity_main);
mb = BluetoothAdapter.getDefaultAdapter();
tb1 = (ToggleButton)findViewById(R.id.appToggleBtn);

    tb1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Check();
        }
    });
}


   public void Check()
{
    final Dialog dialog = new Dialog(MainActivity.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog);
    dialog.setCancelable(true);

    dialog.show();

    ImageView saveBtn=(ImageView)dialog.findViewById(R.id.confirmdialogBtn);
    final EditText password=(EditText)dialog.findViewById(R.id.confirmdialogEditText);

     saveBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            if(password.getText().toString().equals("asd"))
             {
                if(mb.isEnabled())
                {
                    mb.disable();
                    tb1.setChecked(mb.isEnabled());
                }
                else
                {
                    mb.enable();
                    tb1.setChecked(mb.isEnabled());
                }
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Wrong", 1000).show();  
                if(mb.isEnabled())
                {
                    tb1.setChecked(mb.isEnabled());
                }
                else
                {
                    tb1.setChecked(mb.isEnabled());
                }
            }
            dialog.dismiss();
        }

    });

}

Also, for Toggle Button, i used:-
android:background="@drawable/toggle_selector"

and in toggle_selector.xml:--
{

          <item android:drawable="@drawable/toggle_on" android:state_checked="true"/>
          <item android:drawable="@drawable/toggle_off" android:state_checked="false"/>

}

Please Please Help me into this. I'm badly stuck
Thanks in advance

Reply