Sie sind auf Seite 1von 18

Assignment

For
Mobile Application Development

Course Instructors Adnan Waheed


Assignment No. 02
Name: Arslan Azhar
Roll No: 026
Submitted date 1 Sep 2017

Submitted to Mam Fanila Younis

Department of Computer Science


Codes
MainActivity.java
package com.example.Arslan.basiccalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import static com.example.dell.basiccalculator.R.id.bZero;

public class MainActivity extends AppCompatActivity {


private Button
Nine,Eight,Seven,Six,Five,Four,Three,Two,One,Zero,Dot,Add,Subtra
ct,Division,Multi,Equal,Clear;
private TextView et,et1;
double no1,no2;
boolean add,Minus,Multiply,Divison;
char lO;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Nine=(Button)findViewById(R.id.bNine);
Eight=(Button)findViewById(R.id.bEight);
Seven=(Button)findViewById(R.id.bSeven);
Six=(Button)findViewById(R.id.bSix);
Five=(Button)findViewById(R.id.bFive);
Four=(Button)findViewById(R.id.bFour);
Three=(Button)findViewById(R.id.bThree);
Two=(Button)findViewById(R.id.bTwo);
One=(Button)findViewById(R.id.bOne);
Zero=(Button)findViewById(bZero);
Dot=(Button)findViewById(R.id.btnDot);
Add=(Button)findViewById(R.id.btnAdd);
Subtract=(Button)findViewById(R.id.btnSubtract);
Division=(Button)findViewById(R.id.btnDivide);
Multi=(Button)findViewById(R.id.btnMultiply);
Equal=(Button)findViewById(R.id.btnEqual);
Clear=(Button)findViewById(R.id.bClear);
et=(TextView)findViewById(R.id.textView);
et1=(TextView)findViewById(R.id.textView2);
Zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
et1.setText(et1.getText()+"0");
}

});
One.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
et1.setText(et1.getText()+"1");
}

});
Two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
et1.setText(et1.getText()+"2");
}

});
Three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
et1.setText(et1.getText()+"3");
}

});
Four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
et1.setText(et1.getText()+"4");
}

});
Five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
et1.setText(et1.getText()+"5");
}

});
Six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
et1.setText(et1.getText()+"6");
}

});
Seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
et1.setText(et1.getText()+"7");
}

});
Eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
et1.setText(et1.getText()+"8");
}

});
Nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
et1.setText(et1.getText()+"9");
}

});
Dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
et1.setText(et1.getText()+".");
}

});

Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
no1= Double.parseDouble(et1.getText()+"");
add=true;
et1.setText("");
lO='+';
et.setText(no1 + "" + lO +"");
}

});
Subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
no1= Double.parseDouble(et1.getText()+"");
Minus=true;
et1.setText("");
lO='-';
et.setText(no1+"" + lO+"");
}

});
Multi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
no1= Double.parseDouble(et1.getText()+"");
Multiply=true;
et1.setText("");
lO='*';
et.setText(no1+""+lO+"");
}

});
Division.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
no1= Double.parseDouble(et1.getText()+"");
Divison=true;
et1.setText("");
lO='/';
et.setText(no1+""+lO+"");
}

});
Equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
no2= Double.parseDouble(et1.getText()+"");
if(add==true){
et.setText(no1+""+lO+""+no2+"");
et1.setText(no1+no2+"");
add=false;
}
if(Minus==true) {
et.setText(no1 + "" + lO+ "" + no2 + "");
et1.setText(no1-no2 + "");
Minus = false;
}
if(Multiply==true){
et.setText(no1+""+lO+""+no2+"");
et1.setText(no1*no2+"");
Multiply=false;
}
if(Divison==true){
et.setText(no1+""+lO+""+no2+"");
et1.setText(no1/no2+"");
Divison=false;

});
Clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
et1.setText("");
et.setText("");
}
});
}
}

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"

tools:context="com.example.dell.basiccalculator.MainActivity">

<Button
android:id="@+id/btnEqual"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/bZero"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="="
android:textColor="@android:color/holo_orange_dark"
android:textSize="36sp" />

