How to implement Calculator Layout and Coding in Android.
How to implement Calculator Layout and Coding in Android.
100% Working Code and Projects.
if you need any help about project and Application Contact Shelly at:+917889163814.See Code Below and Download Project Free.
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button0;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;
Button button9;
Button buttonAdd;
Button buttonSubstract;
Button buttonMul;
Button buttonDiv;
Button buttonClear;
Button buttonEqual;
String result;
String tmp;
String operator;
TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControl();
initControlListener();
}
private void initControlListener() {
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("9");
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClearButtonClicked();
}
});
buttonSubstract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
onOperatorButtonClicked("-");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOperatorButtonClicked("+");
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOperatorButtonClicked("X");
}
});
buttonDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOperatorButtonClicked("/");
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onEqualButtonClicked();
}
});
}
private void onEqualButtonClicked() {
int res = 0;
try {
int number = Integer.valueOf(tmp);
int number2 = Integer.valueOf(resultTextView.getText().toString());
switch (operator) {
case "+":
res = number + number2;
break;
case "/":
res = number / number2;
break;
case "-":
res = number - number2;
break;
case "X":
res = number * number2;
break;
}
result = String.valueOf(res);
resultTextView.setText(result);
}
catch (Exception e) {
e.printStackTrace();
}
}
private void onOperatorButtonClicked(String operator) {
tmp = resultTextView.getText().toString();
onClearButtonClicked();
this.operator = operator;
}
private void onClearButtonClicked() {
result = "";
resultTextView.setText("");
}
private void onNumberButtonClicked(String pos) {
result = resultTextView.getText().toString();
result = result + pos;
resultTextView.setText(result);
}
private void initControl() {
button0 = (Button)findViewById(R.id.button0);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);
button5 = (Button)findViewById(R.id.button5);
button6 = (Button)findViewById(R.id.button6);
button7 = (Button)findViewById(R.id.button7);
button8 = (Button)findViewById(R.id.button8);
button9 = (Button)findViewById(R.id.button9);
buttonAdd = (Button)findViewById(R.id.buttonAdd);
buttonClear = (Button)findViewById(R.id.buttonClear);
buttonSubstract = (Button)findViewById(R.id.buttonSub);
buttonMul = (Button)findViewById(R.id.buttonMul);
buttonDiv = (Button)findViewById(R.id.buttonDiv);
buttonEqual = (Button)findViewById(R.id.buttonEqual);
resultTextView = (TextView)findViewById(R.id.text_view_result);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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:background="#ffff00"
tools:context="luongvo.com.mycalculator.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_view_result"
android:textSize="60sp"
android:textAlignment="viewEnd"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:text="1"
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:id="@+id/button2"
android:text="2"
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:id="@+id/button3"
android:text="3"
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:id="@+id/buttonAdd"
android:text="+"
android:layout_weight="1"
android:textSize="20sp"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button4"
android:text="4"
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:id="@+id/button5"
android:text="5"
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:id="@+id/button6"
android:text="6"
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:id="@+id/buttonSub"
android:text="-"
android:layout_weight="1"
android:textSize="20sp"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button7"
android:text="7"
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:id="@+id/button8"
android:text="8"
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:id="@+id/button9"
android:text="9"
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:id="@+id/buttonMul"
android:text="*"
android:layout_weight="1"
android:textSize="20sp"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttonClear"
android:text="CLEAR"
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:id="@+id/button0"
android:text="0"
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:id="@+id/buttonEqual"
android:text="="
android:layout_weight="1"
android:textSize="20sp"/>
<Button
android:id="@+id/buttonDiv"
android:text="/"
android:layout_weight="1"
android:textSize="20sp"/>
</TableRow>
</TableLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="luongvo.com.mycalculator" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
Screenshot Given Below:
for Download click here:-https://drive.google.com/file/d/1_wc9ghxirz_5bjKrN8-9n13ZTsvM9Ilk/view?usp=sharing
Comments
Post a Comment