27
Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3: Introduction Android Development Wanting to demonstrate our knowledge on Android Studio, we provided screenshots and code of each step of the lab, including the first part of the lab, Basics. Provided are screenshots of our Android emulators alongside code necessary to get the programs started. Part I. Basics “Hello World”: Following the tutorial demonstrated on https://developer.android.com/training/basics/firstapp, provided are the files listed on the site (build.gradle, AndroidManifest.xml, MainActivity.java, and Activity_main.xml), in addition to a screenshot of the emulator when the program is run. Screenshot of Emulator: build.gradle (:app): apply plugin: 'com.android.application' android { compileSdkVersion 27 defaultConfig { applicationId "com.example.myfirstapplication" minSdkVersion 15 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

Adrina GarrettErika Gutierrez

May 12, 2020Lab 3: Introduction Android Development

Wanting to demonstrate our knowledge on Android Studio, we provided screenshots and code of eachstep of the lab, including the first part of the lab, Basics. Provided are screenshots of our Androidemulators alongside code necessary to get the programs started.

Part I. Basics“Hello World”:Following the tutorial demonstrated on https://developer.android.com/training/basics/firstapp, provided arethe files listed on the site (build.gradle, AndroidManifest.xml, MainActivity.java, and Activity_main.xml), inaddition to a screenshot of the emulator when the program is run.

❖ Screenshot of Emulator:

❖ build.gradle (:app):apply plugin: 'com.android.application'

android {compileSdkVersion 27defaultConfig {

applicationId "com.example.myfirstapplication"minSdkVersion 15targetSdkVersion 27versionCode 1versionName "1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}buildTypes {

release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

Page 2: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

}}

}

dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])implementation 'com.android.support:appcompat-v7:27.1.1'implementation 'com.android.support.constraint:constraint-layout:1.1.3'testImplementation 'junit:junit:4.12'androidTestImplementation 'com.android.support.test:runner:1.0.2'androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

❖ build.gradle (myfirstapplication):// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

repositories {google()jcenter()

}dependencies {

classpath 'com.android.tools.build:gradle:3.1.0'

// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files

}}allprojects {

repositories {google()jcenter()

}}

task clean(type: Delete) {delete rootProject.buildDir

}

❖ AndroidManifest.xml:<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.myfirstapplication">

<applicationandroid: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" />

Page 3: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

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

</activity></application>

</manifest>

❖ MainActivity.Javapackage com.example.myfirstapplication;

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Overrideprotected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);

}}

❖ Activity_main.xml:<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"tools:context=".MainActivity">

<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

“Hello, CSE 461-660.":Looking through the code, all the files listed above remained unchanged, however, to change the text onthe screen, we changed the android.text code listed under “Textview” in the Activity_main.xml file, from“Hello World!” to “Hello, CSE 461-660.” For your reference, we included all files, but began with the editedActivity_main.xml file (illustrated directly under the emulator).

Page 4: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

❖ Screenshot of Emulator:

❖ Activity_main.xml:<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"tools:context=".MainActivity">

<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, CSE 461-660."app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

❖ build.gradle (:app):apply plugin: 'com.android.application'

android {compileSdkVersion 27defaultConfig {

Page 5: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

applicationId "com.example.myfirstapplication"minSdkVersion 15targetSdkVersion 27versionCode 1versionName "1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}buildTypes {

release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}}

}

dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])implementation 'com.android.support:appcompat-v7:27.1.1'implementation 'com.android.support.constraint:constraint-layout:1.1.3'testImplementation 'junit:junit:4.12'androidTestImplementation 'com.android.support.test:runner:1.0.2'androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

❖ build.gradle (myfirstapplication):// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

repositories {google()jcenter()

}dependencies {

classpath 'com.android.tools.build:gradle:3.1.0'

// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files

}}allprojects {

repositories {google()jcenter()

}}

task clean(type: Delete) {delete rootProject.buildDir

}

❖ AndroidManifest.xml:<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.myfirstapplication">

Page 6: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

<applicationandroid: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>

❖ MainActivity.Javapackage com.example.myfirstapplication;

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Overrideprotected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);

}}

Part II. Simple CalculatorOperator Symbol Buttons:Following the tutorial demonstrated on https://startandroid.ru/en/lessons/225-lesson-19-creating-a-simple-calculator.html to create a simple calculator, provided are the same files listed under Part I(build.gradle,AndroidManifest.xml, MainActivity.java, and Activity_main.xml), in addition to a screenshot of the emulatorwhen the program is run for all operations (addition, subtraction, division, and multiplication), where themultiplication operation illustrated using decimal values.

