Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Dynamically Changing the Background Color
- FunFactsActivity.java
- private FactBook mFactBook = new FactBook();
- 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);
- //change background color onClick:
- relativeLayout.setBackgroundColor(Color.RED);
- }
- };
- 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.
- }
- }
- // 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