Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yongkangc/46ae29fdd27aba881e8a0d7629268acc to your computer and use it in GitHub Desktop.
Save yongkangc/46ae29fdd27aba881e8a0d7629268acc to your computer and use it in GitHub Desktop.
Easiest On click method
public class AwesomeButtonActivity extends AppCompatActivity {
private Button awesomeButton;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
awesomeButton = new Button(this);
awesomeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
awesomeButtonClicked();
}
});
}
private void awesomeButtonClicked() {
awesomeButton.setText("AWESOME!");
}
}
@yongkangc
Copy link
Author

One reason why I avoid this is because it clutters up your onCreate method. This becomes even more apparent when you want to observe click events from multiple views. Of course retrolambda or simply using Kotlin would make it possible to take this from 5 lines down to a single line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment