***Last updated on 11 May 2013 ***

Friday, 29 March 2013

PART 3 :Adding the funtionality


hello everyone ,
 hope you have up till now done the xml part , so now we will be adding the functionality to our app. That will be done by the java. If you have some basic knowledge about JAVA it will be helpful here , if not dont worry just follow the steps carefully , i will be explaining everything.


STEP 1 : Initializing the variables

  • go to the  Mistic > src > com.game.mistic > MainActivity.java  .This is where the java class is, open it and you will find something like this .

  • after this initialize the variable , here we will be needing add button , subtract buton and the textview to show our output. For this initialize the variables as shown below. Initially you will be having some error , for that you have to import the packages (importing packages mean to get the definitions for the keywords we are going to use). For this  just hover over the keywords (here Button and TextView) and click on import (the first option in yellow box).


STEP 2: connecting the java class with xml file  

  •  For this in OnCreate method  add below code   

     add = (Button) findViewById(R.id.badd);
        sub = (Button) findViewById(R.id.bsub);
        display = (TextView) findViewById(R.id.tvDisplay);  
                  This will allow the java program to know from where to take the values, ie from where it is    taking response

STEP 3: Adding the listeners

  • In this step will will be adding listeners which will listen to the user's activity.

  • add the listener  add.setOnClickListener( )     (just type add.setOn    and Eclipse will suggest you this one .)

  • take the void listener and in place of null write new.ViewOnClickListener() again this will be suggested by the eclipse so you dont have to worry about the method it defines.

  • Note: Since java is case sensitive so take good care of cases(uppercase/ lowercase)

  •  After this add one more listener for adition ie, sub.setOnClickListener()


    Uptill now will will be having this , after this will will be assigning the work to our listeners

     

     for any query or suggestions comment below or share this post so that others can also benefit from it .

     

     

No comments:

Post a Comment