Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TreeHouse Java notes
- // What a Method is:
- public Pizza orderPizza(String toppings) {
- // code on how to make pizza goes here
- }
- public datatype methodname(parameters) { //we used String to hold textdata: toppings
- // code on how to make pizza goes here
- }
- https://teamtreehouse.com/library/build-an-android-app/basic-android-programming/introduction-to-methods-and-classes
- //-----------------------------------------------------------------------------------------------------------------------------------------
- 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);
- View.OnClickListener listenerName = new View.onClickListener(){
- @Override
- public void onClick(View view) {
- String fact = "";
- Random randomGenerator = new Random ();
- int randomNumber = randomGenerator.nextInt(3);
- fact = randomNumber + "";
- factLabel.setText(fact);
- }
- };
- showFactButton.setOnClickListener(listenerName);
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------------------------
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- 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.
- //Declare new variable:
- *final TextView factLabel = (TextView) findViewById(R.id.factTextView); * //hey android, give me that TextView we created in xml //final = this factLabel cannot be reassigned
- Button showFactButton = (Button) findViewById(R.id.showFactButton); //hey android, give me that Button we created in xml
- //translation: // DataTypeOfVariable giveName = (DataTypeofVariable <-casting) findViewById(R.id.idOfTheVariableWeCreated);
- View.OnClickListener listenerName = new View.onClickListener(){ //the "View" version of "OnClickListener
- @Override
- public void onClick(View view) {
- // The button was clicked so update the fact label with a new fact
- String fact = ""; //use String because the fun facts are Strings
- // Randomly select a fact
- 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.
- 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
- // "(3)" limit random number generator to just 3 choices
- fact = randomNumber + ""; // Convert the randomNumber integer to a String value, then store it in the "fact" variable. "Moosh" them together
- // Update the label with our dynamic fact.
- 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 ()
- //"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.
- }
- };
- showFactButton.setOnClickListener(listenerName); //listen for button press on the "showFactButton"
- }
- /*
- void = doesn't return any kind of visual result
- 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.
- * now is line 14, 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 a key to the layout we've been working on.
- So what this method does is it tells this activity to use activity_fun_facts.XML layout, as the layout for the entire screen.
- The purple stuff like CONTENT_ITEM_TYPE basically all refer to predetermined stuff hidden inside R.java file
- ProjectView>App>Build>Generated>Source>r>com.example...>R.java
- */
- /*
- what is a constructor: http://www.homeandlearn.co.uk/java/class_constructor.html
- */
- https://teamtreehouse.com/library/build-an-android-app/basic-android-programming/accessing-views-in-code
- //-----------------------------------------------------------------------------------------------------------------------------------------
- /*
- Java is an OOP Language
- 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
- a game who can interact with other characters or objects.
- I said that an object has its own properties and abilities, so in this game character example, if the character is considered an object,
- then its properties might be its name and whether it's male or female, and its abilities could be things like walking and jumping.
- */
- /*
- //-----------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement