Thursday, March 1, 2012

Android- Sample calculator program

Hi, This is a sample program in android. It explains basic entities and programming concepts of android. Try this one.. 
Start eclipse IDE with ADT plugin. 

First select "File->New->Project->Android->Android Project" and click "Next".
In the new screen type in a Project Name as shown below.




 On the new screen Select the android platform in which you want to  create your application. See the image below.





Click "Next" here. You will get a new window..Give a package name their. Package name must be of two folder name separated with a dot. See the image below.

 
Click "Finish" Button. So you can see your project is on the "Package Explorer" panel on the left side of your Eclipse window.  Click on the project now. That will contain many folders as below.


 1. The "src" folder will contain your package (com.edayans here) in which the the Activity is located.
 2. The "gen" folder will contain a "R.java " file. This is automatically created and we need not to alter it.
 3. The "res" folder will contain resources for the application.
       A.  "drawable" folders will contain different images you want to use in your program.
       B.  The "layout-> main.xml" will provide you graphical User Interface for your project.
       C.  The  "values->strings.xml" file will be provided with different resourced like string, color etc.
4. The "AndroidManifest.xml" file will keep all records like application name, hardware access permissions  etc..

 Now  we can take the "values->strings.xml"  file. It will be of two form. "Resources" and  "strings.xml" file. Click on the Resources and you will see a window like following.

 Select the "hello(String)" from the left panel and click "Remove.." button. (You may notice an error has invoked at other files. leave it for now. We can fix it later).
 Now we can add our own resources. For that click "Add..." button. Select "string"  item from the new window and click "OK".
Now on the right panel you will see "Name" and "Value" fields. Type in the corresponding fields as follows.
Name: sum
Value: Calculate Sum

For more details see the image below.

 

Also add another String as follows

Name: result
Value: Result

Save the work till now. (Press ctrl+s). Now go to "layout-> main.xml". You can see two tabs at the bottom left corner named "Graphical Layout" and "main.xml". Select the Graphical layout. You will see a window like below.

Click on the "@string/hello" field and delete it. Then we can save our project again. (So the error is now fixed).

Now from the "palette" panel from the left, select "Text Fields" and drag "editText" to the screen. ("editText" may be noted with letters "abc" on it.) Do this twise to add another "editText" to the screen. So you have two "editText" on your screen.
 
Then add a "textview"  from the "Form Widgets" by dragging it to the screen.
 Now drag a "button" from the "Form Widgets" to the screen.  Now the screen will look like this.

Now we have to specify each fields properties. First we begin with Button. Right click on it and select "EditText". In the new window select "sum" and click "ok"..


 
Then right click on the "textview" select ""EditText". In the new window select "result" and click "ok".

Now the screen will look like follows.


Now right click again on button go to "Other Properties -> All by name -> onClick" set  value "onMyClickdoThis". (This wiil be used later for our onclick function ).

After that right click on the "editText" field. go to "Other Properties -> All by name ->Input Type" set them as "Number Signed" and "Number Desimal". Do this for both "editText" fields.

Now right click on "editText" again and set "Layout Width=Match Parent" and "Layout Height=Wrap Content".
Do this for both "editText" fields and also for "textView".

Now save the project again.

Now go to "src " folder and select "EdayansCalculatorActivity.java". Copy  the code given below and paste their.

package com.edayans;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EdayansCalculatorActivity extends Activity {
    /** Called when the activity is first created. */
    private EditText text1;
    private EditText text2;
    private TextView txtResult;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text1 = (EditText) findViewById(R.id.editText1);
        text2 = (EditText) findViewById(R.id.editText2);
        txtResult=(TextView)findViewById(R.id.textView1);
    }
    public void  onMyClickdoThis (View view) {
        switch (view.getId()) {
        case R.id.button1:
            if ((text1.getText().length() == 0) ||(text2.getText().length() == 0)) {
                Toast.makeText(this, "Please enter a valid number",
                        Toast.LENGTH_LONG).show();
                return;
            }else{
                int f_num=Integer.parseInt(text1.getText().toString());
                int s_num=Integer.parseInt(text2.getText().toString());
                txtResult.setText(String.valueOf(f_num+s_num));
               
            }
            break;
        }
}
}

Then right click on the project folder on the left panel. Then select "Run As -> Android Application". (See the image below).


Wait for some seconds, the emulator will be loaded soon. See the image below.



Click on the "lock" button and drag it to right side.(In the image below, it is indicated by  red arrow). Wait  for a second, your application will be launched.

 Note: The emulator varies with android platform and API version etc. Your emulator may not be like the above. Even if that, you have to unlock the emulator.



Then try it, give numbers to the text Fields and click the button "Calculate Sum". You got it.





No comments:

Post a Comment