Create a Simple Sum Application Use in Android

Step 1: Install android studio and sdk.

Step 2: Open the adroid studio after design the app use this code.

Step 3: Write a java code in this app use to this code.

Java Code: 

  1. package com.example.sreenath.sum1;  
  2.   
  3. import android.app.Activity;  
  4.   
  5. import android.os.Bundle;  
  6.   
  7. import android.view.Menu;  
  8.   
  9. import android.view.MenuItem;  
  10.   
  11. import android.view.View;  
  12.   
  13. import android.widget.*;  
  14.   
  15. public class MainActivity extends Activity  
  16. {  
  17.   
  18.     EditText txtNum1;  
  19.   
  20.     EditText txtNum2;  
  21.   
  22.     EditText txtTotal;  
  23.   
  24.     Button btnCompute;  
  25.   
  26.     @Override  
  27.   
  28.     public void onCreate(Bundle savedInstanceState)  
  29.     {  
  30.   
  31.         super.onCreate(savedInstanceState);  
  32.   
  33.         setContentView(R.layout.activity_main);  
  34.   
  35.         txtNum1 = (EditText) findViewById(R.id.editText1);  
  36.   
  37.         txtNum2 = (EditText) findViewById(R.id.editText2);  
  38.   
  39.         txtTotal = (EditText) findViewById(R.id.editText3);  
  40.   
  41.         btnCompute = (Button) findViewById(R.id.button1);  
  42.   
  43.         btnCompute.setOnClickListener(new ClickButton());  
  44.   
  45.     }  
  46.   
  47.     private class ClickButton implements onClickListener  
  48.     {  
  49.   
  50.         public void onClick(View v)  
  51.         {  
  52.   
  53.             int x = Integer.parseInt(txtNum1.getText().toString());  
  54.   
  55.             int y = Integer.parseInt(txtNum2.getText().toString());  
  56.   
  57.             int total = x + y;  
  58.   
  59.             txtTotal.setText(Integer.toString(total));  
  60.   
  61.         }  
  62.   
  63.     }  
  64.   
  65.     @Override  
  66.   
  67.     public boolean onCreateOptionsMenu(Menu menu)  
  68.     {  
  69.   
  70.         // Inflate the menu; this adds items to the action bar if it is present.  
  71.   
  72.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  73.   
  74.         return true;  
  75.   
  76.     }  
  77.   
  78.     @Override  
  79.   
  80.     public boolean onOptionsItemSelected(MenuItem item)  
  81.     {  
  82.   
  83.         // Handle action bar item clicks here. The action bar will  
  84.   
  85.         // automatically handle clicks on the Home/Up button, so long  
  86.   
  87.         // as you specify a parent activity in AndroidManifest.xml.  
  88.   
  89.         int id = item.getItemId();  
  90.   
  91.         //noinspection SimplifiableIfStatement  
  92.   
  93.         if (id == R.id.action_settings)  
  94.         {  
  95.   
  96.             return true;  
  97.   
  98.         }  
  99.   
  100.         return super.onOptionsItemSelected(item);  
  101.   
  102.     }  
  103.   

XML Code:

  1. <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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:id="@+id/sree">  
  2.   
  3.     <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText1" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="41dp" android:layout_marginStart="41dp" android:layout_marginTop="94dp" />  
  4.   
  5.     <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_below="@+id/editText1" android:layout_alignLeft="@+id/editText1" android:layout_alignStart="@+id/editText1" android:layout_marginTop="47dp" />  
  6.   
  7.     <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText3" android:layout_below="@+id/editText2" android:layout_alignLeft="@+id/editText2" android:layout_alignStart="@+id/editText2" android:layout_marginTop="65dp" />  
  8.   
  9.     <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SUM" android:id="@+id/button1" android:layout_below="@+id/editText3" android:layout_centerHorizontal="true" android:layout_marginTop="54dp" />  
  10.   
  11. </RelativeLayout>  

Step 4: Run the application use to emulator.

Step 5: Create a AVD.

Successfully complted the your sum application.Thank you all.