Advertisement
yo2man

treehouse android dev lesson notes

Jun 19th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.89 KB | None | 0 0
  1. //TreeHouse Java notes
  2. // What a Method is:
  3.  
  4. public Pizza orderPizza(String toppings) {
  5.  // code on how to make pizza goes here
  6. }
  7.  
  8. public datatype methodname(parameters) {  //we used String to hold textdata: toppings
  9.  // code on how to make pizza goes here
  10. }
  11.  
  12.  
  13.  
  14.  
  15. https://teamtreehouse.com/library/build-an-android-app/basic-android-programming/introduction-to-methods-and-classes
  16.  
  17. //-----------------------------------------------------------------------------------------------------------------------------------------
  18.  
  19. public class FunFactsActivity extends Activity {
  20. @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_fun_facts);
  24.  
  25.     final TextView factLabel = (TextView) findViewById(R.id.factTextView);
  26.     Button showFactButton = (Button) findViewById(R.id.showFactButton);
  27.     View.OnClickListener listenerName = new View.onClickListener(){
  28.        
  29.         @Override
  30.         public void onClick(View view) {
  31.         String fact = "";
  32.                 Random randomGenerator = new Random ();
  33.         int randomNumber = randomGenerator.nextInt(3);
  34.         fact = randomNumber + "";
  35.         factLabel.setText(fact);
  36.                        }
  37.         };
  38.                
  39.         showFactButton.setOnClickListener(listenerName);
  40.     }
  41. }
  42.  
  43.  
  44. //-----------------------------------------------------------------------------------------------------------------------------------------
  45.  
  46.  
  47.  
  48.  
  49. @Override
  50.     public void onCreate(Bundle savedInstanceState) {
  51.         super.onCreate(savedInstanceState);
  52.         setContentView(R.layout.activity_fun_facts);  //This shows how to call a method in code. The call is to the method named setContentView and the parameter passed in is this R.Layout.activity_fun_facts, which is the "key" to the layout we have been working on.
  53.  
  54.     //Declare new variable:
  55.         *final TextView factLabel = (TextView) findViewById(R.id.factTextView); * //hey android, give me that TextView we created in xml //final = this factLabel cannot be reassigned
  56.         Button showFactButton = (Button) findViewById(R.id.showFactButton);   //hey android, give me that Button we created in xml
  57.     //translation: // DataTypeOfVariable giveName = (DataTypeofVariable <-casting) findViewById(R.id.idOfTheVariableWeCreated);
  58.  
  59.     View.OnClickListener listenerName = new View.onClickListener(){ //the "View" version of "OnClickListener
  60.         @Override
  61.         public void onClick(View view) {
  62.             // The button was clicked so update the fact label with a new fact
  63.             String fact = ""; //use String because the fun facts are Strings
  64.             // Randomly select a fact
  65.             Random randomGenerator = new Random (); // Constructor: construct a new Random number generator // use a Random Object. Java is Object orientated language // hint: Attributes vs Scripts //what we've just done is called instantiating our object because we're making a new instance of the random class by using that new keyword that we used previously to create a new OnClickListener.
  66.             int randomNumber = randomGenerator.nextInt(3); // declare a variable to hold the integer number generated by the random generator // Data types: primitive vs Objects = int vs String, float vs Random, char vs TextView
  67.             // "(3)" limit random number generator to just 3 choices
  68.             fact = randomNumber + ""; // Convert the randomNumber integer to a String value, then store it in the "fact" variable. "Moosh" them together
  69.  
  70.             // Update the label with our dynamic fact.
  71.             factLabel.setText(fact); //CharSequence is a generic version of String, but usually in Android, wherever you see CharSequence, you can just pass in a string. //our String was named "fact" so we put "fact" in the ()
  72.  
  73. //"final"* will be added in front. //This is important because our onClick method must refer to the same view as long as it exist. We can't make it update a different text view by reassigning factLabel to something else.
  74.  
  75.         }
  76.     };
  77.        
  78.     showFactButton.setOnClickListener(listenerName); //listen for button press on the "showFactButton"
  79.     }
  80.  
  81.  
  82.  
  83. /*
  84. void = doesn't return any kind of visual result
  85.  
  86. Relevant Note: The onCreate method is the type of method that performs an action but, doesn't return any kind of result. That is indicated by the void keyword here.
  87.  
  88. * now is line 14, setContentView(R.Layout.activity_fun_facts);
  89. ^This shows how to call a method in code. The call is to the method named setContentView and the parameter passed in is this R.Layout.activity_fun_facts, which is a key to the layout we've been working on.
  90. So what this method does is it tells this activity to use activity_fun_facts.XML layout, as the layout for the entire screen.
  91.  
  92. The purple stuff like CONTENT_ITEM_TYPE basically all refer to predetermined stuff hidden inside R.java file
  93. ProjectView>App>Build>Generated>Source>r>com.example...>R.java
  94. */
  95.  
  96. /*
  97. what is a constructor: http://www.homeandlearn.co.uk/java/class_constructor.html
  98. */
  99.  
  100.  
  101. https://teamtreehouse.com/library/build-an-android-app/basic-android-programming/accessing-views-in-code
  102.  
  103. //-----------------------------------------------------------------------------------------------------------------------------------------
  104.  
  105. /*
  106. Java is an OOP Language
  107. This is a different approach than just writing a script of commands for the computer to follow. We can think of it like this. The script approach is like watching an actor read lines from a screenplay. Whereas the object-oriented approach is like controlling a character in
  108. a game who can interact with other characters or objects.
  109. I said that an object has its own properties and abilities, so in this game character example, if the character is considered an object,
  110. then its properties might be its name and whether it's male or female, and its abilities could be things like walking and jumping.
  111. */
  112.  
  113.  
  114. /*
  115.  
  116. //-----------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement