Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Adding More Colors: creating a new java class to add more colors
- FunFactsActivity.java
- private FactBook mFactBook = new FactBook();*
- private ColorWheel mColorWheel = new ColorWheel();*
- public class FunFactsActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_fun_facts);
- final TextView factLabel = (TextView) findViewById(R.id.factTextView);
- Button showFactButton = (Button) findViewById(R.id.showFactButton);
- RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.idOfRelativeLayout);
- View.OnClickListener listenerName = new View.onClickListener(){
- @Override
- public void onClick(View view) {
- String fact = mFactBook.getFact();
- factLabel.setText(fact);
- int color = mColorWheel.getColor();
- //change background color onClick:
- relativeLayout.setBackgroundColor(color);
- //change button text color with the background color
- showFactbutton.setTextColor(color);
- }
- };
- showFactButton.setOnClickListener(listenerName);
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------------------------
- FactBook.java*
- public class FactBook {
- public 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() {
- String fact = "";
- Random randomGenerator = new Random ();
- int randomNumber = randomGenerator.nextInt(mFacts.length);
- fact = mFacts[randomNumber];
- return fact; //return = I am done with this method, here is the result.
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------------------------
- ColorWheel.java*
- //Step 1 copy over from Factbook.Java Step 2: change "FactBook" and "fact" to ColorWheel" and "Color"
- public String[] mColors = {
- "#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
- };
- public String[] mColors = {
- public int getColor() {
- String color = "";
- Random randomGenerator = new Random ();
- int randomNumber = randomGenerator.nextInt(mColors.length);
- color = mColors[randomNumber];
- int ColorAsInt = Color.parseColor(color); //parse takes a hex decimal string for a color and converts it to interger so that android can read it.
- return ColorAsInt; //return = I am done with this method, here is the result.
- }
- }
- // https://teamtreehouse.com/library/build-a-simple-android-app-new/improving-our-code/adding-more-colors
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement