hello, iãm edu from edu4java this is android tutorial number 3 we are going to see user interface java versus xml as weãve seen the user interface can be developed in java code or using xml letãs review the last example we have been looking at in the past tutorials here ventana is an activity that has a method oncreate
Android xml tutorial, which we override and this oncreate has a setcontentview method which is fed by an r.layout.main that is this layout xml here this xml has a linearlayout and inside this linearlayout there is container in which we find a textview
letãs run it letãs see what happens when we run it the first time we run the emulator we get this screen we can press menu and we get our application as you can see it is the layout and the hello edu which is the textview we saw in the main xml it is inside a layout which is invisible ok, letãs see if we can do this by code instead of xml in order to do that we are going to comment this lines
setcontentview and we are going to start creating the components by code iãm going to paste this java code here. we get these errors for imports, if you press ctrl+shift+o we get automatically the imports as you can see here ok, now we are going to put the xml here in order to compare what we do with java code ok, the linearlayout, i create a linearlayout here with a new linearlayout and this needs a parameter. this parameter is a context.
what is a context? here we can see the context is actually the activity if you come here, ctrl+t and we can see here the activity inherits from context then, the activity and ventana is a context this is the context our component is asking for ok, then we are going to start with textview we create a new one in the same way, set the text; string hello, that is in a string xml but from the code with point to it with r.string hello
instead of string/hello now we are going to set in the linearlayout we are going to add this textview inside and at the end we are going to set the contextview instead of with the xml just with the linearlayout which is the first component created here ok, letãs run the application we can run it from here, just pressing this button here and the emulator starts and you can see our application is the same but instead of xml we have used java code
java code instead of xml we get the same functionality ok, letãs try to make something more interesting letãs add for example a button here i paste this code, shift+ctrl+o to get the imports and i get an error because update is not here, iãm going to pick update and paste the update method here, okã– i have a variable without definitionã– iãm going to ask to create a fieldã–
i will initialize this in zero ok, this is a new button and then the button is added to the linearlayout; addview iãm going to organize this here better we call an update method here with button in order to set the text in the button ok, letãs run it it is loadingã–loaded! here we get a problem; the orientation of the layout here is horizontal instead of verticalã–
if you remember the xml main, the layout was vertical and here we set nothing ok, we are going to set here the setorientation orientation is an int so surely there is a constantã– in linearlayoutã– linearlayout.vertical, this oneã– okã– then, now we set the orientationã– and we execute againã– we getã– perfect!! when we click here, nothing happens! letãs try to make something happen when we press the button.
letãs add a listener to the button in order to do something when we click the button ok, iãm going to paste this here in button i set an onclicklistener and the listener is going to be the same object; ventana i onclicklistener, ok, the problem is that ventana is not an onclicklistener you have to implement the interface this is easy, we can click here and we pick let ventana implement onclicklistener, this one
magically we get onclicklistener here but, we get an error now that we have to add unimplemented methods now, yes, we get onclick here we can put the code to do something we are going to put update in order to increment ã¬iã® we are going to see the effect we have a problem here because i need a cast letãs check it here you can see i click here and i get different numbers
ok, we do all this in java code letãs try to do the same in xml we need to add here a button i can paste the code here for a button and iãm going to comment all the code about creating the componentsã– i get a problem here buttonã– i have no reference for button i need the reference for button in order to call the update method and for the listener
how can i get itã– i can get it with this check this id hereã– +id/buttonã–generate in the r file this constant button and we can reference this id button from the java code here and this file view by id gives me the reference to the button which is created in layout mainã– ok, i have the reference for button here and i can now work with the button letãs check if this is working yes, as you can see now we are working with xml instead of java code the last version of android has a better solution for this,
an attribute onclick, you can set in the button an attribute onclick and fill this onclick with a method name, we are saying here that onclick has to be attended by onclick method then, we go to ventana here and we can delete this because we donãt need the listener any more, we can delete this; we can delete the implementation because we donãt need a listener any more we delete this too and this onclick which receives as a parameter a view is enough
this gives me an error, iãm going to comment it and we are going to handle it later ok, runã– letãs see if this works we get a blank button but if we click itãs working the blank button is because the update here we commented does not initialize but we can fix this here; we are going to put ã¬click number zeroã® and just enough!
letãs start and see what happens, ok, we get zero, one, two, threeã– ok, everything is working
as you can see today we work with java and with xml and for this tutorial we are out of time, see you!!