Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

attach onClickListener to ToggleButton

I have a ToggleButton and I need to set up simple click actions. How do I implement a simple click listener for a ToggleButton?

If you need details please ask.

like image 316
nkcmr Avatar asked Aug 21 '11 22:08

nkcmr


People also ask

How many ways can you attach OnClickListener to button layout?

Using an OnClickListener There are two ways to do this event handler programmatically : Implementing View. OnClickListener in your Activity or fragment. Creating new anonymous View.

Can we set OnClickListener on TextView?

In Android, TextView is a child class of View, and hence can use the method setOnClickListener() on the object of TextView. In this tutorial, we will learn how to set OnClickListener for TextView in Kotlin file, with the help of an example.

How do I use OnClickListener?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.


2 Answers

    this.someToggleButton = (ToggleButton)findViewById(R.id.someToggleButton) ;     this.someToggleButton.setOnCheckedChangeListener( new OnCheckedChangeListener() {         @Override         public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {             doSomethingWith(toggleButton, isChecked) ;         }     }) ; 
like image 54
Keith John Hutchison Avatar answered Oct 22 '22 20:10

Keith John Hutchison


ToggleButton extends View, so you can simply use View.setOnClickListener(), like this:

// get your ToggleButton ToggleButton b = (ToggleButton) findViewById(R.id.myButton);  // attach an OnClickListener b.setOnClickListener(new OnClickListener() {     @Override     public void onClick(View v)     {         // your click actions go here     } }); 
like image 28
Jacob Ras Avatar answered Oct 22 '22 20:10

Jacob Ras