Friday, 4 November 2016

Calculator in Android Studio



Calculator in Android Studio
In this project i will tell you how to create calculator app in Android Studio.
Firstly Create new Project in Android Studio and Add these Line in
 activity_main.xml
Course-specific creative-The Complete Android Developer Course - Build 14 Apps  
<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" tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/tvResult"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Number1"
        android:id="@+id/tvNumber1"
        android:layout_below="@+id/tvResult"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/etEnterNum1"
        android:layout_below="@+id/tvNumber1"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Number2"
        android:id="@+id/tvNumber2"
        android:layout_below="@+id/etEnterNum1"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/etEnterNum2"
        android:layout_below="@+id/tvNumber2"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:id="@+id/btnAdd"
        android:layout_below="@+id/etEnterNum2"
        android:onClick="doAdd"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:id="@+id/btnSub"
        android:onClick="doSub"
        android:layout_above="@+id/btnDiv"
        android:layout_toRightOf="@+id/tvNumber2"
        android:layout_toEndOf="@+id/tvNumber2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="*"
        android:id="@+id/btnMul"
        android:layout_below="@+id/btnAdd"
        android:layout_alignLeft="@+id/btnAdd"
        android:layout_alignStart="@+id/btnAdd"
        android:onClick="doMul"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="/"
        android:id="@+id/btnDiv"
        android:layout_alignTop="@+id/btnMul"
        android:layout_alignLeft="@+id/btnSub"
        android:layout_alignStart="@+id/btnSub"
        android:onClick="doDiv"/>

</RelativeLayout>


Add these line in
MainActivity.java


package html.sahilbhatia.com.cal;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
   
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void doAdd(View view) {
        EditText editText1 = (EditText) findViewById(R.id.etEnterNum1);
        EditText editText2 = (EditText) findViewById(R.id.etEnterNum2);
        int a = Integer.parseInt(editText1.getText().toString());
        int b = Integer.parseInt(editText2.getText().toString());
        TextView textView3 = (TextView) findViewById(R.id.tvResult);
        textView3.setText(String.valueOf(a + b));

    }

    public void doSub(View view) {
        EditText editText1 = (EditText) findViewById(R.id.etEnterNum1);
        EditText editText2 = (EditText) findViewById(R.id.etEnterNum2);
        int a = Integer.parseInt(editText1.getText().toString());
        int b = Integer.parseInt(editText2.getText().toString());
        TextView textView3 = (TextView) findViewById(R.id.tvResult);
        textView3.setText(String.valueOf(a - b));

    }
}

You can try multiply and divide you have any problem please comment it i will try to solve your problem. you like my post please share with friends. you also see my other posts directly this link






 


 
 

No comments:

Post a Comment