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

Trying to add a previous button to the project but it isn't working. Just asking to be pointed in the right direction

Code for Buttons

public class FunFactsActivity extends AppCompatActivity implements OnClickListener {
    //Declare our view variables
    private TextView factTextView;
    public static final String TAG = FunFactsActivity.class.getSimpleName();
    private ColorWheel colorWheel = new ColorWheel();
    private Button showFactButton;
    private Button previousFactButton;
    private FactBook factBook = new FactBook();
    private RelativeLayout relativeLayout;
    public static final List<Integer> factNumbers = new ArrayList<Integer>();
    public static final List<Integer> previousColors = new ArrayList<Integer>();

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

        //Assign the Views from the layout file to corresponding variables
        factTextView = findViewById(R.id.factTextView);
        relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
        showFactButton = findViewById(R.id.showFactButton);
        showFactButton.setOnClickListener(this);
        previousFactButton = findViewById(R.id.previousFactButton);
        previousFactButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.showFactButton:
                String fact = factBook.getFact();
                //update the screen with our new fact
                factTextView.setText(fact);

                int color = colorWheel.getColor();
                relativeLayout.setBackgroundColor(color);
                showFactButton.setTextColor(color);
                break;
            case R.id.previousFactButton:
                fact = factBook.getPreviousFact();
                factTextView.setText(fact);

                color = colorWheel.getPreviousColor();
                relativeLayout.setBackgroundColor(color);
                previousFactButton.setTextColor(color);
                break;
            default:
                break;
        }
    }
}

Code for Facts

class FactBook {
    //Fields or Member Variables - Properties about the object
    private final String[] facts = {
            "Ants stretch when they wake up in the morning.",
            "Ostriches can run faster than horses.",
            "Olympic gold medals are actually made mostly of silver.",
            "You are born with 300 bones; by the time you are an adult you will have 206.",
            "It takes 8 minutes and 20 seconds for light from the Sun to reach Earth.",
            "Some bamboo plants can grow almost a meter in just one day.",
            "The state of Florida is bigger than England.",
            "Some penguins can leap 2-3 meters out of the water.",
            "On average, it takes 66 days to form a new habit.",
            "Mammoths still walked the earth when the Great Pyramid was being built." };


    // Methods - Actions the object can take


    String getFact() {

        //Randomly select a fact
            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(facts.length);
            factNumbers.add(randomNumber);
            return facts[randomNumber];

        }
    String getPreviousFact(){
        //select the previous fact
     int n = factNumbers.size();
        n -= 1;
        int x = factNumbers.get(n);
        return facts[x];
    }
}

Code for Colors

public class ColorWheel {

    //Fields or Member Variables - Properties about the object
    private final String[] colors = {
            "#39add1", // light blue
            "#3079ab", // dark blue
            "#c25975", // mauve
            "#e15258", // red
            "#f9845b", // orange
            "#838cc7", // lavender
            "#7d669e", // purple
            "#53bbb4", // aqua
            "#51b46d", // green
            "#e0ab18", // mustard
            "#637a91", // dark gray
            "#f092b0", // pink
            "#b7c0c7", // light gray
            "#03c4eb"  // DanzerPress
    };


    // Methods - Actions the object can take

   int getColor() {

        //Randomly select a fact
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(colors.length);
        int color = Color.parseColor(colors[randomNumber]);
        previousColors.add(color);
        return color;
    }

    int getPreviousColor(){
       //get previous fact color scheme
        int n = previousColors.size();
        n -= 1;
        int prevColor = previousColors.get(n);
        return prevColor;
    }

}

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

You are on the right track with storing the previous color and fact, but I think you would do better to store each in their respective classes than in the Activity.