Created
May 13, 2016 18:17
-
-
Save udacityandroid/d5826cdde2e0591c0162452f48f6be2d to your computer and use it in GitHub Desktop.
Use OnClickListeners for All Categories - onCreate method in MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}); |
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
Hello, fellow coders.
I am sharing code based on my development environment as below.
I am using Android Studio 4.2.2, Build #AI-202.7660.26.42.7486908, built on June 24, 2021, Runtime version: 11.0.8+10-b944.6842174 amd64, VM: OpenJDK 64-Bit Server VM by N/A
openjdk version "11.0.10" 2021-01-19
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.10+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.10+9, mixed mode)
The MainActivity.java code is look like this and it works for me.
Hope this is helpful for someone out there. Happy coding!
``
package com.tutorial.android.miwok;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
}