Page 7: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

❖ Addition Screenshot:

❖ Subtraction Screenshot:

Page 8: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

❖ Division Screenshot:

❖ Multiplication Screenshot:

❖ build.gradle (:app):apply plugin: 'com.android.application'

Page 9: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

android {compileSdkVersion 28

defaultConfig {applicationId "com.example.simplecalculator"minSdkVersion 16targetSdkVersion 28versionCode 1versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}

buildTypes {release {

minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

}}

}

dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'implementation 'androidx.constraintlayout:constraintlayout:1.1.3'testImplementation 'junit:junit:4.12'androidTestImplementation 'androidx.test.ext:junit:1.1.1'androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

❖ build.gradle (SimpleCalculator):// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

repositories {google()jcenter()

}dependencies {

classpath 'com.android.tools.build:gradle:3.6.3'

// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files

}}

allprojects {repositories {

google()

Page 10: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

jcenter()

}}

task clean(type: Delete) {delete rootProject.buildDir

}

❖ AndroidManifest.xml:<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.simplecalculator">

<applicationandroid: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>

❖ MainActivity.Javapackage com.example.simplecalculator;

import android.app.Activity;import android.text.TextUtils;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends Activity implements View.OnClickListener {

EditText etNum1;EditText etNum2;

Button btnAdd;Button btnSub;Button btnMult;Button btnDiv;

Page 11: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

TextView tvResult;

String oper = "";

/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);

// find the elementsetNum1 = (EditText) findViewById(R.id.etNum1);etNum2 = (EditText) findViewById(R.id.etNum2);

btnAdd = (Button) findViewById(R.id.btnAdd);btnSub = (Button) findViewById(R.id.btnSub);btnMult = (Button) findViewById(R.id.btnMult);btnDiv = (Button) findViewById(R.id.btnDiv);

tvResult = (TextView) findViewById(R.id.tvResult);

// set a listenerbtnAdd.setOnClickListener(this);btnSub.setOnClickListener(this);btnMult.setOnClickListener(this);btnDiv.setOnClickListener(this);

}

@Overridepublic void onClick(View v) {

// TODO Auto-generated method stubfloat num1 = 0;float num2 = 0;float result = 0;

// check if the fields are emptyif (TextUtils.isEmpty(etNum1.getText().toString())

|| TextUtils.isEmpty(etNum2.getText().toString())) {return;

}

// read EditText and fill variables with numbersnum1 = Float.parseFloat(etNum1.getText().toString());num2 = Float.parseFloat(etNum2.getText().toString());

// defines the button that has been clicked and performs the corresponding operation// write operation into oper, we will use it later for outputswitch (v.getId()) {

case R.id.btnAdd:oper = "+";result = num1 + num2;break;

case R.id.btnSub:oper = "-";

Page 12: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

result = num1 - num2;break;

case R.id.btnMult:oper = "*";result = num1 * num2;break;

case R.id.btnDiv:oper = "/";result = num1 / num2;break;

default:break;

}

// form the output linetvResult.setText(num1 + " " + oper + " " + num2 + " = " + result);

}}

❖ 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"><LinearLayout

android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/linearLayout1"android:layout_marginLeft="10pt"android:layout_marginRight="10pt"android:layout_marginTop="3pt"><EditText

android:layout_weight="1"android:layout_height="wrap_content"android:layout_marginRight="5pt"android:id="@+id/etNum1"android:layout_width="match_parent"android:inputType="numberDecimal">

</EditText><EditText

android:layout_height="wrap_content"android:layout_weight="1"android:layout_marginLeft="5pt"android:id="@+id/etNum2"android:layout_width="match_parent"android:inputType="numberDecimal">

</EditText></LinearLayout><LinearLayout

android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/linearLayout2"android:layout_marginTop="3pt"android:layout_marginLeft="5pt"

Page 13: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

android:layout_marginRight="5pt"><Button

android:layout_height="wrap_content"android:layout_width="match_parent"android:layout_weight="1"android:text="+"android:textSize="8pt"android:id="@+id/btnAdd">

</Button><Button

android:layout_height="wrap_content"android:layout_width="match_parent"android:layout_weight="1"android:text="-"android:textSize="8pt"android:id="@+id/btnSub">

</Button><Button

android:layout_height="wrap_content"android:layout_width="match_parent"android:layout_weight="1"android:text="*"android:textSize="8pt"android:id="@+id/btnMult">

</Button><Button

android:layout_height="wrap_content"android:layout_width="match_parent"android:layout_weight="1"android:text="/"android:textSize="8pt"android:id="@+id/btnDiv">