<Button
android:id="@+id/bClear"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_above="@+id/btnEqual"
android:layout_alignParentStart="true"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="C"
android:textColor="@android:color/holo_orange_dark"
android:textSize="36sp" />

<Button
android:id="@+id/bZero"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_above="@+id/btnEqual"
android:layout_toEndOf="@+id/bClear"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="0"
android:textColor="@android:color/holo_orange_dark"
android:textSize="36sp" />

<Button
android:id="@+id/btnAdd"
android:layout_width="120dp"
android:layout_height="70dp"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="+"
android:textColor="@android:color/holo_orange_dark"
android:textSize="36sp"
android:layout_below="@+id/btnSubtract"
android:layout_toEndOf="@+id/bThree" />

<Button
android:id="@+id/bOne"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_above="@+id/bClear"
android:layout_alignParentStart="true"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="1"
android:textColor="#ffff8800"
android:textSize="36sp" />

<Button
android:id="@+id/bTwo"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_above="@+id/bZero"
android:layout_toEndOf="@+id/bClear"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="2"
android:textColor="#ffff8800"
android:textSize="36sp" />

<Button
android:id="@+id/bThree"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_above="@+id/bZero"
android:layout_toEndOf="@+id/bZero"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="3"
android:textColor="#ffff8800"
android:textSize="36sp" />

<Button
android:id="@+id/btnSubtract"
android:layout_width="120dp"
android:layout_height="70dp"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="-"
android:textColor="#ffff8800"
android:textSize="36sp"
android:layout_below="@+id/btnMultiply"
android:layout_toEndOf="@+id/bThree" />

<Button
android:id="@+id/bFour"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_above="@+id/bOne"
android:layout_alignParentStart="true"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="4"
android:textColor="#ffff8800"
android:textSize="36sp" />

<Button
android:id="@+id/bFive"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_above="@+id/bTwo"
android:layout_toEndOf="@+id/bOne"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="5"
android:textColor="#ffff8800"
android:textSize="36sp" />

<Button
android:id="@+id/bSix"
android:layout_width="90dp"
android:layout_height="70dp"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="6"
android:textColor="#ffff8800"
android:textSize="36sp"
android:layout_below="@+id/bNine"
android:layout_toEndOf="@+id/bTwo" />

<Button
android:id="@+id/btnMultiply"
android:layout_width="120dp"
android:layout_height="70dp"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="X"
android:textColor="#ffff8800"
android:textSize="36sp"
android:layout_below="@+id/textView2"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/bSix" />
<Button
android:id="@+id/bSeven"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_above="@+id/bFour"
android:layout_alignParentStart="true"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="7"
android:textColor="#ffff8800"
android:textSize="36sp" />

<Button
android:id="@+id/bEight"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_above="@+id/bFive"
android:layout_toEndOf="@+id/bFour"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="8"
android:textColor="#ffff8800"
android:textSize="36sp" />

<Button
android:id="@+id/bNine"
android:layout_width="90dp"
android:layout_height="70dp"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="9"
android:textColor="#ffff8800"
android:textSize="36sp"
android:layout_below="@+id/textView2"
android:layout_toEndOf="@+id/bEight" />

<Button
android:id="@+id/btnDivide"
android:layout_width="120dp"
android:layout_height="70dp"
android:background="#F5F5F5"
android:fontFamily="sans-serif-condensed"
android:text="/"
android:textColor="#ffff8800"
android:textSize="36sp"
android:layout_below="@+id/btnAdd"
android:layout_toEndOf="@+id/btnDot" />

<TextView
android:id="@+id/textView"
android:layout_width="45dp"
android:layout_height="60dp"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:gravity="bottom|right"
android:textColor="@android:color/holo_orange_dark"
android:textSize="36sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_above="@+id/bSeven"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:gravity="bottom|right"
android:textColor="@android:color/darker_gray"
android:textSize="36sp" />

<Button
android:id="@+id/btnDot"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_above="@+id/btnEqual"
android:layout_below="@+id/btnAdd"
android:layout_toEndOf="@+id/bZero"
android:background="#F5F5F5"
android:text="."
android:textColor="@android:color/holo_orange_dark"
android:textSize="36sp" />
</RelativeLayout>

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

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

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

</manifest>

Screenshots