Addition Application For Android

Introduction

Ninety percent of people use Android smart phones. I will show you how to create an addition application for Android using Android studio. Android is a kernel based operating system. It allows the user to modify the GUI components and source code.

Requirements

  • Android studio
  • Little bit of XML and JAVA Knowledge.

Download link (android studio) https//developer.android.com/studio/index.html

Steps should be followed

Carefully follow my steps on how to create the addition application for android using android studio and I have included the source code below.

Step 1

Open Android studio start the new project.

Android

Step 2

Put the application name and company domain. If you wish to use c++ for code the project, Include c++ support then click next.

Android

Step 3

Select the android minimum SDK. After you chose the minimum SDK it will show approximate percentage of people who use that sdk then click next.

Android

Step 4

Choose the basic activity then click next

Android

Step 5

Put the activity name and layout name. Android studio basically takes the java class name from what you provide as the activity name.

Android

Step 6

Goto activity_layout.xml then click the text bottom. Into the activity_layout.xml copy and paste the below code.

activity_layout.xml code

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/relativeLayout1"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" >  
  5.   
  6.     <LinearLayout  
  7.         android:id="@+id/linearLayout1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_alignParentRight="true"  
  12.         android:layout_alignParentTop="true" >  
  13.   
  14.         <TextView  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:layout_gravity="center"  
  18.             android:text="ADDITION"  
  19.             android:textSize="20dp" >  
  20.   
  21.         </TextView>  
  22.     </LinearLayout>  
  23.     <LinearLayout  
  24.         android:id="@+id/linearLayout2"  
  25.         android:layout_width="wrap_content"  
  26.         android:layout_height="wrap_content"  
  27.         android:layout_alignParentLeft="true"  
  28.         android:layout_alignParentRight="true"  
  29.         android:layout_below="@+id/linearLayout1" >  
  30.         <TextView  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="ENTER NO 1" >  
  34.         </TextView>  
  35.         <EditText  
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             android:layout_weight="0.20"  
  39.             android:id="@+id/edittext1"  
  40.             android:inputType="number">  
  41.         </EditText>  
  42.     </LinearLayout>  
  43.     <LinearLayout  
  44.         android:id="@+id/linearLayout3"  
  45.         android:layout_width="wrap_content"  
  46.         android:layout_height="wrap_content"  
  47.         android:layout_alignParentLeft="true"  
  48.         android:layout_alignParentRight="true"  
  49.         android:layout_below="@+id/linearLayout2" >  
  50.         <TextView  
  51.             android:layout_width="wrap_content"  
  52.             android:layout_height="wrap_content"  
  53.             android:text="ENTER NO 2" >  
  54.         </TextView>  
  55.         <EditText  
  56.             android:layout_width="wrap_content"  
  57.             android:layout_height="wrap_content"  
  58.             android:layout_weight="0.20"  
  59.             android:id="@+id/edittext2"  
  60.             android:inputType="number">  
  61.         </EditText>  
  62.     </LinearLayout>  
  63.     <LinearLayout  
  64.         android:id="@+id/linearLayout4"  
  65.         android:layout_width="wrap_content"  
  66.         android:layout_height="wrap_content"  
  67.         android:layout_alignParentLeft="true"  
  68.         android:layout_alignParentRight="true"  
  69.         android:layout_below="@+id/linearLayout3" >  
  70.   
  71.         <Button  
  72.             android:layout_width="wrap_content"  
  73.             android:id="@+id/button1"  
  74.             android:layout_height="wrap_content"  
  75.             android:text="Addition"  
  76.             android:layout_weight="0.50" />  
  77.   
  78.         <Button  
  79.             android:layout_width="wrap_content"  
  80.             android:id="@+id/button3"  
  81.             android:layout_height="wrap_content"  
  82.             android:text="subtraction"  
  83.             android:layout_weight="0.50" />  
  84.         <Button  
  85.             android:layout_width="wrap_content"  
  86.             android:id="@+id/button2"  
  87.             android:layout_height="wrap_content"  
  88.             android:text="CLEAR"  
  89.             android:layout_weight="0.50" />  
  90.     </LinearLayout>  
  91.     <View  
  92.         android:layout_height="2px"  
  93.         android:layout_width="fill_parent"  
  94.         android:layout_below="@+id/linearLayout4"  
  95.         android:background="#DDFFDD"/>  
  96. </RelativeLayout>  
