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 Build a Simple Android App Improving Our Code Using the New Colors

Andrew Pritykin
Andrew Pritykin
6,574 Points

Refactoring into a new method

I wanted to refactor my code more by moving the operations when the button is clicked (Change text and background color) to a method. I also call the method on startup so that the user gets a new message on any boot.

Problem is that when I go to press the button nothing happens. I do get a new fact on each app launch which is telling me that it is getting the into the method, but somehow stopping. Im not sure what im doing wrong.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_motivational);

        // Assign Views from layout to variables
        mQuoteTextView = (TextView) findViewById(R.id.quoteTextView);
        mShowQuoteButton = (Button) findViewById(R.id.showQuoteButton);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

        updateQuote();

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateQuote();
            }
        };


        mShowQuoteButton.setOnClickListener(listener);


    }

    public void updateQuote(){
        mQuoteTextView.setText(quote);
        mRelativeLayout.setBackgroundColor(color);
        mShowQuoteButton.setTextColor(color);
        return;
    }

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

While I see that you're setting the the color and text of the views, where are you changing the quote and color to set them to?

Andrew Pritykin
Andrew Pritykin
6,574 Points

Seth Kroger Wow funny how I missed this. Looks like I was assignning the color and quote values under the onCreate method as view variables rather than assigning them in the updateQutote() method

// Assign View Variables
    private TextView mQuoteTextView;
    private Button mShowQuoteButton;
    private RelativeLayout mRelativeLayout;
    String quote = mQuoteVault.getQuote();
    int color = mColorWheel.getColor();