Skip to content

Instantly share code, notes, and snippets.

@abhishekhugetech
Created January 2, 2019 06:31
Show Gist options
  • Save abhishekhugetech/c375ae93a3c794acb56b1455aadb7cf6 to your computer and use it in GitHub Desktop.
Save abhishekhugetech/c375ae93a3c794acb56b1455aadb7cf6 to your computer and use it in GitHub Desktop.
Click Event handing using OnClickListener Listener Interface
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ConstraintLayout constraintLayout;
private Button button1;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constraintLayout = findViewById(R.id.c);
button1 = findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = findViewById(R.id.button2);
button2.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
if (id==R.id.button1){
constraintLayout.setBackgroundColor(Color.GREEN);
}else if (id==R.id.button2){
constraintLayout.setBackgroundColor(Color.BLUE);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment