Skip to content

Releases: busaud/android.calculator

calculator

11 Aug 14:10
Compare
Choose a tag to compare

package goldenapple.calculator;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity
{
@OverRide
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    //bn = Button Number
    Button bn1 = (Button) findViewById(R.id.Bn1);
    Button bn2 = (Button) findViewById(R.id.Bn2);
    Button bn3 = (Button) findViewById(R.id.Bn3);
    Button bn4 = (Button) findViewById(R.id.Bn4);
    Button bn5 = (Button) findViewById(R.id.Bn5);
    Button bn6 = (Button) findViewById(R.id.Bn6);
    Button bn7 = (Button) findViewById(R.id.Bn7);
    Button bn8 = (Button) findViewById(R.id.Bn8);
    Button bn9 = (Button) findViewById(R.id.Bn9);
    Button bn0 = (Button) findViewById(R.id.Bn0);
    //ba = Button Action
    Button ba1 = (Button) findViewById(R.id.Ba1);
    Button ba2 = (Button) findViewById(R.id.Ba2);
    Button ba3 = (Button) findViewById(R.id.Ba3);
    Button ba4 = (Button) findViewById(R.id.Ba4);
    Button ba5 = (Button) findViewById(R.id.Ba5);
    Button bac = (Button) findViewById(R.id.Bac);

    final TextView tv1 = (TextView) findViewById (R.id.TV1); 
    // مربع الأرقام المدخلة
    final TextView tv2 = (TextView) findViewById (R.id.TV2);
    // المربع اسفل الصفحة
    // وضعته فقط لاستطيع تكرار الأرقام في مربع الإدخال
    final TextView tv3 = (TextView) findViewById (R.id.TV3);
    // مربع الرقم المنتظر
    final TextView tv4 = (TextView) findViewById (R.id.TV4);
    // مربع العملية الحسابية

    // تصفير الخانات
    tv1.setText("");
    tv2.setText("");


    bn1.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            tv2.setText(tv1.getText());
            tv1.setText(tv2.getText() + "1");
        }
    });
    bn2.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            tv2.setText(tv1.getText());
            tv1.setText(tv2.getText() + "2");
        }
    });
    bn3.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            tv2.setText(tv1.getText());
            tv1.setText(tv2.getText() + "3");
        }
    });
    bn4.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            tv2.setText(tv1.getText());
            tv1.setText(tv2.getText() + "4");
        }
    });
    bn5.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            tv2.setText(tv1.getText());
            tv1.setText(tv2.getText() + "5");
        }
    });
    bn6.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            tv2.setText(tv1.getText());
            tv1.setText(tv2.getText() + "6");
        }
    });
    bn7.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            tv2.setText(tv1.getText());
            tv1.setText(tv2.getText() + "7");
        }
    });
    bn8.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            tv2.setText(tv1.getText());
            tv1.setText(tv2.getText() + "8");
        }
    });
    bn9.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            tv2.setText(tv1.getText());
            tv1.setText(tv2.getText() + "9");
        }
    });
    bn0.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            tv2.setText(tv1.getText());
            tv1.setText(tv2.getText() + "0");
        }
    });
    ba1.setOnClickListener(new View.OnClickListener()
    {   
        @Override
        public void onClick(View v) 
        {
            tv3.setText(tv1.getText());
            tv4.setText("+");
            tv1.setText("");
        }
    });    
    ba2.setOnClickListener(new View.OnClickListener()
    {   
        @Override
        public void onClick(View v) 
        {
            tv3.setText(tv1.getText());
            tv4.setText("-");
            tv1.setText("");
        }
    }); 
    ba3.setOnClickListener(new View.OnClickListener()
    {   
        @Override
        public void onClick(View v) 
        {
            tv3.setText(tv1.getText());
            tv4.setText("×");
            tv1.setText("");
        }
    });  
    ba4.setOnClickListener(new View.OnClickListener()
    {   
        @Override
        public void onClick(View v) 
        {
            tv3.setText(tv1.getText());
            tv4.setText("÷");
            tv1.setText("");
        }
    });  
    ba5.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            double a = Double.parseDouble(tv3.getText().toString());
            double b = Double.parseDouble(tv1.getText().toString());
            double c;
            String d;
            String e = tv4.getText().toString();

            switch (e) {
            case "+":
                c = a + b;
                d = Double.toString(c);

                tv1.setText(d);
                tv2.setText("");
                tv3.setText("");
                tv4.setText("=");
                break;

            case "-":
                c = a - b;
                d = Double.toString(c);

                tv1.setText(d);
                tv2.setText("");
                tv3.setText("");
                tv4.setText("=");
                break;

            case "×":
                c = a * b;
                d = Double.toString(c);

                tv1.setText(d);
                tv2.setText("");
                tv3.setText("");
                tv4.setText("=");
                break;

            case "÷":
                c = a / b;
                d = Double.toString(c);

                tv1.setText(d);
                tv2.setText("");
                tv3.setText("");
                tv4.setText("=");
                break;

            default:
                tv1.setText("Err");
                tv2.setText("");
                tv3.setText("");
                tv4.setText("=");
                break;
            }
        }
    });

    bac.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            tv1.setText("");
            tv2.setText("");
            tv3.setText("");
            tv4.setText("");
        }
    });

}

protected void setIntent(int i) {
    // TODO Auto-generated method stub

}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}