Android

 

Step 7

This is our user interface of the application

Android

Step 8

Go to LayoutActivity.java then click the text bottom. Into the LayoutActivity.java copy and paste the below code. Do not replace your package name.

LayoutActivity.java code

  1. package ganeshannt.layout;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.Toast;  
  10.   
  11. public class LayoutActivity extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     EditText txtData1,txtData2;  
  14.     float num1,num2,result1,result2;  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState)  
  17.     {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_layout);  
  20.   
  21.   
  22.         Button add = (Button) findViewById(R.id.button1);  
  23.         add.setOnClickListener(new OnClickListener()  
  24.         {  
  25.             public void onClick(View v)  
  26.             {  
  27.                 try  
  28.                 {  
  29.                     txtData1 = (EditText) findViewById(R.id.edittext1);  
  30.                     txtData2 = (EditText) findViewById(R.id.edittext2);  
  31.                     num1 = Float.parseFloat(txtData1.getText().toString());  
  32.                     num2 = Float.parseFloat(txtData2.getText().toString());  
  33.                     result1=num1+num2;  
  34.                     Toast.makeText(getBaseContext(),"ANSWER:"+result1,Toast.LENGTH_SHORT).show();  
  35.                 }  
  36.                 catch(Exception e)  
  37.                 {  
  38.                     Toast.makeText(getBaseContext(), e.getMessage(),  
  39.                             Toast.LENGTH_SHORT).show();  
  40.                 }  
  41.             }  
  42.         });  
  43.         Button sub = (Button) findViewById(R.id.button3);  
  44.         sub.setOnClickListener(new OnClickListener()  
  45.         {  
  46.   
  47.             public void onClick(View v)  
  48.             {  
  49.                 try  
  50.                 {  
  51.                     txtData1 = (EditText) findViewById(R.id.edittext1);  
  52.                     txtData2 = (EditText) findViewById(R.id.edittext2);  
  53.                     num1 = Float.parseFloat(txtData1.getText().toString());  
  54.                     num2 = Float.parseFloat(txtData2.getText().toString());  
  55.                     result2=num1-num2;  
  56.                     Toast.makeText(getBaseContext(),"ANSWER:"+result2,Toast.LENGTH_SHORT).show();  
  57.                 }  
  58.                 catch(Exception e)  
  59.                 {  
  60.                     Toast.makeText(getBaseContext(), e.getMessage(),  
  61.                             Toast.LENGTH_SHORT).show();  
  62.                 }  
  63.             }  
  64.         });  
  65.   
  66.         Button clear = (Button) findViewById(R.id.button2);  
  67.         clear.setOnClickListener(new OnClickListener()  
  68.         {  
  69.             public void onClick(View v)  
  70.             {  
  71.                 try  
  72.                 {  
  73.                     txtData1.setText("");  
  74.                     txtData2.setText("");  
  75.                 }  
  76.                 catch(Exception e)  
  77.                 {  
  78.                     Toast.makeText(getBaseContext(), e.getMessage(),  
  79.                             Toast.LENGTH_SHORT).show();  
  80.                 }  
  81.   
  82.   
  83.             }  
  84.         });  
  85.   
  86.     }  
  87. }  
Android

 

Step 9

Click the build bottom and make the project. Now your project compilation process is going on. It will show you any error found in your project 

Android

Step 10

Click run bottom. Your emulator will  start then run your application.

Android

Deliverables

Here we have successfully creates addition application for android using android studio

Android

Android

Don’t forgot to like and follow me. If you have any doubts just comment below.

Up Next
    Ebook Download
    View all
    Learn
    View all