MainActivity.java
package kantapp.simplecalculator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button add,sub,mult,div,clear; EditText num1,num2; TextView result; int number1; int number2; int total; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); num1=(EditText)findViewById(R.id.num1); num2=(EditText)findViewById(R.id.num2); result = (TextView) findViewById(R.id.result); add = (Button) findViewById(R.id.add); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { number1 = Integer.parseInt(num1.getText().toString()); number2 = Integer.parseInt(num2.getText().toString()); total = number1 + number2; result.setText(Integer.toString(total)); } }); sub = (Button) findViewById(R.id.sub); sub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { number1 = Integer.parseInt(num1.getText().toString()); number2 = Integer.parseInt(num2.getText().toString()); total = number1 - number2; result.setText(Integer.toString(total)); } }); mult = (Button) findViewById(R.id.mult); mult.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { number1 = Integer.parseInt(num1.getText().toString()); number2 = Integer.parseInt(num2.getText().toString()); total = number1 * number2; result.setText(Integer.toString(total)); } }); div = (Button) findViewById(R.id.div); div.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { number1 = Integer.parseInt(num1.getText().toString()); number2 = Integer.parseInt(num2.getText().toString()); if(number2==0) { Toast.makeText(MainActivity.this, "Please Enter Other Number", Toast.LENGTH_SHORT).show(); } else { total = number1 / number2; result.setText(Integer.toString(total)); } } }); clear = (Button) findViewById(R.id.clear); clear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { num1.setText("0"); num2.setText("0"); } }); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" 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="kantapp.simplecalculator.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignEnd="@+id/num1" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_alignRight="@+id/num1" android:text="Enter Number 1 :" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/num1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_below="@+id/textView" android:text="0" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/num1" android:text="Enter Number 2 :" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/num2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:text="0" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" /> <Button android:id="@+id/sub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" /> <Button android:id="@+id/mult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*" /> <Button android:id="@+id/div" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" /> </LinearLayout> <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Clear" /> <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Result" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="kantapp.simplecalculator"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
No comments:
Post a Comment