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

Button Text and Booleans (Simple Android App)

I'm build the Fun Facts app on the Android Development Track. I decided to take a exploratory detour and try to create a very basic introductory message to the user. I changed the factTextView text to "You can click the button below to see a new fact!" and changed the showFactButton text to "Try it out!"

From there, I changed the final line onClick object (is that an object?) to the following:

        public void onClick(View view) {
            String fact = mFactBook.getFact();
            // Update the label with our dynamic fact
            factLabel.setText(fact);
            // Set button text to new fact prompt
            showFactButton.setText("Show another fun fact.");

This seems to work fine. However, I feel like "updating" the button text to the same new string on every press isn't always the best practice, even if it is easy and readable. I tried to add a boolean that will check the text of the button, and update it only if it has not already been updated. This is what I've come up with so far:

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public String launchText = getResources().getString(R.string.start_text);
        public String nextText = getResources().getString(R.string.next_text);
        public String buttonText = (String) showFactButton.getText();
        public boolean updateLaunchText() {
            if (buttonText.equals(launchText)) {
                buttonText.replaceAll(launchText, nextText);
                return true;
            }
            else {
                return true;
            }
        }
        public void onClick(View view) {
            String fact = mFactBook.getFact();
            // Update the label with our dynamic fact
            factLabel.setText(fact);
        }
    };

With the following added to strings.xml:

<string name="start_text">Try it out!</string>
<string name="next_text">Show another Fun Fact!</string>

No errors, but the button text stays on "Try it out!" I'm sure that all the extra objects are totally unnecessary compared to the first, working method for the scope of this app, but I'd still like to figure it out since I don't really have any idea what I'm doing with the boolean.

Questions: 1) What am I missing in the longer boolean approach? 2) What's the actual most efficient approach to accomplish this task?

3 Answers

Hi Christopher

I think the biggest problem is where your code is located. You have put your code inside the method updateLauncherText() of the onClickListener class when it is only the onClick() method of the onClickListener which is called so your code never gets run.

Secondly the return true; line inside the if statement would end your method there which you don't want to do so remove these.

Lastly I would use the method showFactbutton.setText(nextText); instead of the replaceAll() method as replacing the text is unnecessary as you only want the new text displayed.

I have edited the code below to reflect these changes, I haven't tested so please let me know if it doesn't work and we can try and work it out.

View.OnClickListener listener = new View.OnClickListener() {
        @Override

        public void onClick(View view) {

        public String launchText = getResources().getString(R.string.start_text);
        public String nextText = getResources().getString(R.string.next_text);
        public String buttonText = (String) showFactButton.getText();


         if (buttonText.equals(launchText)) {
                showFactButton.setText(nextText);

            }


            String fact = mFactBook.getFact();
            // Update the label with our dynamic fact
            factLabel.setText(fact);
        }
    }

Hope this helps

Daniel

Thanks Daniel, I appreciate the help. I'll try out your fixes once I'm back at my home PC and update with the results.

Tried out Daniel's suggestion, worked out beautifully. Android Studio returned error alerts on public modifier to the strings, so I removed those to be sure and it performed great.