Advertisement
yo2man

Dynamically Changing the Background Color

Jun 21st, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. // Dynamically Changing the Background Color
  2.  
  3. FunFactsActivity.java
  4.  
  5. private FactBook mFactBook = new FactBook();  
  6.  
  7. public class FunFactsActivity extends Activity {
  8. @Override
  9.     public void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_fun_facts);
  12.  
  13.         final TextView factLabel = (TextView) findViewById(R.id.factTextView);
  14.         Button showFactButton = (Button) findViewById(R.id.showFactButton);
  15.    
  16.     RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.idOfRelativeLayout);
  17.  
  18.         View.OnClickListener listenerName = new View.onClickListener(){
  19.                
  20.                 @Override
  21.                 public void onClick(View view) {
  22.  
  23.                 String fact = mFactBook.getFact();
  24.  
  25.                 factLabel.setText(fact);
  26.         //change background color onClick:
  27.         relativeLayout.setBackgroundColor(Color.RED);
  28.                                }
  29.         };
  30.                
  31.         showFactButton.setOnClickListener(listenerName);
  32.     }
  33. }
  34.  
  35. //-----------------------------------------------------------------------------------------------------------------------------------------
  36.  
  37. FactBook.java
  38.  
  39. public class FactBook {
  40.  
  41.         public String[] mFacts = {                              
  42.                         "Ants stretch when they wake up in the morning.",                                
  43.                         "Ostriches can run faster than horses.",                                        
  44.                         "Olympic gold medals are actually made mostly of silver.",                      
  45.                         "You are born with 300 bones; by the time you are an adult you will have 206.",  
  46.                         "It takes about 8 minutes for light from the Sun to reach Earth.",              
  47.                         "Some bamboo plants can grow almost a meter in just one day.",                  
  48.                         "The state of Florida is bigger than England.",                                  
  49.                         "Some penguins can leap 2-3 meters out of the water.",                          
  50.                         "On average, it takes 66 days to form a new habit.",                            
  51.                         "Mammoths still walked the earth when the Great Pyramid was being built." };    
  52.  
  53.         public String getFact() {
  54.                
  55.                 String fact = "";
  56.  
  57.                 Random randomGenerator = new Random ();
  58.                 int randomNumber = randomGenerator.nextInt(mFacts.length);  
  59.  
  60.                 fact = mFacts[randomNumber];  
  61.                
  62.                 return fact; //return = I am done with this method, here is the result.
  63.         }
  64. }
  65.  
  66.  
  67. // https://teamtreehouse.com/library/build-a-simple-android-app-new/improving-our-code/dynamically-changing-the-background-color
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement