Make a calculator apk code for me im 0 knowledgeable in coding
- User
Make a calculator apk code for me im 0 knowledgeable in coding
- Assistant
As an AI language model, I am unable to create the entire APK file for you, but I can help you build a simple calculator app using the Android Studio and Java as the programming language. Here's a step-by-step guide on how to create a simple calculator app:
- Install Android Studio:
If you haven't installed Android Studio already, you can download and install it from the following link: https://developer.android.com/studio
- Create a new project:
- Open Android Studio and click on "Create New Project"
- Select "Empty Activity" and click on "Next"
- Write a name for your app, such as "CalculatorApp", and click on "Finish"
- Add elements to the layout:
Open the
res/layout/activity_main.xmlfile in the project and replace the existing code with the following:<?xml version="1.0" encoding="utf-8"?> <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="match_parent" android:orientation="vertical" android:weightSum="5" tools:context=".MainActivity"> <EditText android:id="@+id/etInput" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:inputType="numberDecimal" android:gravity="end|center_vertical" android:textSize="40sp" android:hint="Enter Value"/> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" android:weightSum="4"> <Button android:id="@+id/btnAdd" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="Add" android:textSize="20sp"/> <Button android:id="@+id/btnSubtract" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="Subtract" android:textSize="20sp"/> <Button android:id="@+id/btnMultiply" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="Multiply" android:textSize="20sp"/> <Button android:id="@+id/btnDivide" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="Divide" android:textSize="20sp"/> </LinearLayout> <TextView android:id="@+id/tvResult" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:gravity="end|center_vertical" android:textSize="80sp" android:text="0"/> </LinearLayout>- Add logic to the app:
Open the
MainActivity.javafile in the project, and replace the existing code with the following:package com.example.calculatorapp; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { EditText etInput; Button btnAdd, btnSubtract, btnMultiply, btnDivide; TextView tvResult; double currentValue, result; String currentOperation = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etInput = findViewById(R.id.etInput); btnAdd = findViewById(R.id.btnAdd); btnSubtract = findViewById(R.id.btnSubtract); btnMultiply = findViewById(R.id.btnMultiply); btnDivide = findViewById(R.id.btnDivide); tvResult = findViewById(R.id.tvResult); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { performCalculation('+'); } }); btnSubtract.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { performCalculation('-'); } }); btnMultiply.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { performCalculation('*'); } }); btnDivide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { performCalculation('/'); } }); } private void performCalculation(char operation) { if (!etInput.getText().toString().equals("")) { currentValue = Double.parseDouble(etInput.getText().toString()); etInput.setText(""); switch (operation) { case '+': result += currentValue; break; case '-': result -= currentValue; break; case '*': result *= currentValue; break; case '/': if (currentValue != 0) { result /= currentValue; } else { tvResult.setText("Error"); return; } break; } tvResult.setText(String.valueOf(result)); currentValue = 0; } } }- Build and run your app:
In the Android Studio toolbar, click the green triangle (Run) button to run your app on the emulator or an Android device.
This simple calculator app will allow you to perform addition, subtraction, multiplication, and division on numeric values. Note that this sample code is for educational purposes only and is not intended for production use.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.