Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Clarify stuff

I almost finished create a simple android app, and I want to clarify stuff in my head.

I also made a sticky note with a lot of lines... But I would like it to be more organized, like have a part about java, part about design... You know any app/program which can do that ;D ?

So return is the command used to set what will the syntax I mean the codes between those 2{} will return. As I understood :

    public int getColor() {
    String color = "";
    Random randomGenerator =  new Random();
    int randomNumber = randomGenerator.nextInt(mColors.length);
    color = mColors[randomNumber];
    int colorAsInt = Color.parseColor(color);
    return colorAsInt; 
    };

"return "colorAsInt"" is the thing we want this to give as result ? Is colorAsInt a random name ? Or is ColorAsInt a command to cast/make it an int (if it is, why are there no dots) ? int are values like 1 2 3 and -5 -9 ... We assign "" (nothing) to color string just to create it, without the "" we would get an error ... right ? m is used to make the variable usable between classes.. ? only that ?

    int colorAsInt = Color.parseColor(color);

in this line we create the variavle colorAsInt and assign its value to Color.parseColor(color).

An int can't be a string too, a string are letter like a word, can it be values too ? An int can just be a number .. ?

    public int getColor() { .... }

In this line we declare getColor and tell it to be the stuff between the {} but as return we return colorAsInt ... I don't understand that part.

And I am almost done :

    showFactButton.setOnClickListener(listener);

showFactButton is the name we created for the button, .setOnClickListerner is want we want to do , and (listener) is the name we created for the listener, therefore make it be listened.

final RelativeLayout RelativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);

Well I am confused which one do we declare ? why is it there two time ?(because RelativeLayout is also a kind a (I don't know how to call this)) why do we add RelativeLayout between "()" ?

    @Override

What does that mean ?

Also I don't exactly understand these : (I see want it's for but not how it exactly works)

private FactBook mFactBook = new FactBook();
private ColorWheel mColorWheel = new ColorWheel();

Thank you very much, I know most of them as you can see, but just want to make things clear.

Hehe - lots of questions!

I'll try my best to help clarify for you :)

  1. return values in methods - colorAsInt is an int variable that is arbitrarily named "colorAsInt". You can name it anything else and it would also work.

  2. In terms of the String color = "" - you could just declare it as String color;, however at that point the variable is null and if you try and access it, you would get an error - your code will still compile and work - it's just good practice to assign an empty value (and then testing for it being empty) vs. leaving it null.

  3. int colorAsInt = Color.parseColor(color); - You are taking a string color, passing it to Color.parseColor and this method assigns an integer representation of the color to the colorAsInt variable. Try this link to play with converting hex values to integers: http://www.binaryhexconverter.com/hex-to-decimal-converter

  4. An int can only contain numbers. A String can contain numbers, but they are treated as strings vs. actual numbers with a numerical value.

  5. The getColor method explained:

/* the following method will return a random color selected from our mColors array - represented as an integer */

public int getColor() {

/*

This is our array filled with colors String[] mColors = {"#FFFFFF","#E0E0E0","#FFAAEE", "#ABABAB"}

*/

String color = ""; // define a string variable to hold a randomly selected color from our mColors array


Random randomGenerator =  new Random(); // define a random number generator

/* based on the number of elements in the array, select a random number from 0-3 */

int randomNumber = randomGenerator.nextInt(mColors.length); 


color = mColors[randomNumber]; // select the element from the array that corresponds to our random number and assign it to the color variable (value held by color will be a string e.g. "#FFFFFF")

int colorAsInt = Color.parseColor(color); // use the Color.parseColor method, pass it the color we randomly selected and convert the "hex" string to an integer - assign the converted integer value to the colorAsInt variable

return colorAsInt;  // return the integer

};

  1. final RelativeLayout RelativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);

Most elements in an iOS screen are types of "Views" - buttons, textboxes, even the "frame" that holds all of those elements - the "frame" is a Layout Object, and in this case we are using the RelativeLayout type of the Layout Object.

When you call the findViewById(R.id.RelativeLayout), you are basically saying "get me a reference to the RelativeLayout object that has the id = "R.id.RelativeLayout" (this is assigned internally by the compiler - kind of like a constant value). This returns an object of type View - but we want to use it as a RelativeLayout, so we use: (RelativeLayout) findViewById(R.id.RelativeLayout); which CASTS the object View to a type of RelativeLayout object.

  1. @Override - this is used to mark methods that will override methods with the same name and parameters that might exist the parent object

  2. In these statements, you are defining field level variables (traditionally prefixed with 'm') and instantiating them by using the "new" keyword. This reserves memory for the newly created objects.

private FactBook mFactBook = new FactBook(); private ColorWheel mColorWheel = new ColorWheel();

Hope this helps!

God bless you !! Thank you very much, btw do you know an app where I could organized all my notes, there are so much, I get lost sometimes :P
Thanks !!!

1 Answer

I like using Evernote...