Advertisement
yo2man

Adding More Colors: creating new java class to add more colo

Jun 21st, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.33 KB | None | 0 0
  1. // Adding More Colors: creating a new java class to add more colors
  2.  
  3. FunFactsActivity.java
  4.  
  5. private FactBook mFactBook = new FactBook();*
  6. private ColorWheel mColorWheel = new ColorWheel();*
  7.  
  8. public class FunFactsActivity extends Activity {
  9. @Override
  10.     public void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_fun_facts);
  13.  
  14.         final TextView factLabel = (TextView) findViewById(R.id.factTextView);
  15.         Button showFactButton = (Button) findViewById(R.id.showFactButton);
  16.        
  17.         RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.idOfRelativeLayout);
  18.  
  19.         View.OnClickListener listenerName = new View.onClickListener(){
  20.                
  21.                 @Override
  22.                 public void onClick(View view) {
  23.  
  24.                 String fact = mFactBook.getFact();
  25.                 factLabel.setText(fact);
  26.  
  27.         int color = mColorWheel.getColor();
  28.                 //change background color onClick:
  29.                 relativeLayout.setBackgroundColor(color);
  30.  
  31.         //change button text color with the background color
  32.         showFactbutton.setTextColor(color);
  33.                                }
  34.         };
  35.                
  36.         showFactButton.setOnClickListener(listenerName);
  37.     }
  38. }
  39.  
  40. //-----------------------------------------------------------------------------------------------------------------------------------------
  41.  
  42. FactBook.java*
  43.  
  44. public class FactBook {
  45.  
  46.         public String[] mFacts = {                              
  47.                         "Ants stretch when they wake up in the morning.",                                
  48.                         "Ostriches can run faster than horses.",                                        
  49.                         "Olympic gold medals are actually made mostly of silver.",                      
  50.                         "You are born with 300 bones; by the time you are an adult you will have 206.",  
  51.                         "It takes about 8 minutes for light from the Sun to reach Earth.",              
  52.                         "Some bamboo plants can grow almost a meter in just one day.",                  
  53.                         "The state of Florida is bigger than England.",                                  
  54.                         "Some penguins can leap 2-3 meters out of the water.",                          
  55.                         "On average, it takes 66 days to form a new habit.",                            
  56.                         "Mammoths still walked the earth when the Great Pyramid was being built." };    
  57.  
  58.         public String getFact() {
  59.                
  60.                 String fact = "";
  61.  
  62.                 Random randomGenerator = new Random ();
  63.                 int randomNumber = randomGenerator.nextInt(mFacts.length);  
  64.  
  65.                 fact = mFacts[randomNumber];  
  66.                
  67.                 return fact; //return = I am done with this method, here is the result.
  68.         }
  69. }
  70.  
  71. //-----------------------------------------------------------------------------------------------------------------------------------------
  72.  
  73. ColorWheel.java*
  74.  
  75. //Step 1 copy over from Factbook.Java Step 2: change "FactBook" and "fact" to ColorWheel" and "Color"
  76.  
  77. public String[] mColors = {
  78.     "#39add1", // light blue
  79.     "#3079ab", // dark blue
  80.     "#c25975", // mauve
  81.     "#e15258", // red
  82.     "#f9845b", // orange
  83.     "#838cc7", // lavender
  84.     "#7d669e", // purple
  85.     "#53bbb4", // aqua
  86.     "#51b46d", // green
  87.     "#e0ab18", // mustard
  88.     "#637a91", // dark gray
  89.     "#f092b0", // pink
  90.     "#b7c0c7"  // light gray
  91. };
  92.  
  93.         public String[] mColors = {                              
  94.  
  95.         public int getColor() {
  96.                
  97.                 String color = "";
  98.  
  99.                 Random randomGenerator = new Random ();
  100.                 int randomNumber = randomGenerator.nextInt(mColors.length);  
  101.  
  102.                 color = mColors[randomNumber];  
  103.         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.
  104.                
  105.                 return ColorAsInt; //return = I am done with this method, here is the result.
  106.         }
  107. }
  108.  
  109. // 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