-
-
Save udacityandroid/d5826cdde2e0591c0162452f48f6be2d to your computer and use it in GitHub Desktop.
// 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); | |
} | |
}); |
I want to share my version of the code:
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set the content of the activity to use the activity_main.xml layout file setContentView(R.layout.activity_main); //Number Listener setListenerOpenActivity(findViewById(R.id.numbers),NumbersActivity.class); setListenerOpenActivity(findViewById(R.id.colors),ColorsActivity.class); setListenerOpenActivity(findViewById(R.id.phrases),PhrasesActivity.class); setListenerOpenActivity(findViewById(R.id.family),FamilyActivity.class); } public void setListenerOpenActivity (View view, final Class cActivity){ view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(MainActivity.this,cActivity)); } }); }
}
Why do we pass (MainActviity,this, NumbersActivity,class) instead of (this, NumbersActivity.class)
Because we are in the Anonymous inner class. Class inside other class.
put this code into the onCreate method
Question : do we have to copy past the code for all 4 categories ? or is it possible to just build one onclicklistener class and then use it on the 4 different textView objects ?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
TextView numbers= (TextView)findViewById(R.id.numbers);
TextView phrases = (TextView)findViewById(R.id.phrases);
TextView family = (TextView)findViewById(R.id.family);
TextView colors = (TextView)findViewById(R.id.colors);
numbers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent Numb = new Intent(MainActivity.this, numbersActivity.class);
startActivity(Numb);
}
});
phrases.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent phrase = new Intent(MainActivity.this, phrasesActivity.class);
startActivity(phrase);
}
});
family.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent fam = new Intent(MainActivity.this, familyMembersActivity.class);
startActivity(fam);
}
});
colors.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent col = new Intent(MainActivity.this, colorsActivity.class);
startActivity(col);
}
});
}
}
Here's my code, but I don't like the too much repetition :/ is there like an IF statement or a switch to use to implement this in less lines?
setOnClickListener
same with me
Please check if you've pasted this code within the onCreate method.
Thanks
is anyone lost like me? :(
yes, i don't understand a single word of the video
The first Parameter you are passing is the context of current activity because in case you want to press back after going to the next activity it'll act as a link and the next one is pretty self explanatory (it is passing the class you want to go to after clicking on the view).
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView numbers = findViewById(R.id.numbers);
numbers.setOnClickListener(view -> {
Intent numbersIntent = new Intent(this, NumbersActivity.class);
startActivity(numbersIntent);
});
TextView family = findViewById(R.id.family);
family.setOnClickListener(view -> {
Intent familyIntent = new Intent(this, FamilyActivity.class);
startActivity(familyIntent);
});
TextView colors = findViewById(R.id.colors);
colors.setOnClickListener(view -> {
Intent colorsIntent = new Intent(this, ColorsActivity.class);
startActivity(colorsIntent);
});
TextView phrases = findViewById(R.id.phrases);
phrases.setOnClickListener(view -> {
Intent phrasesIntent = new Intent(this, PhrasesActivity.class);
startActivity(phrasesIntent);
});
}
}
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
harshavardhan3199
Me too bro