1. Download our Official Android App: Forums for Android!

Apps Analysing hello world

Discussion in 'Android Development' started by ketanco, Sep 22, 2011.

  1. ketanco

    ketanco Newbie
    Thread Starter
    Rank:
     #615
    Points:
    16
    Posts:
    30
    Joined:
    Apr 13, 2011

    Apr 13, 2011
    30
    3
    16
    IN the famous hello world tutorial below, I want to ask about a few things:

    Code (Text):
    1. package com.mysite.helloworld;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.widget.TextView;
    6.  
    7. public class MysitehelloworldActivity extends Activity {
    8.     /** Called when the activity is first created. */
    9.     @Override
    10.     public void onCreate(Bundle savedInstanceState) {
    11.         super.onCreate(savedInstanceState);
    12.         TextView tv = new TextView(this);
    13.         tv.setText("Hello, Android");
    14.         setContentView(tv);
    15.     }
    16. }
    here are my questions:
    1-Is tv a variable or what is that?
    2-what is "this" in TextView(this)? What does it represent? What does it do?
    3-when you say tv.setText("Hello, Android"), what does tv.setText mean? I know setText is a command but what why do we have tv. in front of it?
    4-Is setContentView a command? The tv in paranthesis there... is it a variable or what does it do? is it coming from TextView tv, or tv.setText?

    Please answer one by one to each question, as I asked it the way I will understand sorry I am just a beginner...
     

    Advertisement

  2. raduq

    raduq Newbie
    Rank:
    None
    Points:
    16
    Posts:
    10
    Joined:
    Sep 18, 2011

    Sep 18, 2011
    10
    1
    16
    Student
    Cluj Napoca - Romania
    First of all I think you should study a bit about Object Oriented Programming, because this programming language is Java and it's an OO (Object Oriented) type of language.

    What this means is: (to start answering your questions)
    Answer: tv is a type of variable, but it's called an Object. Objects are exactly like variables but they can hold much more information. For example, an object has more properties (can hold more information about itself), than for example a regular integer variable (ex. int x; ).

    You can consider a CAR an Object. A CAR has width, length, color, power, etc. All these characteristics are its properties. You can define the CAR's poperties almost like this (ex: CAR.length = 10 ft, CAR.widht = 5 ft, CAR.color = blue, CAR.power = 200 bhp).

    tv is an Object but it is of type TextView. TextView is a type of Object that you use for printing out text (you could also set its color, size, etc.)

    The variable "tv" has the property of being able to have a certain text. It also can have a different color, size, etc. like I've mentioned before.

    (From this point on I'll answer your 3rd question too but I will continue explaining the idea that I left off at the first question).

    This is why you write code like:

    Code (Text):
    1. tv.setText("This is a lesson");
    You tell the object tv to set its text variable to "This is a lesson", and you do this by first telling the program what object you are talking about (tv), then the property you want to set by using the dot (.) right after the name of the object.

    The setText("This is a lesson"); is just a command, we call it method, (like functions in C or C++ if you've studied before) that tells the tv Object to call upon the function that sets its text to "This is a lesson".

    Answer: The "this" represents the fact that the TextView type of object needs to be told where to be printed on (a console, a user interface, a file, etc.). In Object Oriented programming, when you say "this", you are talking about the class in which the variable tv is defined. (I'm a bit sure you didn't understand what I just said here, but you really should check out a few basic tutorials about Object Oriented programming, and then you will understand.) In our case, the class is public class MysitehelloworldActivity.

    This class
    (class means type of Objects. Ex: When you want to define the basic properties of ANY Car object you want to make you first create a class that you call Car like this:

    Code (Text):
    1. public class Car{[INDENT] int bhp;
    2. [/INDENT][INDENT] Color car_color;
    3. [/INDENT]}
    ), is the main class of your program. When you run the program, it makes an Object of type MysitehelloworldActivity which contains all the information about the program.

    And when you write TextView(this); it means that the new TextView type of Object that you created is a part of the MysitehelloworldActivity class (which is the main class of your program, like I said).

    To better understand that please follow this link: What Is a Class? (The Java
     
  3. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    Ill futher raduq's post with some more info:

    1) While raduq was partly correct here, there were a couple things that weren't entirely true (I think he has the right idea, but just didn't relay is correctly). First of all, tv is a pointer capable of pointing to an object of type Textview. When the "new TextView(this)" command is executed, it creates an instance (object) of type Textview and assigns the address of the newly created instance to the tv pointer. I am not sure of your programming background, so the concept of pointers might be over your head, but in java, everything that is not a primitive (int, float, double, etc..) is a pointer.

    2) The this keyword is pretty standard in all OOP languages (including C++). It is simply a keyword that tells the compiler to "look at" the class that contains the method call or variable.

    3) Whenever you see a '.' in a line of code, it means that whatever precedes the '.' is a pointer to an object (in this case, tv), and whatever follows the '.' is a member of the object pointed to by tv (in this case, tv is of type TextView). a member can be either a method (a.k.a member function), or a data member ( a.k.a. member variable). If you see a member that is directly followed by a '(', then that means it is a method, not a data member. In this case, setText is directly followed by '(', so it is a method.

    4) setContentView is a method of the Activity class. Since the MysiteHelloWorld class is a subclass of Activity (as noted by the extends keyword), it contains all of the methods of the Activity class. Because you are inside an Activity class and calling a method of the Activity class, it is ok to call the method directly, without a pointer and a '.' preceding it. the setContentView() method takes one parameter - a pointer to a object of type View. Since TextView is a subclass of View, you can send a TextView as a parameter.
     
    ketanco likes this.
  4. raduq

    raduq Newbie
    Rank:
    None
    Points:
    16
    Posts:
    10
    Joined:
    Sep 18, 2011

    Sep 18, 2011
    10
    1
    16
    Student
    Cluj Napoca - Romania
    Thanks for clearing up some things for me too :)
     
    ketanco likes this.
  5. ketanco

    ketanco Newbie
    Thread Starter
    Rank:
     #615
    Points:
    16
    Posts:
    30
    Joined:
    Apr 13, 2011

    Apr 13, 2011
    30
    3
    16
    Thanks so much but I am still not entirely clear due to my beginner level. In your answer to

    1-Textview is an object but TextView is a command right? Why do these names need to be so close to each other? Isn't this confusing? Is TextView also called a "function" here? If not which one(s) in this code are called function? I know the concept of function. There I can relate.

    2- What does it mean when you say "look at" here? Does it mean in the class we are looking at, there was a predefined method for "this"? And are we looking into MysitehelloworldActivity class?

    3- In answer 1 you said tv was a pointer. But now again tv is a pointer but this time the syntax is different. There is a dot now. I am confused here. Also by method do you mean function? Also what do you mean by data member?

    4-I didnt understand the last two sentences here.

    sorry i am being a pain but trying to understand the basics. I am also reading the oracle tutorial about classes objects etc... i am aware of all hose. but still even tey write it as if I know or I am supposed to know some things beforehand. even the Textview vs TextView confuses me.
     
  6. raduq

    raduq Newbie
    Rank:
    None
    Points:
    16
    Posts:
    10
    Joined:
    Sep 18, 2011

    Sep 18, 2011
    10
    1
    16
    Student
    Cluj Napoca - Romania
    A class has more parts:

    The definition/declaration

    Code (Text):
    1. public class Car{
    2. int car_horsepower;
    3. int number_of_doors;
    4. }
    through which you define your type of object. (In this case, Car).

    Also, in order to create a Car type of object you need to create a function inside the Car class called constructor through which the program allocates memory for that type of object. An example of constructor for the Car class could be like this: (VERY IMPORTANT: the name of the constructor function needs to be the same as the name of the class, that's why you are probably confused)

    Code (Text):
    1. public Car(int horespower, int nr_doors){
    2. this.car_horsepower = horsepower;
    3. this.number_of_doors = nr_doors;
    4. }
    The entire code for the class would look something like this:

    Code (Text):
    1. public class Car{
    2. int [COLOR=Blue][B]car_horsepower[/B][/COLOR]; //because all cars have an amount of horsepower
    3. int [COLOR=SeaGreen][B]number_of_doors[/B][/COLOR]; //because all cars have doors
    4.  
    5. public Car(int [COLOR=DarkOrange]horsepower[/COLOR], int [COLOR=Red]nr_doors[/COLOR]){
    6. this.[COLOR=Blue][B]car_horsepower[/B][/COLOR] = [COLOR=DarkOrange]horsepower[/COLOR]; //the car's horsepower that i have just created is equal the "horsepower" parameter
    7. this.[COLOR=SeaGreen][B]number_of_doors[/B][/COLOR] = [COLOR=Red]nr_doors[/COLOR]; //the number of doors on the car i have just created is equal to the "nr_doors" parameter
    8. }
    9. }
    The allocation of memmory is through this code:

    Code (Text):
    1. new Car(250,4)
    So, in conclusion, the following code:

    Code (Text):
    1. Car bugatti = new Car(1000,2);
    allocates memmory for a new Car type object (for the two integers, number of car doors and horsepower,etc.) who's "name" (or pointer) is bugatti and it calls the constructor method Car() by using the new command and sends those parameters that will be 1000 and 2, which are the values for the car_horsepower and number_of_doors properties.

    I hope I've been a little clearer now in my explanation and I hope you understand more :) In case there are any more questions, feel free to post.
     
  7. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    @OP: I don't mean this reply to be offensive, but it seems that you have no experience with the OOP paradigm. Furthermore, by your frequent use of the word "command," I am assuming you have little to no experience in programming in general. If either of these is the case, you shouldn't be starting with Android. Android makes use of many advanced programming concepts and isn't for the beginner. I have been seeing a lot of new programmers trying to start with Android development. This is simply something that cannot be done. You need to learn the basics of programming, as well as OOP, and also, knowledge of the Java programming language (NOT Javascript!) is beneficial before even thinking about Android development.
     
  8. Malkoma

    Malkoma Newbie
    Rank:
    None
    Points:
    15
    Posts:
    11
    Joined:
    Aug 20, 2010

    Aug 20, 2010
    11
    0
    15
    Software Developer
    UK
    Hi ketanco

    Some of the posters are trying to point out how difficult it is for beginners who don't understand Java and its Object Orientated aspects to get started developing for Android. The Android SDK assumes one understands Java OOP but if you take it one step at a time and try to analyze the Hello World you will get there, its just a steep learning curve. You are on the right track and once you understand how the Android GUI fits together it becomes easier. The OOP is not an easy thing to learn but some of the posts here are a good place to start as they explain things in a friendly manner.

    _____

    In your questions - tv is a TextView, a part of the screen you can draw to and you can direct "commands" to the TextView by appending the relevant ".command" to it, eg

    tv.setText("Hello, Android")

    If you Google for "Android" and "TextView" you will find the Android developer docs, they list all the commands and functions one can use with a TextView. There are lots of Classes like TextView in Android, you can even make your own.

    _____

    Some of the posters know how hard it is for beginners to get started so you could always take a look at HAC (HyperNext Android Creator). It takes care of all the SDK stuff and hides the OOP so you just need to use English-like commands. In HAC the command to draw on canvas (like a textview) would be as follows where 1 is the canvas number, x and y are coords :-

    CanvasDrawText(1,'Hello, Android',x,y)

    Furthermore, one doesn't need all the code surrounding your example as the canvas is set up in the HAC GUI although they are dynamic and can be changed at runtime.

    HAC doesn't support everything in the Android SDK but its getting better with every update. By the way, there is a freeHAC demo but the full version isn't free.

    Best of luck with your programming, by the way, Stackoverflow website is a mine of Android information.

    Malcolm
     

Share This Page

Loading...