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

Putting content in order

Hey guys, I'm doing the new simple app project in Android. In the project you learn how to construct content in a randomized order. But what if you want the content to go in order, what do you do?

3 Answers

You could accomplish this by iterating through the array one-by-one, each time the button is pressed.

This may not be the best way to do this, but the following code does what you are asking:
(I will iterate through the mFacts array as an example.)

protected int mIndex = 1;  //declare and initialize an int variable to store the value of your index
//Side note: this needs to be 1, since the first fact is already displayed when you start the app
//You want to continue by calling the second fact in the mFacts array, which can be accessed with an index of 1.

public String getFact() {  //modify your getFact() method with the following code
    String fact = "";
    if (mIndex < mFacts.length) {  //make sure the index is still within the array
        fact = mFacts[mIndex];  //set fact equal to the fact at mIndex
        mIndex++;  //increase mIndex by one, so that next time it is called, it will access the next fact in the array
    }
    else {  //when mIndex goes past the index of the last fact in the array
        mIndex = 0;  //set mIndex back to 0
        fact = mFacts[mIndex];  //once again, set fact equal to the fact at mIndex (in this case, the first fact)
        mIndex++;
    }
    return fact;
}

Thanks and do you know how to make a back button on the app so each time you press that button, it goes back to the fact.

To make a back button that would go back by one each time it is pressed, you would need to add a button in activity_fun_facts.xml (I gave mine an id of backButton), and then declare it in FunFactsActivity.java inside the onCreate method as follows:

final Button backButton = (Button) findViewById(R.id.backButton);

Below is the current OnClickListener in the onCreate method. If you also want the back button's text color to be the same as the background color each time the background color changes, you will need to modify this code slightly (see the comment).

  View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String fact = mFactBook.getFact();
            // Update the label with our dynamic fact
            factLabel.setText(fact);

            int color = mColorWheel.getColor();
            relativeLayout.setBackgroundColor(color);
            showFactButton.setTextColor(color);
            backButton.setTextColor(color);  //ADD THIS LINE HERE
        }
    };
    showFactButton.setOnClickListener(listener);

After the above OnClickListener, you would need to add a new OnClickListener to "listen" for the back button being pressed.

  View.OnClickListener backListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String fact = mFactBook.getPreviousFact(); //we will need to create a new method in FactBook.java called getPreviousFact()
            factLabel.setText(fact);

            int color = mColorWheel.getColor();
            relativeLayout.setBackgroundColor(color);
            backButton.setTextColor(color);
            showFactButton.setTextColor(color);
        }
    };
    backButton.setOnClickListener(backListener);

To get this to work, I also had to modify the code that I wrote in my previous response (in FactBook.java):

protected int mIndex = 0; //I changed this back to 0, because we are going to increase it by one before we do anything with it

Your getFact() method should be changed to this:

public String getFact() {
    String fact = "";
    mIndex++;
    if (mIndex < mFacts.length) {
        fact = mFacts[mIndex];
    }
    else {
        mIndex = 0;
        fact = mFacts[mIndex];
    }
    return fact;
}

Add a getPreviousFact() method:

public String getPreviousFact() {
    String fact = "";
    mIndex--;
    if (mIndex >= 0) {
        fact = mFacts[mIndex];
    }
    else {
        mIndex = mFacts.length - 1;
        fact = mFacts[mIndex];
    }
    return fact;
}

Let me know if you have any questions about the above code. Hope this helps!

Thanks I really appreciate the help, now I just have a few more questions. How do I put in an area where I would make a question and there would be a small white box for the user to put input? Also do you know how to make games on android studio?

I don't think I'm the best person to answer those questions, as I've only just started working with Android apps myself. Try making a new post on the forum so that others can help you out. Good luck!

Hey what does the mIndex++; mean?

It increases mIndex by one. It's the shorthand way of writing mIndex += 1 or mIndex = mIndex + 1.

ok and what does mIndex < mFacts.length mean?

It's used as the condition inside the if statement. It's checking to see if mIndex is less than the length of the mFacts array to make sure the index is still valid. Once the index is greater than or equal to the length of mFacts, then we would be trying to access an element at an index that is not within the array, and it would result in an Array Index Out of Bounds Exception.