Posts

Showing posts with the label Onclicklistener

Android - SetOnClickListener Vs OnClickListener Vs View.OnClickListener

Answer : Imagine that we have 3 buttons for example public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Capture our button from layout Button button = (Button)findViewById(R.id.corky); Button button2 = (Button)findViewById(R.id.corky2); Button button3 = (Button)findViewById(R.id.corky3); // Register the onClick listener with the implementation above button.setOnClickListener(mCorkyListener); button2.setOnClickListener(mCorkyListener); button3.setOnClickListener(mCorkyListener); } // Create an anonymous implementation of OnClickListener private View.OnClickListener mCorkyListener = new View.OnClickListener() { public void onClick(View v) { // do something when the button is clicked // Yes we will handle ...

Android - Programmatically Change The State Of A Switch Without Triggering OnCheckChanged Listener

Answer : Set the listener to null before calling setCheck() function, and enable it after that, such as the following: switch.setOnCheckedChangeListener (null); switch.setChecked(true); switch.setOnCheckedChangeListener (this); Reference : Change Checkbox value without triggering onCheckChanged Well, just before doing things in code with the switch you could just unregister the Listener, then do whatever you need to, and again register the listener. Every CompoundButton (two states button - on/off) has a pressed state which is true only when a user is pressing the view . Just add a check in your listener before starting the actual logic: if(compoundButton.isPressed()) { // continue with your listener } That way, changing the checked value programmatically won't trigger the unwanted code. From @krisDrOid answer.