Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Choice 2 click event

When we're on the last page then isFinalPage() will return true which will execute the first block of code which will hide the choiceButton1 and set the text for the second button. So When we click on choice2 when the final page is true how the click listener method for choiceButton2 inside of loadButtons() executes.

    if(page.isFinalPage()){
            choiceButton1.setVisibility(View.INVISIBLE);
            choiceButton2.setText(R.string.button_description_default);
        }else {
            loadButtons(page);
        }
private void loadButtons(final Page page) {
        choiceButton1.setVisibility(View.VISIBLE);
        choiceButton1.setText(page.getChoice1().getTextId());
        choiceButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int nextPage = page.getChoice1().getNextPage();
                loadPage(nextPage);
            }
        });

        choiceButton2.setVisibility(View.VISIBLE);
        choiceButton2.setText(page.getChoice2().getTextId());
        choiceButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int nextPage = page.getChoice2().getNextPage();
                String con =  Integer.toString(nextPage);
                loadPage(nextPage);
            }
        });
    }

1 Answer

Nikos Tzouvelekis
Nikos Tzouvelekis
8,155 Points

As you see for button 2 must set the click listener inside to isFianalPage and for button 1 inside to else ... this is my code :) :) :) i think if you continue on videos he show how to do this thing

 if(page.isFinalPage()){
            choice1Button.setVisibility(View.INVISIBLE);
            choice2Button.setText("Main menu");
            choice2Button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }else {
            choice1Button.setText(page.getChoice1().getTextID());
            choice1Button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int nextPage = page.getChoice1().getNextPage();
                    loadPage(nextPage);
                }
            });
            choice2Button.setText(page.getChoice2().getTextID());
            choice2Button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int nextPage = page.getChoice2().getNextPage();
                    loadPage(nextPage);
                }
            });