Note:This calculator simply performs very basic additon subtraction multiplication and division, The data type is limited only to integer, It perorms well even though there little errors on data validation part.
<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="wrap_content" android:layout_weight="1" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:layout_weight="1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"> <Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="7" android:onClick="btnNumOnClick" /> <Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="8" android:onClick="btnNumOnClick" /> <Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="9" android:onClick="btnNumOnClick" />
<Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="/" android:onClick="btnOperators" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"> <Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="4" android:onClick="btnNumOnClick" /> <Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="5" android:onClick="btnNumOnClick" /> <Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="6" android:onClick="btnNumOnClick" /> <Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="*" android:onClick="btnOperators" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="1" android:onClick="btnNumOnClick" /> <Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="2" android:onClick="btnNumOnClick" /> <Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="3" android:onClick="btnNumOnClick" /> <Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="-" android:onClick="btnOperators" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="0" android:onClick="btnNumOnClick" /> <Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="."
android:id="@+id/button2" /> <Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="="
android:id="@+id/button3"
android:onClick="btnEqualClick" />
<Button android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/button4"
android:onClick="btnOperators" />
</LinearLayout> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back space"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:onClick="onBackSpaceClick" /> </LinearLayout>
Here Basic concepts of string manipulation is used in MainActivity.java,
The Code is as follows
import android.app.Activity;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import java.util.ArrayList;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast; public class MainActivity extends Activity { String strTemp1,strTemp2;@Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);} public void btnNumOnClick(View v) { Button button=(Button) v;strTemp1=button.getText().toString(); if(strTemp2==null) { strTemp2=strTemp1+""; } else { strTemp2=strTemp2+strTemp1; } TextView et=(TextView) findViewById(R.id.editText2);et.setText(strTemp2); } public void btnOperators(View v) { strTemp2=null;strTemp1=null;Button button=(Button) v;strTemp1=button.getText().toString();TextView view=(TextView) findViewById(R.id.editText2);strTemp2=view.getText().toString();strTemp2=strTemp2+strTemp1;TextView et=(TextView) findViewById(R.id.editText2);et.setText(strTemp2); } public void btnEqualClick(View v) { if (strTemp2.contains("+")) { Calculate(strTemp2,"+" ); } else if(strTemp2.contains("-")) { Calculate(strTemp2,"-"); } else if(strTemp2.contains("*")) { Calculate(strTemp2,"*"); } else if(strTemp2.contains("/")) { Calculate(strTemp2,"/" ) ; } } public void Calculate(String strTemp2,String Sign) { int index1=strTemp2.indexOf(Sign);int index2=strTemp2.length();String firstValue;String secondValue;int Result;TextView tv=(TextView) findViewById(R.id.editText1);firstValue=strTemp2.substring(0,index1);secondValue=strTemp2.substring(index1+1,index2);int FirstValue= Integer.parseInt(firstValue);int SecondValue=Integer.parseInt(secondValue); switch (Sign) { case "+": Result=FirstValue+SecondValue;tv.setText(String.valueOf(Result));break;case "-":Result=FirstValue-SecondValue;tv.setText(String.valueOf(Result));break;case "*": Result=FirstValue*SecondValue;tv.setText(String.valueOf(Result));break;case "/": Result=FirstValue/SecondValue;tv.setText(String.valueOf(Result));break;} } public void onBackSpaceClick(View v) { strTemp2=strTemp2.replace(strTemp2.substring(strTemp2.length()-1),"") ;TextView et=(TextView) findViewById(R.id.editText2);et.setText(strTemp2); } }
No comments:
Post a Comment