Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App Improving Our Code Using the New Colors

I want to implement the ColorWheel without random function. Can someone help me?

I want to implement the ColorWheel without random function. Can someone help me?

2 Answers

Ezekiel dela Peña
Ezekiel dela Peña
6,231 Points

Hi! there are many ways you can do this. But here is an example. If you wanted to have a fixed color for each fact.

Here is just some idea on how it will turn out

In my code mColor[] from Color wheel and mFacts[] in FactBook is 1:1

example: if I get fact "Olympic gold medals are actually made mostly of silver." which is on index[2] of mFacts. the color I should get should be index[2] of mColor[] which is "#c25975", // mauve

Main Code:

package com.zeke.funfacts;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class FunFactsActivity extends AppCompatActivity {
    // Declare view var
    private FactBook mFactBookfact = new FactBook();
    private ColorWheel mColorWheel = new ColorWheel();
    private TextView mFactTxtView;
    private Button mShowFactBtn;
    private RelativeLayout mRelativeLayout;

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

        // Assign views from the layout file to the corresponding variable
        mFactTxtView = (TextView) findViewById(R.id.factTextView);
        mShowFactBtn = (Button) findViewById(R.id.showFactButton);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

        mShowFactBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               //randomNum gets the random number used 
                int randomNum = mFactBookfact.returnRandomNum();
                String fact = mFactBookfact.getFact(randomNum);
                int color = mColorWheel.getColor(randomNum);

                mRelativeLayout.setBackgroundColor(color);

                // Update the screen with a random fact
                mFactTxtView.setText(fact);
            }
        });
    }
}

FactBook

package com.zeke.funfacts;

import java.util.Random;

public class FactBook {



    private String[] mFacts = {
            "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 about 8 minutes 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." };


    public String getFact(int randomNum) {
        // Randomly select a fact
        String fact = mFacts[randomNum];

        return fact;
    }

//this function returns randomNum use to get the fact
    public int returnRandomNum() {
        return  new Random().nextInt(mFacts.length);
    }
}

Color Wheel:

package com.zeke.funfacts;

import android.graphics.Color;

import java.util.Random;

public class ColorWheel {

    private String[] mColor = {
            "#39add1", // light blue
            "#3079ab", // dark blue
            "#c25975", // mauve
            "#e15258", // red
            "#f9845b", // orange
            "#838cc7", // lavender
            "#7d669e", // purple
            "#53bbb4", // aqua
            "#51b46d", // green
            "#e0ab18", // mustard
           /* commenting this
            "#637a91", // dark gray
            "#f092b0", // pink
            "#b7c0c7"  // light gray
*/
    };

    public int getColor(int randomNum) {
        // Randomly select a color
        String color = mColor[randomNum];
        int colorAsInt = Color.parseColor(color);

        return colorAsInt;
    }
}

Thank you very much, for your response.