Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 25 You must be signed in to fork a gist
  • Save udacityandroid/d5826cdde2e0591c0162452f48f6be2d to your computer and use it in GitHub Desktop.
Save udacityandroid/d5826cdde2e0591c0162452f48f6be2d to your computer and use it in GitHub Desktop.
Use OnClickListeners for All Categories - onCreate method in MainActivity.java
// Find the View that shows the numbers category
TextView numbers = (TextView) findViewById(R.id.numbers);
// Set a click listener on that View
numbers.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
@Override
public void onClick(View view) {
Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);
startActivity(numbersIntent);
}
});
@Harriskobia
Copy link

Note: If you don't want to create a Intent class, you can simply call the Intent constructor INSIDE the startActivity() method, like this:

TextView numbers = findViewById(R.id.numbers);

    numbers.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            startActivity(new Intent(MainActivity.this, NumbersActivity.class));
        }
    });
}

This is actually nice

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