Basic Android App to add 2 numbers code| Android Studio

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    
>

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
>
        <TextView
            android:id="@+id/result"
            android:layout_height="25pt"
            android:textSize="14pt"
            android:padding="4pt"
            android:gravity="right"
            android:text="@string/result_default"
            
/>
        <View
            android:layout_height="2dip"
            android:background="#FF909090" 
/>

        <TableRow
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginTop="5pt"
            android:id="@+id/tableRow2"
            android:paddingLeft="4pt"
            android:paddingRight="4pt"
>
            <Button
                android:text="@string/buttonSeven"
                android:textSize="14pt"
                android:id="@+id/button7"
                android:layout_width="25pt"
                android:layout_height="25pt"
                android:layout_weight="1"
                android:onClick="handleClick" 
/>
            <Button
                android:text="@string/buttonEight"
                android:textSize="14pt"
                android:id="@+id/button8"
                android:layout_height="25pt"
                android:layout_width="25pt"
                android:layout_weight="1"
                android:onClick="handleClick" 
/>
            <Button
                android:text="@string/buttonNine"
                android:textSize="14pt"
                android:id="@+id/button9"
                android:layout_width="25pt"
                android:layout_height="25pt"
                android:layout_weight="1"
                android:onClick="handleClick" 
/>

        </TableRow>

        <TableRow
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginTop="1pt"
            android:id="@+id/tableRow2"
            android:paddingLeft="4pt"
            android:paddingRight="4pt"
>
            <Button
                android:text="@string/buttonFour"
                android:textSize="14pt"
                android:id="@+id/button4"
                android:layout_width="25pt"
                android:layout_height="25pt"
                android:layout_weight="1"
                android:onClick="handleClick" 
/>
            <Button
                android:text="@string/buttonFive"
                android:textSize="14pt"
                android:id="@+id/button5"
                android:layout_height="25pt"
                android:layout_width="25pt"
                android:layout_weight="1"
                android:onClick="handleClick" 
/>
            <Button
                android:text="@string/buttonSix"
                android:textSize="14pt"
                android:id="@+id/button6"
                android:layout_width="25pt"
                android:layout_height="25pt"
                android:layout_weight="1"
                android:onClick="handleClick" 
/>

        </TableRow>

        <TableRow
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginTop="1pt"
            android:id="@+id/tableRow2"
            android:paddingLeft="4pt"
            android:paddingRight="4pt"
>
            <Button
                android:text="@string/buttonOne"
                android:textSize="14pt"
                android:id="@+id/button1"
                android:layout_width="25pt"
                android:layout_height="25pt"
                android:layout_weight="1"
                android:onClick="handleClick" 
/>
            <Button
                android:text="@string/buttonTwo"
                android:textSize="14pt"
                android:id="@+id/button2"
                android:layout_height="25pt"
                android:layout_width="25pt"
                android:layout_weight="1"
                android:onClick="handleClick" 
/>
            <Button
                android:text="@string/buttonThree"
                android:textSize="14pt"
                android:id="@+id/button3"
                android:layout_width="25pt"
                android:layout_height="25pt"
                android:layout_weight="1"
                android:onClick="handleClick" 
/>

        </TableRow>

        <TableRow
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginTop="1pt"
            android:id="@+id/tableRow2"
            android:paddingLeft="4pt"
            android:paddingRight="4pt"
>
            <Button
                android:text="@string/buttonClear"
                android:textSize="14pt"
                android:id="@+id/buttonClear"
                android:layout_width="25pt"
                android:layout_height="25pt"
                android:layout_weight="1"
                android:background="#F08080"
                android:onClick="handleClick" 
/>
            <Button
                android:text="@string/buttonZero"
                android:textSize="14pt"
                android:id="@+id/button0"
                android:layout_height="25pt"
                android:layout_width="25pt"
                android:layout_weight="1"
                android:onClick="handleClick" 
/>
            <Button
                android:text="@string/buttonResult"
                android:textSize="14pt"
                android:id="@+id/buttonResult"
                android:layout_width="25pt"
                android:layout_height="25pt"
                android:layout_weight="1"
                android:background="#F08080"
                android:onClick="handleClick" 
/>
            <Button
                android:text="@string/buttonPlus"
                android:textSize="14pt"
                android:id="@+id/buttonPlus"
                android:layout_width="25pt"
                android:layout_height="25pt"
                android:layout_weight="1"
                android:background="#F08080"
                android:onClick="handleClick" 
/>
        </TableRow>
    </TableLayout>
</LinearLayout>

strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Basic calculator</string>
    <string name="app_name">Basic Calculator </string>

    <string name="result_default">0</string>
<string name="buttonPlus">+</string>
<string name="buttonClear">C</string>
    <string name="buttonResult">=</string>
 <string name="buttonZero">0</string>
    <string name="buttonOne">1</string>
    <string name="buttonTwo">2</string>
    <string name="buttonThree">3</string>

<string name="buttonFour">4</string>
  <string name="buttonFive">5</string>
    <string name="buttonSix">6</string>
   <string name="buttonSeven">7</string>
    <string name="buttonEight">8</string>
    <string name="buttonNine">9</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dell.add"
>


    android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="28" 
/>

        <application
            android:allowBackup="true"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" 
>
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" 
>
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>


MainActivity.java


package com.example.dell.add;

import java.util.HashSet;
import java.util.Set;

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 {

    private TextView result;

    private String operand;

    private String operator;

    private Set<String> numbers;

    private Set<String> operators;

    /** Called when the activity is first created. */
    
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        result = (TextView) findViewById(R.id.result);
    }

    /**
   calculator has 0 to 9 numbers
     */
    
private void initNumbers() {
        numbers new HashSet<String>();
        for (int i = 0; i < 10; i++) {
            numbers.add(Integer.toString(i));
        }
    }

    /**
     * + operator
     */
    
private void initOperators() {
        operators new HashSet<String>();
        String[] ops = { "+" };
        for (String operator : ops) {
            operators.add(operator);
        }
    }

    /**
     * Button click event handler.

     */
    
public void handleClick(View view) {
        Button clicked = (Button) view;
        String value = clicked.getText().toString();

        if (isNumerical(value)) {
            if (!isDefaultResult(result.getText().toString())) {
                value = result.getText().toString() + value;
            }
        } else if (isOperator(value)) {
            operand result.getText().toString();
            operator = value;
        } else if (isClear(value)) {
            value = getString(R.string.result_default);
        } else {
            double a = Double.parseDouble(operand), b = Double
                    .parseDouble(result.getText().toString());

            if (operator.equals("+")) {
                value = Double.toString(a + b);
            }

            // Reset values.
            
operator null;
            operand null;
        }

        result.setText(value);
    }

    /**
     * Test if value is the same as clear button's.

     */
    
private boolean isClear(String value) {
        return value.equals(getString(R.string.buttonClear));
    }

    /**
     * Test if value is operator.

     */
    
private boolean isOperator(String value) {
        if (operators == null) {
            initOperators();
        }
        return operators.contains(value);
    }

    /**
     * Test if result was modified.

     */
    
private boolean isDefaultResult(String value) {
        return value.equals(getString(R.string.result_default));
    }

    /**
     * Test if value is numerical.
     *
     */
    
private boolean isNumerical(String value) {
        if (numbers == null) {
            initNumbers();
        }
        return numbers.contains(value);
    }

}

Output


Basic Android App to add 2 numbersBasic Android App to add 2 numbers code| Android Studio harsha navalkar blog daily eexperience
Basic Android App to add 2 numbers
Engineerin blogs CLICK HERE
Basic Android App to add 2 numbers code| Android Studio