Sunday, 25 September 2016

Android basic Addition

Addition in android



Android Button Example

android button
Android Button represents a push-button. The android.widget.Button is subclass of TextView class and CompoundButton is the subclass of Button class.
There are different types of buttons in android such as RadioButton, ToggleButton, CompoundButton etc.
Here, we are going to create two textfields and one button for sum of two numbers. If user clicks button, sum of two input values is displayed on the Toast.

Drag the component or write the code for UI in activity_main.xml

First of all, drag 2 textfields from the Text Fields palette and one button from the Form Widgets palette as shown in the following figure.
android button example

Activity class

Now write the code to display the sum of two numbers.
File: MainActivity.java
  1. package com.example.sumof2numbers;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.Toast;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     private EditText edittext1,edittext2;  
  14.     private Button buttonSum;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.           
  20.         addListenerOnButton();  
  21.           
  22.     }  
  23.     public void addListenerOnButton(){  
  24.         edittext1=(EditText)findViewById(R.id.editText1);  
  25.         edittext2=(EditText)findViewById(R.id.editText2);  
  26.         buttonSum=(Button)findViewById(R.id.button1);  
  27.           
  28.         buttonSum.setOnClickListener(new OnClickListener(){  
  29.   
  30.             @Override  
  31.             public void onClick(View view) {  
  32.                 String value1=edittext1.getText().toString();  
  33.                 String value2=edittext2.getText().toString();  
  34.                 int a=Integer.parseInt(value1);  
  35.                 int b=Integer.parseInt(value2);  
  36.                 int sum=a+b;  
  37.     Toast.makeText(getApplicationContext(),String.valueOf(sum),Toast.LENGTH_LONG).show();  
  38.             }  
  39.               
  40.         });  
  41.           
  42.     }  
  43.     @Override  
  44.     public boolean onCreateOptionsMenu(Menu menu) {  
  45.         // Inflate the menu; this adds items to the action bar if it is present.  
  46.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  47.         return true;  
  48.     }  
  49.   
  50. }  

It is developed by adt bundle on android 4.2 using minimum sdk 8 and target sdk 16.

Output:

android button example output 1 android button example output 2

No comments:

Post a Comment