</Button></LinearLayout><TextView

android:layout_height="wrap_content"android:layout_width="match_parent"android:layout_marginLeft="5pt"android:layout_marginRight="5pt"android:textSize="12pt"android:layout_marginTop="3pt"android:id="@+id/tvResult"android:gravity="center_horizontal">

</TextView></LinearLayout>

Operator Picture Buttons:To change the buttons from using symbol operators to images, we first began by searching images ofbuttons and named them in a “_operator” format (i.e. the addition image is named “_addition” and so onfor the remaining operators). The images we found online are shown below.

Page 14: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

After we named each image, we copied and pasted all four images into Android Studio underapp>res>drawable. A picture of the images copied into Android Studio is shown below.

To change the buttons from showing symbols to the images we edited the code in the Activity_man.xmlfile. For each respective button that represents each operation, we replaced two lines of code (i.e. usingdivision operator: android:text="/" and android:textSize="8pt") with one line (i.e. using division operator:android:background="@drawable/_division"></Button>). Acting like the calculator above, upon pressingthe image, the calculator successfully performs the appropriate operator. To illustrate this, below areimages of each operation (using the same numbers used in the previous calculator) when the emulator isrunning in addition to all the files shown in Part I, beginning with the Activity_man.xml file.

❖ Addition Screenshot:

Page 15: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

❖ Subtraction Screenshot:

❖ Division Screenshot:

Page 16: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

❖ Multiplication Screenshot:

❖ 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"><LinearLayout

android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/linearLayout1"android:layout_marginLeft="10pt"android:layout_marginRight="10pt"android:layout_marginTop="3pt"><EditText

android:layout_weight="1"android:layout_height="wrap_content"android:layout_marginRight="5pt"android:id="@+id/etNum1"android:layout_width="match_parent"android:inputType="numberDecimal">

</EditText><EditText

android:layout_height="wrap_content"android:layout_weight="1"android:layout_marginLeft="5pt"android:id="@+id/etNum2"

Page 17: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

android:layout_width="match_parent"android:inputType="numberDecimal">

</EditText></LinearLayout>

<LinearLayoutandroid:id="@+id/linearLayout2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5pt"android:layout_marginRight="5pt"android:layout_marginTop="3pt">

<Buttonandroid:id="@+id/btnAdd"android:layout_width="20dp"android:layout_height="60dp"android:layout_weight="1"android:background="@drawable/_addition"></Button>

<Buttonandroid:id="@+id/btnSub"android:layout_width="20dp"android:layout_height="60dp"android:layout_weight="1"android:background="@drawable/_subtraction"></Button>

<Buttonandroid:id="@+id/btnMult"android:layout_width="20dp"android:layout_height="60dp"android:layout_weight="1"android:background="@drawable/_multiplication"></Button>

<Buttonandroid:id="@+id/btnDiv"android:layout_width="20dp"android:layout_height="60dp"android:layout_weight="1"android:background="@drawable/_division"></Button>

</LinearLayout>

<TextViewandroid:layout_height="wrap_content"android:layout_width="match_parent"android:layout_marginLeft="5pt"android:layout_marginRight="5pt"android:textSize="12pt"android:layout_marginTop="3pt"android:id="@+id/tvResult"android:gravity="center_horizontal">

</TextView></LinearLayout>

❖ build.gradle (:app):apply plugin: 'com.android.application'

Page 18: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

android {compileSdkVersion 28

defaultConfig {applicationId "com.example.simplecalculator"minSdkVersion 16targetSdkVersion 28versionCode 1versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}

buildTypes {release {

minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

}}

}

dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'implementation 'androidx.constraintlayout:constraintlayout:1.1.3'testImplementation 'junit:junit:4.12'androidTestImplementation 'androidx.test.ext:junit:1.1.1'androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

❖ build.gradle (SimpleCalculator):// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

repositories {google()jcenter()

}dependencies {

classpath 'com.android.tools.build:gradle:3.6.3'

// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files

}}

allprojects {repositories {

google()

Page 19: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

jcenter()

}}

task clean(type: Delete) {delete rootProject.buildDir

}

❖ AndroidManifest.xml:<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.simplecalculator">

<applicationandroid: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>

❖ MainActivity.Javapackage com.example.simplecalculator;

import android.app.Activity;import android.text.TextUtils;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends Activity implements View.OnClickListener {

EditText etNum1;EditText etNum2;

Button btnAdd;Button btnSub;Button btnMult;Button btnDiv;

Page 20: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

TextView tvResult;

String oper = "";

/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);

