Welcome to Google Developers Hackademy.
This is the first part of the Android SDK
introduction course,
developed by Enrique Diaz, from GDG Androidtitlan Mexico.
If this is your first contact with the course,
I recommend you watch it in order and do the exercises.
Welcome to lesson 3:
"Building blocks for Android."
My name is Enrique Diaz
and remember that last time we saw
basic tools for developing in Eclipse IDE.
In this lesson we will see
three very important things for developing.
We will learn what's an activity,
we will implement a fragment,
and finally, we will learn how to start an intent.
Next, we will talk about what's an activity.
An activity is a class contained in Android framework
to create use interfaces.
Imagine, like in a browser we have windows,
it's equivalent in Android is an activity.
I already have a project here called Hola Mundo,
in which we will implement an activity.
So we enter MainActivity
and will go quickly over
which methods are implemented.
They are two:
onCreate and onCreateOptionsMenu.
In order to create an activity, with a right click,
we hit "New" and then we look for "Class,"
and, when we select it,
it creates a window
where we need to add a name
and a superclass.
In this case we will name it "OtherActivity"
and we will inherit
from Activity.
Having this we hit on "Finish"
and, in our package
mx.androidtitlan.holamundo,
we already have this class created
just as we did it.
Now we will override a method
in order to add onCreate.
We hit "OK" and automatically
it generates this codelines.
I format a bit and now I'm writing
the setContentView method,
which receives a parameter,
which is the layout of my activity.
We can see it on this side, where we have our files,
in the layout folder.
We will also create an .xml file in there
right-clicking on my layout folder,
I hit "New..."
and then "Android XML file."
There, it will allow me to add a name for my file.
This one I will name activity_other.xml.
And while I'm at it, I can see
which elements I can have
as parent of my file.
We have ScrollView, SlidingDrawer,
Space, Spinner,
TextView, etcetera, etcetera.
In this case I look for RelativeLayout,
which is the layout that allows us to add views
and define them depending on others.
When I open my file, I can notice it has nothing,
just the title inside my file,
but I can also see its parent tag,
which is just RelativeLayout.
We will add the first activity
in the first TextView.
In the second TextView we will just place
a TextView, we format it,
we take the chance to visualize some properties
of RelativeLayout,
like aligning to the parent at the top,
wide, tall, margin.
And if we go to the graphical interface,
we can notice the change.
I'm going to close this file.
And inside the method setContentView
of OtherActivity
I'm going to pass it this other .xml file
and, as with any activity,
I have to declare it inside AndroidManifest.
In this case, in HolaMundoManifest
I have to copy the activity tags.
And now I'm going to run my example.
For that, see the play button of the upper menu?
I click on it and then, in that window that shows up,
I hit Android Application.
So I select it and I'll wait for the emulator
to launch the application.
Before it's launched, a window will show up
where it allows me to choose
if I want to launch the application in a device
that is already ready,
connected to the framework,
or if I want to launch a new virtual device.
In this case, I will use one that is already connected.
One interesting thing about this window
is that it tells you to select a device with the minimum API
your project is configured with.
In this case, it's API level 8.
I select genymotion-galaxy_nexus,
I hit "OK,"
and that's it!
We can already see my app Hola Mundo
with two texts that we created.
"Mi primer Activity" and "TextView."
Up next we will see the concept of intent.
An intent is a class within the framework
that allows us to relate one activity with another.
Whether we know it or not.
That, precisely,
is the concept of explicit or implicit intent.
Okay...
Now, the same instance of Hola Mundo.
Let's create an intent.
For that, I'll go to OtherActivity.java
and I will modify the .xml file
called "Activity_other.xml."
I'm going to drag a button from the user interface,
and I'll add another one below it.
I'll name the first one "Open browser,"
and the second one, in the "text" property,
will be "Enter another activity."
Besides, I will add the onClick property,
which will receive a method for both buttons.
The first one will be "openBrowser,"
and the second one, the method will be "openActivity."
The onClick property,
in a view, like a button,
allows us to send to it a method as a parameter
which, in Java coding, will do something.
This is the easiest way of making
a listener of a button.
Now, in my OtherActivity.java class,
I'm going to build both methods.
For that, I have to write this:
"public void method"
and I pass it a parameter,
which is the view that will be called.
Inside the method I'm going to declare
an intent-like object.
This one will be called "openBrowser."
Inside openBrowser method,
which receives the "view" parameter,
where "view" is the button to which we're sending
or defining the method,
we're going to create an intent instance,
which receives two parameters:
intent.ACTION_VIEW,
and the URL which I want to access.
Besides, I have to initialize the activity
with the startActivity method,
and which receives the intent as a parameter.
Then, I will create another method called "openActivity."
It will also receive a View as a parameter.
Besides, I will create an intent instance.
The difference is it'll receive two different parameters.
In this second intent,
it will receive the context of the package and of the class.
In this case is one of the parameters,
otherActivity.this,
and the second parameter is where I want to go.
Said in a simpler way,
de second parameter of the intent it receives
is where I am,
and where I want to access.
And I also initialize the activity
with the method startActivity and I pass the intent to it,
as a parameter.
I'm going to run the app. the same dialogue shows up.
Done, I now have both buttons inside my activity.
What each button should do
is, the first button,
ask me "What do you want to open?,"
because, in the particular case of my emulator,
I have two applications that open URIs,
which is the URL I'm passing to it.
Here we proved an implicit un intent.
I can select any of the two options,
I can select it does it every time it's opened
or just once.
And when I click on it, it automatically opens
the browser and it will enter the URL I sent to it
inside the intent,
which in this case is androidtitlan.mx.
Back to the example, and with the other button,
all it does is send me to the other activity.
Why? this other intent is an explicit intent,
because what I'm defining is where I am
and where I want to go.
Now we will se how to implement
a fragment.
A fragment is an user interface
that's not a view or an activity.
It's rather a subactivity, in some way.
It allows us to insert, in a programmatic way,
or through an XML, any element
we want to reuse in our code.
In this example,
which will be a totally new one,
we enter the package mx.androidtitlan.xmlfragment.
In the class MainActivity.java,
the first thing we have to do is change the class
from which it inherits
and we will modify it through fragmentActivity.
We will leave as it is,
but we will create a new fragment,
a Java class that will be called Simplefragment,
that will inherit from Fragment.
In my Simplefragment.java,
what I will return in onCreateView
is the LayoutInflater,
which is the one in charge of inflating views, layouts,
inflater.inlfate,
and I'll pass it the interface that I want to inflate.
Inside my fragment I contain a TextView.
I'm going to change the text for "fragment" only
and I'll add a background to the parent view,
any color, in this case, "f123."
I use the "f" to define,
whether the parameter, or I use it to define
if it'll have a transparency.
When you put "f" in hexadecimals
that have no transparency,
and "123" is a hexadecimal as it is.
And I'll also modify the activity_main file,
which is the interface of my activity.
What I'm going to do is enter the graphical interface,
and I drag the "fragment" component.
So, as you can see, it's shown on the display,
and I'll modify some things.
I'll modify the width and the height,
just to have control and to know where's my fragment,
that is being called from an .xml file.
Now I run the application and there it is,
I can see a "Hello world," which contains the activity,
but also, I see a square the size of my fragment.
This means that what I did was, through the .xml file,
show a fragment inside an activity,
insert it.
Now, let's do another example
but inserting the fragment programmatically.
This means it will be through Java code.
For that, the same way, I'm going to build a fragment
that will be named as:
"SimpleFragment,"
and that will also inherit from Fragment.
Careful, it's very important
that it inherits from android.support.v4,
because that way we can use this compatibility backwards
and we can develop apps
from API level 4.
I select it, hit "Finish,"
and I wait for it to visualize in my package.
I also implement a method that is also onCreateView.
What I will return in the method onCreateView
will be inflater.inflate
and now I will pass it three parameters,
which are what's the interface I want to be shown,
the container, which is
the activity to which it is being added,
and a Boolean parameter, which is false.
Besides, I'll take the chance to create another .xml file
that will be called fragment_main.
To this one I'll pass,
inside the parent view, which is the RelativeLayout,
a parameter, which is background,
with a "f123" numeral.
What this means is that I'm passing it a color
where my first digit
is the transparency, and the other three
are a hexadecimal number.
Besides, inside the text contained in this fragment,
I'll write "fragment" only.
Now, the parameter I need in the interface
of the main activity is the ID.
So I declare it:
android:id
and I use "@+id"
because that way I tell my class R
that I want to add an instance,
which I want to access.
I close that file and now I'm going to declare,
inside the .xml file,
how I am going to call my fragment.
I'll do it right below setContentView,
but besides, don't forget to change the superclass.
In this case it will be "FragmentActivity."
It will inherit MainActivity from FragmentActivity.
To be able to call getSupportFragmentManager
.beginTransaction.add,
to which I will pass two parameters.
What's the ID to which I want to add
within my interface?
I mean, I am using ID container
that I added inside my RelativeLayout,
that I just modified,
and I show it what I want to add.
In this case, I just wrote a line above
where I am defining a SimpleFragment instance,
which is SimpleFragment, that is called
fragment = new SimpleFragment.
I launch my application...
and that's it, I can see that, in a programmatic way,
I'm calling a fragment
that is being executed or inserted
inside my activity.
Let's recapitulate.
In this lesson we saw three important points:
we created and understood what is an activity,
we implemented an intent,
and, finally, we knew the power of a fragment.
Remember, next lesson we will see
a real life example
creating my own Android app.
My name is Enrique Diaz. Keep on programming!