Quik Links

How to make caluculator app using android.?

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <android.support.constraint.ConstraintLayout  
   xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"  
   android:layout_height="match_parent">  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:orientation="vertical" >  
     <LinearLayout  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content" >  
       <TextView  
         android:id="@+id/textView1"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:text="Value 1"  
         android:textAppearance="?android:attr/textAppearanceLarge" />  
       <EditText  
         android:id="@+id/editText1"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_weight="1"  
         android:ems="10"  
         android:inputType="number" >  
         <requestFocus />  
       </EditText>  
     </LinearLayout>  
     <LinearLayout  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content" >  
       <TextView  
         android:id="@+id/textView2"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:text="Value2"  
         android:textAppearance="?android:attr/textAppearanceLarge" />  
       <EditText  
         android:id="@+id/editText2"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_weight="1"  
         android:ems="10"  
         android:inputType="number" />  
     </LinearLayout>  
     <LinearLayout  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content" >  
       <Button  
         android:id="@+id/button1"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:text="+" />  
       <Button  
         android:id="@+id/button2"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:text="-" />  
       <Button  
         android:id="@+id/button3"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:text="*" />  
       <Button  
         android:id="@+id/button4"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:text="/" />  
       <Button  
         android:id="@+id/button5"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:text="C" />  
     </LinearLayout>  
     <LinearLayout  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content" >  
       <TextView  
         android:id="@+id/textView3"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:text="Result"  
         android:textAppearance="?android:attr/textAppearanceLarge" />  
       <EditText  
         android:id="@+id/editText3"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_weight="1"  
         android:ems="10"  
         android:inputType="number" />  
     </LinearLayout>  
   </LinearLayout>  
 </android.support.constraint.ConstraintLayout>  

MainActivity.java

 package com.app.kapil.calculator;  
 import android.os.Bundle;  
 import android.support.v7.app.AppCompatActivity;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.EditText;  
 public class MainActivity extends AppCompatActivity {  
   Button b1,b2,b3,b4,b5;  
   EditText t1,t2,t3;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     b1=(Button) findViewById(R.id.button1);  
     b2=(Button) findViewById(R.id.button2);  
     b3=(Button) findViewById(R.id.button3);  
     b4=(Button) findViewById(R.id.button4);  
     b5=(Button) findViewById(R.id.button5);  
     t1=(EditText) findViewById(R.id.editText1);  
     t2=(EditText) findViewById(R.id.editText2);  
     t3=(EditText) findViewById(R.id.editText3);  
     b1.setOnClickListener(new A());  
     b2.setOnClickListener(new B());  
     b3.setOnClickListener(new C());  
     b4.setOnClickListener(new D());  
     b5.setOnClickListener(new E());  
   }  
   class A implements View.OnClickListener  
   {  
     @Override  
     public void onClick(View arg0) {  
       int a,b,ans;  
       a=Integer.parseInt(t1.getText().toString());  
       b=Integer.parseInt(t2.getText().toString());  
       ans=a+b;  
       t3.setText(""+ans);  
     }  
   }  
   class B implements View.OnClickListener  
   {  
     @Override  
     public void onClick(View arg0) {  
       int a,b,ans;  
       a=Integer.parseInt(t1.getText().toString());  
       b=Integer.parseInt(t2.getText().toString());  
       ans=a-b;  
       t3.setText(""+ans);  
     }  
   }  
   class C implements View.OnClickListener  
   {  
     @Override  
     public void onClick(View arg0) {  
       int a,b,ans;  
       a=Integer.parseInt(t1.getText().toString());  
       b=Integer.parseInt(t2.getText().toString());  
       ans=a*b;  
       t3.setText(""+ans);  
     }  
   }  
   class D implements View.OnClickListener  
   {  
     @Override  
     public void onClick(View arg0) {  
                try{  
                int a,b,ans;  
       a=Integer.parseInt(t1.getText().toString());  
       b=Integer.parseInt(t2.getText().toString());  
                ans=a/b;  
       t3.setText(""+ans);  
                }  
                catch(Exception e){  
        Toast.makeText(MainActivity.this,"Enter a number other than 0 "+e,Toast.LENGTH_LONG).show();  
                }  
     }  
   }  
   class E implements View.OnClickListener  
   {  
     @Override  
     public void onClick(View arg0) {  
       t1.setText("");  
       t2.setText("");  
       t3.setText("");  
       t1.requestFocus();  
     }  
   }  
 }  

Output



















I hope that helps :)

-Kapil Arora-

No comments:

Post a Comment