// find the elementsetNum1 = (EditText) findViewById(R.id.etNum1);etNum2 = (EditText) findViewById(R.id.etNum2);

btnAdd = (Button) findViewById(R.id.btnAdd);btnSub = (Button) findViewById(R.id.btnSub);btnMult = (Button) findViewById(R.id.btnMult);btnDiv = (Button) findViewById(R.id.btnDiv);

tvResult = (TextView) findViewById(R.id.tvResult);

// set a listenerbtnAdd.setOnClickListener(this);btnSub.setOnClickListener(this);btnMult.setOnClickListener(this);btnDiv.setOnClickListener(this);

}

@Overridepublic void onClick(View v) {

// TODO Auto-generated method stubfloat num1 = 0;float num2 = 0;float result = 0;

// check if the fields are emptyif (TextUtils.isEmpty(etNum1.getText().toString())

|| TextUtils.isEmpty(etNum2.getText().toString())) {return;

}

// read EditText and fill variables with numbersnum1 = Float.parseFloat(etNum1.getText().toString());num2 = Float.parseFloat(etNum2.getText().toString());

// defines the button that has been clicked and performs the corresponding operation// write operation into oper, we will use it later for outputswitch (v.getId()) {

case R.id.btnAdd:oper = "+";result = num1 + num2;break;

case R.id.btnSub:oper = "-";

Page 21: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

result = num1 - num2;break;

case R.id.btnMult:oper = "*";result = num1 * num2;break;

case R.id.btnDiv:oper = "/";result = num1 / num2;break;

default:break;

}

// form the output linetvResult.setText(num1 + " " + oper + " " + num2 + " = " + result);

}}

Part III. FragmentsKnowing we need to make the background change color, we obtained the code for our colors fromhttps://material.io/design/color/the-color-system.html#tools-for-picking-colors and used their codes in thecolors.xml file. Using two buttons, one for Fragment A and one for Fragment B, they were coded, usingfragments so when the user hits the Fragment A button, the text shows “I am Fragment A” and thebackground turns yellow. When the user hits the Fragment B button, the text shows “I am Fragment B”and the background turns blue. Having used fragments, we created two new java files (FragmentA.javaand FragmentB.java) and two new xml files (FFragment.xml and SFragment.xml).To keep consistency, below you will find screenshots of our emulator when each button is hit in additionto all the files we have listed for the past two sections. For the purposes of demonstrating ourunderstanding of fragments, the two new .java and .xml files are also listed.

❖ User hits Fragment A button

Page 22: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

❖ User hits Fragment B button

❖ Colors.xml<?xml version="1.0" encoding="utf-8"?><resources>

<color name="colorPrimary">#6200EE</color><color name="colorPrimaryDark">#3700B3</color><color name="colorAccent">#03DAC5</color><color name="Yellow">#FFF59D</color><color name="Blue">#81D4FA</color>

</resources>

❖ FragmentA.Javapackage com.example.fragments;

import androidx.fragment.app.Fragment;import android.view.LayoutInflater;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;

//subclass

public class FragmentA extends Fragment {

public FragmentA() {// Empty Constructor

}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

Page 23: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.FFragement, container, false);

}

}

❖ FragmentB.Javapackage com.example.fragments;

import androidx.fragment.app.Fragment;import android.view.LayoutInflater;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;

//subclass

public class FragmentB extends Fragment {

public FragmentB() {//Empty Constructor

}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.SFragment, container, false);

}

}

❖ FFragment.xml<?xml version="1.0" encoding="UTF-8"?><androidx.constraintlayout.widget.ConstraintLayout

tools:context=".FragmentA"android:layout_height="match_parent"android:layout_width="match_parent"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:android="http://schemas.android.com/apk/res/android"><TextView android:layout_height="wrap_content"

android:layout_width="wrap_content"android:text="I am Fragment A"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintStart_toStartOf="parent"android:textSize="30sp"app:layout_constraintHorizontal_bias="0.5"android:id="@+id/tvFirstFragment"/>

</androidx.constraintlayout.widget.ConstraintLayout>

❖ SFragement.xml<?xml version="1.0" encoding="UTF-8"?>

<androidx.constraintlayout.widget.ConstraintLayout

Page 24: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

tools:context=".FragmentB"android:layout_height="match_parent"android:layout_width="match_parent"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:android="http://schemas.android.com/apk/res/android"><TextView

android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="I am Fragment B"app:layout_constraintTop_toTopOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintBottom_toBottomOf="parent"android:textSize="30sp"app:layout_constraintHorizontal_bias="0.5"android:id="@+id/tvSecondFragment"/>

