Create a Mobile Calculator - Android Studio

Calculator Mobile Application - Android Studio

1. Open Android Studio and create a new project.

2. Choose "Empty Activity" as the project template.

3. Choose a suitable project name and select a package name.

4. Once the project is created, open the "activity_main.xml" file from the "res/layout" folder.

5. Drag and drop the following components onto the screen:

    a. TextView component (for displaying the result)

    b. Button components (for the calculator keys)

6. Set the properties of the components as per your requirements.

7. Open the "MainActivity.java" file from the "java/com.example.yourprojectname" folder.

8. Add the following code in the "onCreate" method to initialize the components:

 

TextView resultTextView = findViewById(R.id.result_text_view);
Button zeroButton = findViewById(R.id.zero_button);
Button oneButton = findViewById(R.id.one_button);
Button twoButton = findViewById(R.id.two_button);
Button threeButton = findViewById(R.id.three_button);
Button fourButton = findViewById(R.id.four_button);
Button fiveButton = findViewById(R.id.five_button);
Button sixButton = findViewById(R.id.six_button);
Button sevenButton = findViewById(R.id.seven_button);
Button eightButton = findViewById(R.id.eight_button);
Button nineButton = findViewById(R.id.nine_button);
Button addButton = findViewById(R.id.add_button);
Button subtractButton = findViewById(R.id.subtract_button);
Button multiplyButton = findViewById(R.id.multiply_button);
Button divideButton = findViewById(R.id.divide_button);
Button clearButton = findViewById(R.id.clear_button);
Button equalsButton = findViewById(R.id.equals_button);


9. Add the following code in the "onClick" method to perform the calculator operations:


String expression = resultTextView.getText().toString();

switch (view.getId()) {
    case R.id.zero_button:
        expression += "0";
        break;
    case R.id.one_button:
        expression += "1";
        break;
    case R.id.two_button:
        expression += "2";
        break;
    case R.id.three_button:
        expression += "3";
        break;
    case R.id.four_button:
        expression += "4";
        break;
    case R.id.five_button:
        expression += "5";
        break;
    case R.id.six_button:
        expression += "6";
        break;
    case R.id.seven_button:
        expression += "7";
        break;
    case R.id.eight_button:
        expression += "8";
        break;
    case R.id.nine_button:
        expression += "9";
        break;
    case R.id.add_button:
        expression += "+";
        break;
    case R.id.subtract_button:
        expression += "-";
        break;
    case R.id.multiply_button:
        expression += "*";
        break;
    case R.id.divide_button:
        expression += "/";
        break;
    case R.id.clear_button:
        expression = "";
        break;
    case R.id.equals_button:
        try {
            String result = String.valueOf(new ExpressionBuilder(expression).build().evaluate());
            expression = result;
        } catch (Exception e) {
            expression = "Error";
        }
        break;
}

resultTextView.setText(expression);


10. Save the project and run the app on an emulator or a physical device.

Comments

Popular posts from this blog

Blogger Post via .NET Program