Android Tutorial For Simple Calculator
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.asdapp.raghul_smart.simplecalculator.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="173dp"
android:layout_marginEnd="173dp"
android:layout_marginTop="30dp"
android:id="@+id/editText"
android:hint="1st Number" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="32dp"
android:id="@+id/editText2"
android:hint="2nd Number" />
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="40dp"
android:layout_marginStart="40dp"
android:id="@+id/button" />
<Button
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:layout_marginLeft="40dp"
android:layout_marginStart="40dp"
android:id="@+id/button2" />
</RelativeLayout>
MainActivity.java
package com.asdapp.raghul_smart.simplecalculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button b1,b2;
EditText e1,e2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.editText);
e2=(EditText)findViewById(R.id.editText2);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n1,n2,result;
n1=Integer.parseInt(e1.getText().toString());
n2=Integer.parseInt(e2.getText().toString());
result=n1+n2;
Toast.makeText(getBaseContext(),"Result is "+result,Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n1,n2,result;
n1=Integer.parseInt(e1.getText().toString());
n2=Integer.parseInt(e2.getText().toString());
result=n1-n2;
Toast.makeText(getBaseContext(),"Result is "+result,Toast.LENGTH_SHORT).show();
}
});
}
}
Youtube link https://youtu.be/I49xqHFDixw
Comments
Post a Comment