Skip to main content

Sum of two Numbers App in Android

Before starting  example using Android SDK, you have to make sure that you have setup your Android development environment properly in your system and you you have knowledge with Eclipse IDE.

The first step is to create a simple Android Application using Eclipse IDE. Follow the option File -> New -> Project and finally select Android New Application wizard from the wizard list.
Name your application as SumApp



Create blank activity with MainActivity



Next, follow the instructions provided and keep all other entries as default till the final step. Once your project is created successfully, you will have following project screen:

Anatomy of Android Application

You should be aware of a few directories and files in the Android project:

S.N.
                      Folder, File & Description
1
src
This contains the .java source files for your project. By default, it includes an MainActivity.javasource file having an activity class that runs when your app is launched using the app icon.
2
gen
This contains the .R file, a compiler-generated file that references all the resources found in your project. You should not modify this file.
3
bin
This folder contains the Android package files .apk built by the ADT during the build process and everything else needed to run an Android application.
4
res/drawable-hdpi
This is a directory for drawable objects that are designed for high-density screens.
5
res/layout
This is a directory for files that define your app's user interface.
6
res/values
This is a directory for other various XML files that contain a collection of resources, such as strings and colors definitions.
7
AndroidManifest.xml
This is the manifest file which describes the fundamental characteristics of the app and defines each of its components.

The Manifest File

AndroidManifest.xml is a required file for every application. It sits in the root folder for an application, and describes global values for your package, including the application components (activities, services, etc) that the package exposes and the implementation classes for each component, what kind of data each can handle, and where they can be launched.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sumapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>















Activity_main layout File:

Choose graphical view in activity_main file from layout folder. From the Form widget choose two TextView items change their text value from property window. Then place two textfields and Button on the screen. Now layout looks like the screen below:-







Activity_main.xml File :


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.sumapp.MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="39dp"
        android:layout_marginTop="64dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignLeft="@+id/editText1"
        android:layout_marginBottom="46dp"
        android:text="Enter First Number" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="32dp"
        android:text="Enter Second Number" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="23dp"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="41dp"
        android:text="Sum" />

</RelativeLayout>

 

MainActivity File :-


The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately gets converted to a Dalvik executable and runs your application.

package com.example.sumapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
       private EditText edittext1,edittext2;  
    private Button buttonSum; 
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              addListenerOnButton();
       }
       public void addListenerOnButton(){  
        edittext1=(EditText)findViewById(R.id.editText1);  
        edittext2=(EditText)findViewById(R.id.editText2);  
        buttonSum=(Button)findViewById(R.id.button1);  
          
        buttonSum.setOnClickListener(new OnClickListener(){  
        
                   @Override 
                   public void onClick(View view) {  
                       String value1=edittext1.getText().toString();  
                       String value2=edittext2.getText().toString();  
                       int a=Integer.parseInt(value1);  
                       int b=Integer.parseInt(value2);  
                       int sum=a+b;  
           Toast.makeText(getApplicationContext(),String.valueOf(sum),Toast.LENGTH_LONG).show();  
                   }  
                     
               });  
                 
           }  
       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

       @Override
       public boolean onOptionsItemSelected(MenuItem item) {
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
              if (id == R.id.action_settings) {
                     return true;
              }
              return super.onOptionsItemSelected(item);
       }
}






Running the Application


Running an Android App require AVD Emulator or your android mobile phone connected to laptop. Here I am using AVD Emulator to run application. Right click on your android project and select run as android application.







Comments

Popular posts from this blog

Home automation using Arduino Board

Today’s homes require sophistication control in its different gadgets which are basically electronic appliances. This has revolutionized the area of home automation with respect to an increased level of affordability and simplicity through the integration of home appliances with smart phone and tablet connectivity. Smart phones are already feature-perfect and can be made to communicate to any other devices in an adhoc network with a connectivity options like Bluetooth . With the advent of mobile phones, Mobile applications development has seen a major outbreak. Utilizing the opportunity of automating tasks for a smart home, mobile phone commonly found in normal household can be joined in a temporary network inside a home with the electronic equipments. Android, by Google Inc. provides the platform for the development of the mobile applications for the Android devices . Home automation system is a mobile application developed using Android targeting its vast market which will be benefi...

Tips for Building Android Mobile and Tablet Apps

1. Check Popular Apps Before you actually start writing your own apps, you definitely want to resear ch the market if there are already exisiting apps providing the planned features. If there are, then take notes about what they provide and ask yourself if you are confident in doing better. 2. Model After Existing Sample Codes If you installed the Android SDK, there are several great sample projects inside sdk/extras/android/support/samples showcasing almost all the user interface examples and major functionalities, which you can use as templates to build and modify for your apps. 3. Avoid Undocumented APIs Some code worked at first but after a few Android SDK updates, it stopped working. 4. Layout for Device Orientation and Various Screen Sizes Since devices have embedded orientation sensors and they are usually defaulted to automatic mode, we need to account for that to provide a better user experience.

Text To Speech Android App.

                                                                                                                                                                      Before starting  example using Android SDK, you have to make sure that you have setup your Android development environment properly in your system and you you have knowledge with Eclipse IDE. The first step is to create a simple Android Application using Eclipse IDE. Follow the option   File -> New -> Project   and finally select   Android New Application   wizard from the wizard ...