Program description:
This is a simple android calculator program with 3 edit text boxes, with 2 buttons.
User will enter two numbers in first and second edit text, and click the button.
button 1 is to add two numbers, and button 2 is to subtract the numbers.
First Activity First Activity( after entering numbers)
First Activity( click add) First Activity( click sub)
First Activity
Similar basic activity programs:
How to start an activity and pass data to second screen.
changing edittext text from button click listener
How to start an activity from main activity on clicking button, and pass data
Starting second activity based on login and password details
Getting results from child activity
Tags : android calculator program, android tutorial, calculator
This is a simple android calculator program with 3 edit text boxes, with 2 buttons.
User will enter two numbers in first and second edit text, and click the button.
button 1 is to add two numbers, and button 2 is to subtract the numbers.
First Activity First Activity( after entering numbers)
First Activity( click add) First Activity( click sub)
First Activity
package com.example.b15_calc; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { //step1 : create all the variables. EditText et1, et2, et3; Button b1, b2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //step2 : get all the views from xml file. et1 = (EditText) findViewById(R.id.editText1); et2 = (EditText) findViewById(R.id.editText2); et3 = (EditText) findViewById(R.id.editText3); b1 = (Button) findViewById(R.id.button1); b2 = (Button) findViewById(R.id.button2); //step3 : write add functionality. b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String f = et1.getText().toString(); int i = Integer.parseInt(f); String s = et2.getText().toString(); int j = Integer.parseInt(s); Integer result = i+j; String res = result.toString(); et3.setText(res); } }); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String f = et1.getText().toString(); int i = Integer.parseInt(f); String s = et2.getText().toString(); int j = Integer.parseInt(s); Integer result = i-j; String res = result.toString(); et3.setText(res); } }); } @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; } }
xml layout file for First Activity
File name: activity_main.xml
<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=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:ems="10"
android:inputType="textNoSuggestions"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:text="Add" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/editText2"
android:text="Sub" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
Download complete code : Click to download
Similar basic activity programs:
How to start an activity and pass data to second screen.
changing edittext text from button click listener
How to start an activity from main activity on clicking button, and pass data
Starting second activity based on login and password details
Getting results from child activity
Tags : android calculator program, android tutorial, calculator
nice
ReplyDeleteThanks for your clear explanation. easy to understand
ReplyDeletenice
ReplyDeleteI am a beginner in android development and I am Trying to run AVD but my AVD is not running please can any one help me out..
ReplyDelete@rajkumar better u download gennymotion for AVD.
ReplyDelete