How do I disable the toggle button on Android?

Arun Badole picture Arun Badole · Jun 8, 2011 · Viewed 23.8k times · Source

I am using three toggle buttons if I ON first button so all 3 Button should get disabled. Although I have written code but I am not getting, why it is not working. My Code is as follows..

import android.app.Activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;


import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.parser.XMLData;
import com.parser.XMLDataParser;

public class SyncDataPage extends Activity
{
    ToggleButton toggle_syncing;
    ToggleButton toggle_replace_data;
    ToggleButton toggle_replace_photos;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sync_data_page);

        initializeToggleButtons();
    }


    private void initializeToggleButtons()
    {
        toggle_syncing = (ToggleButton) findViewById(R.id.sync_data_toggle_syncing);
        toggle_replace_data = (ToggleButton) findViewById(R.id.sync_data_toggle_data_in_contacts);
        toggle_replace_photos = (ToggleButton) findViewById(R.id.sync_data_toggle_photos_in_contacts);
        toggle_syncing.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(toggle_syncing.isChecked())
                {
                    // Toast.makeText(getApplicationContext(), "Syncing Starts", Toast.LENGTH_SHORT).show();
                    toggle_syncing.setFocusable(false);
                    toggle_replace_data.setFocusable(false);
                    toggle_replace_photos.setFocusable(false);
                    btn_back.setFocusable(false);
                    bar_syncing.setVisibility(LinearLayout.VISIBLE);
                    new ContactsDataSync().execute();
                }
                else
                {
                    // Toast.makeText(getApplicationContext(), "toggle_syncing Unchecked", Toast.LENGTH_SHORT).show();
                }
            }
        });


        toggle_replace_data.setOnClickListener(new OnClickListener()
        {
          @Override
          public void onClick(View v)
          {
              if(toggle_replace_data.isChecked())
              {
                  // Toast.makeText(getApplicationContext(), "toggle_replace_data Checked", Toast.LENGTH_SHORT).show();
              }
              else
              {
                  // Toast.makeText(getApplicationContext(), "toggle_replace_data Unchecked", Toast.LENGTH_SHORT).show();
              }
          }
        });


        toggle_replace_photos.setOnClickListener(new OnClickListener()
        {
          @Override
          public void onClick(View v)
          {
              if(toggle_replace_photos.isChecked())
              {
                  // Toast.makeText(getApplicationContext(), "toggle_replace_photos Checked", Toast.LENGTH_SHORT).show();
              }
              else
              {
                  // Toast.makeText(getApplicationContext(), "toggle_replace_photos Unchecked", Toast.LENGTH_SHORT).show();
              }
          }
        });
    }

Answer

Gabriel Negut picture Gabriel Negut · Jun 8, 2011

Use setEnabled(false) instead of setFocusable(false).