Reputation: 3
I have a randomJoke code in my MainActivity class basically telling my app to display a string everytime a button is clicked. Within the joke view I'm trying to create a function to do the same thing, except with a button WITHIN the joke view. So the user doesn't have to go back to the main page when they want another joke. Instead, they can just click a button on the jokes view. What should I add after "onClicK" to tell the app to do the same function as my randomJoke code (which is in the same activity class). Thanks!!
My code so far is this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
setupJokeButton();
}
private void setupJokeButton() {
// TODO Auto-generated method stub
Button JokeButton = (Button) findViewById(R.id.nextjoke);
JokeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
My randomJoke code looks like this:
public void randomJoke (View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
Random rand = new Random();
int lowerBound = 1, upperBound = 83; //HERE IS WHERE YOU ADD THE HIGHEST NUMBER OF JOKES THERE IS
int randomNumber = rand.nextInt(upperBound - lowerBound + 1) + lowerBound;
String jokeNumber = "joke" + String.valueOf(randomNumber);
String mess = getResources().getString(getStringResourcePath(getApplicationContext(),jokeNumber));
//String jokeNumber = "joke" + String.valueOf(iteration);
//iteration += 1;
//String mess = getResources().getString(getStringResourcePath(getApplicationContext(), jokeNumber));
intent.putExtra(EXTRA_MESSAGE, mess);
startActivity(intent);
Upvotes: 0
Views: 81
Reputation: 44571
If I understand what you want, you could do this
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
randomJoke(v);
Since randomJoke()
just takes a View
as its parameter you just call the method and send the View
or Button
that was clicked.
Upvotes: 1