JAVA CODE:
Screen shots |
package com.infoocode.sonycalculator;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by INFOOCODE on 8/15/2015.
*/
public class Button_Calculator extends Activity implements View.OnClickListener {
Button one,two,three,four,five,six,seven,eight,nine,xero,point,equal,add,subtract,multiply,div,reset,clearbutton;
TextView result,selection;
Double firstnum, secondnum, show;
int plusclicked, minusclicked, divideclicked, multiplyclicked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_calculator);
one=(Button)findViewById(R.id.button1);
one.setText("1");
two=(Button)findViewById(R.id.button2);
two.setText("2");
three=(Button)findViewById(R.id.button3);
three.setText("3");
four=(Button)findViewById(R.id.button4);
four.setText("4");
five=(Button)findViewById(R.id.button5);
five.setText("5");
six=(Button)findViewById(R.id.button6);
six.setText("6");
seven=(Button)findViewById(R.id.button7);
seven.setText("7");
eight=(Button)findViewById(R.id.button8);
eight.setText("8");
nine=(Button)findViewById(R.id.button9);
nine.setText("9");
point=(Button)findViewById(R.id.pointbutton);
xero=(Button)findViewById(R.id.zerobutton);
xero.setText("0");
add=(Button)findViewById(R.id.addbutton);
subtract=(Button)findViewById(R.id.subtractbutton);
div=(Button)findViewById(R.id.dividebutton);
multiply=(Button)findViewById(R.id.multiplybutton);
equal=(Button)findViewById(R.id.equalbutton);
reset=(Button)findViewById(R.id.resetbutton);
clearbutton=(Button)findViewById(R.id.clearbutton);
result=(TextView)findViewById(R.id.rsulttxtvew);
selection=(TextView)findViewById(R.id.selectiontxtvew);
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
six.setOnClickListener(this);
seven.setOnClickListener(this);
eight.setOnClickListener(this);
nine.setOnClickListener(this);
xero.setOnClickListener(this);
point.setOnClickListener(this);
add.setOnClickListener(this);
subtract.setOnClickListener(this);
multiply.setOnClickListener(this);
div.setOnClickListener(this);
equal.setOnClickListener(this);
reset.setOnClickListener(this);
clearbutton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_choice) {
Intent intent = new Intent(this,approvel.class);
startActivity(intent);
return true;
}
else if (id == R.id.action_exit) {
Toast.makeText(this, "Calculator Exit", Toast.LENGTH_SHORT).show();
finishAffinity();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
int id=v.getId();
if(id==R.id.button1){
selection.setText(selection.getText() + one.getText().toString());
selection.setTextSize(25);
}
else if(id==R.id.button2){
selection.setText(selection.getText()+two.getText().toString());
selection.setTextSize(25);
}
else if(id==R.id.button3){
selection.setText(selection.getText() + three.getText().toString());
selection.setTextSize(25);
}
else if(id==R.id.button4){
selection.setText(selection.getText() + four.getText().toString());
selection.setTextSize(25);
}
else if(id==R.id.button5){
selection.setText(selection.getText() + five.getText().toString());
selection.setTextSize(25);
}
else if(id==R.id.button6){
selection.setText(selection.getText() + six.getText().toString());
selection.setTextSize(25);
}
else if(id==R.id.button7){
selection.setText(selection.getText() + seven.getText().toString());
selection.setTextSize(25);
}
else if(id==R.id.button8){
selection.setText(selection.getText() + eight.getText().toString());
selection.setTextSize(25);
}
else if(id==R.id.button9){
selection.setText(selection.getText() + nine.getText().toString());
selection.setTextSize(25);
}
else if(id==R.id.zerobutton){
selection.setText(selection.getText() + xero.getText().toString());
selection.setTextSize(25);
}
else if(id==R.id.pointbutton){
selection.setText(selection.getText() +".");
}
if(selection.getText().length()>0) {
if (id == R.id.addbutton) {
firstnum = Double.parseDouble(selection.getText().toString());
selection.setText("");
plusclicked = 1;
minusclicked = divideclicked = multiplyclicked = 0;
Toast.makeText(this, "Selected operator : +", Toast.LENGTH_SHORT).show();
} else if (id == R.id.subtractbutton) {
firstnum = Double.parseDouble(selection.getText().toString());
selection.setText("");
minusclicked = 1;
plusclicked = divideclicked = multiplyclicked = 0;
Toast.makeText(this, "Selected operator : -", Toast.LENGTH_SHORT).show();
} else if (id == R.id.multiplybutton) {
firstnum = Double.parseDouble(selection.getText().toString());
selection.setText("");
multiplyclicked = 1;
plusclicked = divideclicked = minusclicked = 0;
Toast.makeText(this, "Selected operator : *", Toast.LENGTH_SHORT).show();
} else if (id == R.id.dividebutton) {
firstnum = Double.parseDouble(selection.getText().toString());
selection.setText("");
divideclicked = 1;
plusclicked = multiplyclicked = minusclicked = 0;
Toast.makeText(this, "Selected operator : /", Toast.LENGTH_SHORT).show();
}
if (id == R.id.equalbutton) {
secondnum = Double.parseDouble(selection.getText().toString());
if (plusclicked > 0) {
show = firstnum + secondnum;
result.setText(firstnum+" + "+secondnum+" = " + show);
result.setTextSize(19);
result.setTextColor(Color.BLUE);
selection.setText("");
selection.setTextSize(18);
Toast.makeText(this, "Check result above", Toast.LENGTH_SHORT).show();
}
else if (minusclicked > 0) {
show = firstnum - secondnum;
result.setText(firstnum+" - "+secondnum+" = " + show);
result.setTextSize(19);
result.setTextColor(Color.BLUE);
selection.setText("");
selection.setTextSize(18);
Toast.makeText(this, "Check result above", Toast.LENGTH_SHORT).show();
}
else if (multiplyclicked > 0) {
show = firstnum * secondnum;
result.setText(firstnum+" * "+secondnum+" = " + show);
result.setTextSize(19);
result.setTextColor(Color.BLUE);
selection.setText("");
selection.setTextSize(18);
Toast.makeText(this, "Check result above", Toast.LENGTH_SHORT).show();
}
else if (divideclicked > 0) {
show = firstnum / secondnum;
result.setText(firstnum+" / "+secondnum+" = " + show);
result.setTextSize(19);
result.setTextColor(Color.BLUE);
selection.setText("");
selection.setTextSize(18);
Toast.makeText(this, "Check result above", Toast.LENGTH_SHORT).show();
}
}
else if(id==R.id.clearbutton){
String text;
text=selection.getText().toString();
int lenght = text.length();
text=text.substring(0,lenght-1);
selection.setText(text);
}
}
else if(selection.getText().length()<1){
if (id == R.id.addbutton) {
Toast.makeText(this, "Please, Enter the Numbers to Perform an Action.", Toast.LENGTH_SHORT).show();
}
else if (id == R.id.subtractbutton) {
Toast.makeText(this, "Please, Enter the Numbers to Perform an Action.", Toast.LENGTH_SHORT).show();
}
else if (id == R.id.multiplybutton) {
Toast.makeText(this, "Please, Enter the Numbers to Perform an Action.", Toast.LENGTH_SHORT).show();
}
else if (id == R.id.dividebutton) {
Toast.makeText(this, "Please, Enter the Numbers to Perform an Action.", Toast.LENGTH_SHORT).show();
}
else if (id == R.id.equalbutton) {
Toast.makeText(this, "Please, Enter the Numbers and Operators to Perform an Action.", Toast.LENGTH_SHORT).show();
}
else if(id==R.id.clearbutton){
Toast.makeText(this, "Nothing to Clear", Toast.LENGTH_SHORT).show();
}
}
if(id==R.id.resetbutton){
result.setText("Your Result will be HERE");
result.setTextSize(15);
result.setTextColor(Color.BLACK);
selection.setText("");
selection.setTextSize(18);
Toast.makeText(this, "Calculator Reset", Toast.LENGTH_SHORT).show();
}
}
}
XML CODE:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:contest=".Login"
android:background="@drawable/calcu">
<TextView
android:layout_width="match_parent"
android:layout_height="90dp"
android:text="Your Result will be HERE"
android:id="@+id/rsulttxtvew"
android:gravity="center"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:layout_width="75dp"
android:layout_height="55dp"
android:text="1"
android:id="@+id/button1"
android:textStyle="italic"
android:textSize="23dp"
android:background="@drawable/onebutton"
/>
<Button
android:layout_width="75dp"
android:layout_height="55dp"
android:text="2"
android:id="@+id/button2"
android:textStyle="italic"
android:textSize="23dp"
android:background="@drawable/onebutton"
/>
<Button
android:layout_width="75dp"
android:layout_height="55dp"
android:text="3"
android:id="@+id/button3"
android:textStyle="italic"
android:textSize="23dp"
android:background="@drawable/onebutton"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+"
android:textSize="23dp"
android:id="@+id/addbutton"
android:textStyle="bold"
android:background="@drawable/custome_button"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="75dp"
android:layout_height="55dp"
android:text="4"
android:id="@+id/button4"
android:textStyle="italic"
android:textSize="23dp"
android:background="@drawable/onebutton"
/>
<Button
android:layout_width="75dp"
android:layout_height="55dp"
android:text="5"
android:id="@+id/button5"
android:textStyle="italic"
android:textSize="23dp"
android:background="@drawable/onebutton"
/>
<Button
android:layout_width="75dp"
android:layout_height="55dp"
android:text="6"
android:id="@+id/button6"
android:textStyle="italic"
android:textSize="23dp"
android:background="@drawable/onebutton"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="*"
android:textSize="23dp"
android:id="@+id/multiplybutton"
android:textStyle="bold"
android:background="@drawable/custome_button"
android:layout_weight="0.22"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="75dp"
android:layout_height="55dp"
android:text="7"
android:id="@+id/button7"
android:textStyle="italic"
android:textSize="23dp"
android:background="@drawable/onebutton"
/>
<Button
android:layout_width="75dp"
android:layout_height="55dp"
android:text="8"
android:id="@+id/button8"
android:textStyle="italic"
android:textSize="23dp"
android:background="@drawable/onebutton"
/>
<Button
android:layout_width="75dp"
android:layout_height="55dp"
android:text="9"
android:id="@+id/button9"
android:textStyle="italic"
android:textSize="23dp"
android:background="@drawable/onebutton"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-"
android:textSize="33dp"
android:id="@+id/subtractbutton"
android:textStyle="bold"
android:background="@drawable/custome_button"
android:layout_weight="0.22"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:layout_width="wrap_content"
android:layout_height="55dp"
android:text="."
android:id="@+id/pointbutton"
android:textStyle="italic"
android:textSize="23dp"
android:background="@drawable/onebutton"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/zerobutton"
android:textStyle="italic"
android:textSize="23dp"
android:background="@drawable/onebutton"
android:layout_weight="0.78"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textSize="23dp"
android:id="@+id/dividebutton"
android:textStyle="bold"
android:background="@drawable/custome_button"
android:layout_weight="0.22"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:layout_width="75dp"
android:layout_height="55dp"
android:text="C"
android:id="@+id/resetbutton"
android:textSize="33dp"
android:gravity="center"
android:background="@drawable/rbutton"
android:textColor="#ffffff" />
<Button
android:layout_width="185dp"
android:layout_height="55dp"
android:text="="
android:id="@+id/equalbutton"
android:textStyle="bold"
android:textSize="33dp"
android:background="@drawable/custome_button"
android:textColor="#ffffff"
android:layout_weight="0.5" />
<Button
android:layout_width="120dp"
android:layout_height="55dp"
android:text="<--"
android:id="@+id/clearbutton"
android:textSize="33dp"
android:gravity="center"
android:background="@drawable/clearbutton"
android:textColor="#ffffff"
android:layout_weight="0.5"/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="85dp"
android:hint="Your Selection is Here"
android:textSize="20dp"
android:gravity="center"
android:id="@+id/selectiontxtvew"
android:layout_below="@+id/equalbutton"
android:layout_centerHorizontal="true" />
</LinearLayout>
</ScrollView>
No comments:
Post a Comment