To create Basic Calculator App > Android App Development

To Develop Basic Calculator App > Android 

Mobile Computation and Computing

PROGRAM:

MainActivity.java



package com.example.root.pratdroid;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {


    EditText no1, no2;
    TextView result,symbol;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        no1 = (EditText) findViewById(R.id.editText);
        no2 = (EditText) findViewById(R.id.editText2);
        result = (TextView) findViewById(R.id.textView2);
        symbol = (TextView) findViewById(R.id.textView3);
    }
    public void onButtonClick(View v)
    {
        Double n1,n2,r=0.0;
        String sym="";
        n1=Double.parseDouble(no1.getText().toString());
        n2=Double.parseDouble(no2.getText().toString());
        switch (v.getId()){
            case R.id.button:
                r=n1+n2;
                sym="+";
                break;
            case R.id.button2:
                r=n1-n2;
                sym="-";
                break;
            case R.id.button3:
                r=n1*n2;
                sym="*";
                break;
            case R.id.button4:
                r=n1/n2;
                sym="/";
                break;
        }
        result.setText(r+"");
        symbol.setText(sym);
    }
}




activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.root.pratdroid.MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/textView" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText2"
        android:layout_above="@+id/textView2"
        android:layout_alignStart="@+id/editText" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView2"
        android:layout_above="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="42dp" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:id="@+id/button"
        android:onClick="onButtonClick"
        android:layout_above="@+id/button2"
        android:layout_alignStart="@+id/button3" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Subtract"
        android:id="@+id/button2"
        android:onClick="onButtonClick"
        android:layout_above="@+id/button3"
        android:layout_centerHorizontal="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Multiply"
        android:id="@+id/button3"
        android:onClick="onButtonClick"
        android:layout_above="@+id/button4"
        android:layout_alignEnd="@+id/button2" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Divide"
        android:id="@+id/button4"
        android:onClick="onButtonClick"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/button3"
        android:layout_marginBottom="42dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView3"
        android:layout_above="@+id/editText2"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Output:




Comments

Popular posts from this blog

Intermediate Code Generation > C Program