</androidx.constraintlayout.widget.ConstraintLayout>

❖ MainActivity.Javapackage com.example.fragments;

//importing packagesimport android.view.View;import android.os.Bundle;import androidx.fragment.app.Fragment;import androidx.appcompat.app.AppCompatActivity;import androidx.fragment.app.FragmentManager;import androidx.fragment.app.FragmentTransaction;import android.widget.Button;

public class MainActivity extends AppCompatActivity {//Declaration of variables for background and buttonsView view;Button Fragment1Button; //Fragment AButton Fragment2Button; //Fragment B

//Given@Overrideprotected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);

//Sets variables to IDsFragment1Button = (Button) findViewById(R.id.Fragment1Button);Fragment2Button = (Button) findViewById(R.id.Fragment2Button);

//Enables changing backgroundview = this.getWindow().getDecorView();

//First TransactionFragmentTransaction firstFragment = getSupportFragmentManager().beginTransaction();FragmentA F1 = new FragmentA();firstFragment.replace(R.id.Fragment1, F1);firstFragment.commit();

Page 25: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

//Button Fragment AFragment1Button.setOnClickListener(new View.OnClickListener() {

@Overridepublic void onClick(View v) {

FragmentTransaction firstFragment =getSupportFragmentManager().beginTransaction();

FragmentA fragment1 = new FragmentA();firstFragment.replace(R.id.Fragment1, fragment1);firstFragment.commit();view.setBackgroundResource(R.color.Yellow);

}});//Button Fragment BFragment2Button.setOnClickListener(new View.OnClickListener() {

@Overridepublic void onClick(View v) {

FragmentTransaction secondFragment =getSupportFragmentManager().beginTransaction();

FragmentB F2 = new FragmentB();secondFragment.replace(R.id.Fragment1, F2);secondFragment.commit();view.setBackgroundResource(R.color.Blue);

}});

}}

❖ Activity_main.xml<?xml version="1.0" encoding="UTF-8"?>

<androidx.constraintlayout.widget.ConstraintLayouttools:context=".MainActivity"android:layout_height="match_parent"android:layout_width="match_parent"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:android="http://schemas.android.com/apk/res/android"><Button

android:layout_height="wrap_content"android:layout_width="wrap_content"app:layout_constraintTop_toTopOf="parent"app:layout_constraintStart_toEndOf="@+id/Fragment1Button"app:layout_constraintHorizontal_bias="0.5"android:text="Fragment B"android:id="@+id/Fragment2Button"app:layout_constraintEnd_toEndOf="parent"/>

<Buttonandroid:layout_height="wrap_content"android:layout_width="wrap_content"app:layout_constraintTop_toTopOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintHorizontal_bias="0.5"app:layout_constraintEnd_toStartOf="@+id/Fragment2Button"android:text="Fragment A"android:id="@+id/Fragment1Button"/>

<FrameLayout

Page 26: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

android:layout_height="0dp"android:layout_width="match_parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toBottomOf="@+id/Fragment1Button"app:layout_constraintStart_toStartOf="parent"app:layout_constraintHorizontal_bias="0.5"android:id="@+id/Fragment1"app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

❖ build.gradle (:app):apply plugin: 'com.android.application'

android {compileSdkVersion 28

defaultConfig {applicationId "com.example.fragments"minSdkVersion 16targetSdkVersion 28versionCode 1versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}

buildTypes {release {

minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

}}

}

dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'implementation 'androidx.constraintlayout:constraintlayout:1.1.3'testImplementation 'junit:junit:4.12'androidTestImplementation 'androidx.test.ext:junit:1.1.1'androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

❖ build.gradle (Fragments):// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

repositories {google()jcenter()

}

Page 27: Adrina Garrett Erika Gutierrez May 12, 2020 Lab 3 ...cse.csusb.edu/tongyu/studentwork/cs461/labs/lab3... · step of the lab, including the first part of the lab, Basics. Provided

dependencies {classpath 'com.android.tools.build:gradle:3.6.3'

// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files

}}

allprojects {repositories {

google()jcenter()

}}

task clean(type: Delete) {delete rootProject.buildDir

}

❖ AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.fragments">

<applicationandroid: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>

Self-Evaluation:We successfully completed each section of the lab and provided step-by-step instructions on how weobtained answers in addition to all files listed. We demonstrate our understanding of Android Studiothrough consistent and detailed descriptions, alongside appropriate images, making us believe wedeserve 20/20 possible points.