android Studio.>

profileabdals1
Chapter03slides.pptx

Chapter 3

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 1

Objectives

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 2

Objectives (continued)

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 3

The default Java code for an activity

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 4

How to work with an activity

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 5

The v7 appcompat support library

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 6

How to get references to widgets

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 7

An activity that gets references to the widgets

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 8

An activity that gets references to the widgets (continued)

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 9

How events work

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 10

How to handle the EditorAction event

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 11

An activity that handles the EditorAction event

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 12

An activity that handles the EditorAction event (continued)

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 13

A few constants from the EditorInfo class

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 14

Two methods for working with widgets

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 15

A method that calculates and displays amounts

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 16

How to handle the Click event

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 17

How to handle the Click event (continued)

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 18

The lifecycle of an activity

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 19

Three common states

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 20

How to import the SharedPreferences classes

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 21

How to use onPause to save values

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 22

How to use onResume to restore values

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 23

The Java code

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 24

The Java code (continued)

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 25

The Java code (continued)

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 26

The Java code (continued)

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 27

The Java code (continued)

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 28

The Java code (continued)

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 29

The Java code (continued)

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 30

The Gradle build script

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 31

The dialogs for adding a dependency

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 32

A dependencies block

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 33

The AndroidManifest.xml file

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 34

An activity element that only allows portrait orientation

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 35

How to set the launcher icon for an app

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 36

The Android Asset Studio web page for generating launcher icons

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 37

The directory structure for launcher icons

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 38

The documentation for the Activity class

Murach's Android Programming (2nd Ed.), C3

© 2015, Mike Murach & Associates, Inc.

Slide 39

How to finish

your first Android app

How to finish your first Android app

Applied

 Given the source code for an app that uses any of the XML and

Java skills presented in this chapter, explain what each XML

element and Java statement does.

 Given the package and name of a class within the Android API,

look it up in the official documentation.

Knowledge

 Explain how to use a listener to handle an event that can occur

when a user interacts with a widget.

 List three common used lifecycle methods of an activity and

describe when they are called.

 List three common states of an activity and describe when an app

might be in any of these states.

Applied

Given the source code for an app that uses any of the XML and Java skills presented in this chapter, explain what each XML element and Java statement does.

Given the package and name of a class within the Android API, look it up in the official documentation.

Knowledge

Explain how to use a listener to handle an event that can occur when a user interacts with a widget.

List three common used lifecycle methods of an activity and describe when they are called.

List three common states of an activity and describe when an app might be in any of these states.

 Describe two common situations where Android might not save

the data of an activity.

 Describe a common way of saving and restoring the data for an

Android app.

 Describe two common situations where Android might not save

the data of an activity.

 Describe how Java code handles the events that occur on the user

interface.

 Explain how to use the Gradle build script to control how Android

builds a project.

 Describe some of the essential information that’s provided by the

AndroidManifest.xml file.

Describe two common situations where Android might not save the data of an activity.

Describe a common way of saving and restoring the data for an Android app.

Describe two common situations where Android might not save the data of an activity.

Describe how Java code handles the events that occur on the user interface.

Explain how to use the Gradle build script to control how Android builds a project.

Describe some of the essential information that’s provided by the AndroidManifest.xml file.

package com.murach.tipcalculator;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

public class TipCalculatorActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_tip_calculator);

}

}

package com.murach.tipcalculator;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

public class TipCalculatorActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_tip_calculator);

}

}

 To create a new activity, right -click a package in the Project

window, select the New ActivityEmpty Activity item, and

respond to the resulting dialog.

 The AppCompatActivity class is designed to make your app

compatible with most versions of Android.

 The AppCompatActivity class inherits the Activity class, which is

the base class for all activities.

 Android runs the onCreate method when the activity is started.

 The onCreate method usually passes the Bundle parameter to the

onCreate method of the superclas s.

 The onCreate method usually uses the setContentView method to

specify the layout.

 The R class provides a way to access the compiled resources for

your project. This class is created automatically when you build

your project.

To create a new activity, right-click a package in the Project window, select the NewActivityEmpty Activity item, and respond to the resulting dialog.

The AppCompatActivity class is designed to make your app compatible with most versions of Android.

The AppCompatActivity class inherits the Activity class, which is the base class for all activities.

Android runs the onCreate method when the activity is started.

The onCreate method usually passes the Bundle parameter to the onCreate method of the superclass.

The onCreate method usually uses the setContentView method to specify the layout.

The R class provides a way to access the compiled resources for your project. This class is created automatically when you build your project.

General description

 Provides support back to Android 2.1 (API 7).

Features

 Supports the action bar, which was introduced with Android

4.0 (API 14).

 Supports the Material theme, which was introduced with

Android 5.0 (API 21) .

The Gradle build script dependency

com.android.support:appcompat -v7:23.0.0

General description

Provides support back to Android 2.1 (API 7).

Features

Supports the action bar, which was introduced with Android 4.0 (API 14).

Supports the Material theme, which was introduced with Android 5.0 (API 21).

The Gradle build script dependency

com.android.support:appcompat-v7:23.0.0

 You can use the findViewById method to get references to the

widgets that are declared in the layout.

 This method accepts an argument for the ID of the widget. The R

class provides a way to get the ID.

 This method returns a View object for the widget with the

specified ID that you can cast to the correct type.

You can use the findViewById method to get references to the widgets that are declared in the layout.

This method accepts an argument for the ID of the widget. The R class provides a way to get the ID.

This method returns a View object for the widget with the specified ID that you can cast to the correct type.

package com.murach.tipcalculator;

import android.app.Activity;

import android.os.Bundle;

import android.widget.EditText; // 1. Import classes for widget s

import android.widget.TextView;

public class TipCalculatorActivity extends Activity {

// 2. Declare variables for the widgets

private EditText billAmountEditText;

private TextView tipTextView;

private TextView totalTextView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState) ;

setContentView(R.layout.activity_tip_calculator);

package com.murach.tipcalculator;

import android.app.Activity;

import android.os.Bundle;

import android.widget.EditText; // 1. Import classes for widgets

import android.widget.TextView;

public class TipCalculatorActivity extends Activity {

// 2. Declare variables for the widgets

private EditText billAmountEditText;

private TextView tipTextView;

private TextView totalTextView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_tip_calculator);

// 3. Get references to the widgets

billAmountEditText = (EditText)

findViewById(R.id.billAmountEditText);

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

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

}

}

// 3. Get references to the widgets

billAmountEditText = (EditText)

findViewById(R.id.billAmountEditText);

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

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

}

}

 An event handler is a special type of method that’s executed when

an event occurs.

 Typically, an event occurs when a user interacts with widgets.

 A listener is an object that listens for an event. To create a

listener, import the interface for the listener and implement that

interface.

 After you create a listener, connect, or wire, the listener to an

event that’s available from a widget.

An event handler is a special type of method that’s executed when an event occurs.

Typically, an event occurs when a user interacts with widgets.

A listener is an object that listens for an event. To create a listener, import the interface for the listener and implement that interface.

After you create a listener, connect, or wire, the listener to an event that’s available from a widget.

 The EditorAction event typically occurs when the user uses a soft

keyboard to enter text into an editable text view.

 To determine which widget triggered the event, you can use the

first argument of the onEditorAction method.

 To determine which action key triggered the event, you can use

the second argument of the onEditorAction method.

 To determine whether the action was trigged by pressing the Enter

key, you can use the third argument of the onEditorAction

method.

 To hide the soft keyboard, you can retur n a false value from the

onEditorAction method.

 To keep the soft keyboard displayed, you can return a true value

from the onEditorAction method.

The EditorAction event typically occurs when the user uses a soft keyboard to enter text into an editable text view.

To determine which widget triggered the event, you can use the first argument of the onEditorAction method.

To determine which action key triggered the event, you can use the second argument of the onEditorAction method.

To determine whether the action was trigged by pressing the Enter key, you can use the third argument of the onEditorAction method.

To hide the soft keyboard, you can return a false value from the onEditorAction method.

To keep the soft keyboard displayed, you can return a true value from the onEditorAction method.

package com.murach.tipcalculator;

import android.app.Activity;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.inputmethod.EditorInfo;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.TextView.OnEditorActionListener; // 1. Import

public class TipCalculatorActivity extends Activity

implements OnEditorActionListener { // 2a. Implement

private EditText billAmountEditText;

private TextView tipTextView;

private TextView totalTextView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_tip_calculator);

package com.murach.tipcalculator;

import android.app.Activity;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.inputmethod.EditorInfo;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.TextView.OnEditorActionListener; // 1. Import

public class TipCalculatorActivity extends Activity

implements OnEditorActionListener { // 2a. Implement

private EditText billAmountEditText;

private TextView tipTextView;

private TextView totalTextView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_tip_calculator);

billAmountEditText = (EditText)

findViewById(R.id.billAmountEditText);

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

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

// 3. Set

billAmountEditText.setOnEditorActionListener(this);

}

// 2b. Implement

@Override

public boolean onEditorAction(TextView v, int actionId,

KeyEvent event) {

if (actionId == EditorInfo.IME_ACTION_DONE ||

actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {

tipTextView.setText("$10.00");

totalTextView.setText("$110.00");

}

return false;

}

}

billAmountEditText = (EditText)

findViewById(R.id.billAmountEditText);

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

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

// 3. Set

billAmountEditText.setOnEditorActionListener(this);

}

// 2b. Implement

@Override

public boolean onEditorAction(TextView v, int actionId,

KeyEvent event) {

if (actionId == EditorInfo.IME_ACTION_DONE ||

actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {

tipTextView.setText("$10.00");

totalTextView.setText("$110.00");

}

return false;

}

}

IME_ACTION_DONE

IME_ACTION_GO

IME_ACTION_NEXT

IME_ACTION_SEARCH

IME_ACTION_UNSPECIFIED

What is an IME?

 An input method editor (IME) is a user control such as a soft

keyboard that enables users to input data, often text .

IME_ACTION_DONE

IME_ACTION_GO

IME_ACTION_NEXT

IME_ACTION_SEARCH

IME_ACTION_UNSPECIFIED

What is an IME?

An input method editor (IME) is a user control such as a soft keyboard that enables users to input data, often text.

getText()

setText(string)

Examples

How to get text

String billAmountString =

billAmountEditText.getText().toString();

How to set text

tipTextView.setText("Test 1");

getText()

setText(string)

Examples

How to get text

String billAmountString =

billAmountEditText.getText().toString();

How to set text

tipTextView.setText("Test 1");

public void calculateAndDisplay() {

// get the bill amount

billAmountString = billAmountEditText.getText().toString();

float billAmount;

if (billAmountString.equals("")) {

billAmount = 0;

}

else {

billAmount = Float.parseFloat(billAmountString);

}

// calculate tip and total

float tipPercent = .15f;

float tipAmount = billAmount * tipPercent;

float totalAmount = billAmount + tipAmount;

// display the results with formatting

NumberFormat currency = NumberFormat.getCurrencyInstance();

tipTextView.setText(currency.format(tipAmount));

totalTextView.setText(currency.format(totalAmount));

}

public void calculateAndDisplay() {

// get the bill amount

billAmountString = billAmountEditText.getText().toString();

float billAmount;

if (billAmountString.equals("")) {

billAmount = 0;

}

else {

billAmount = Float.parseFloat(billAmountString);

}

// calculate tip and total

float tipPercent = .15f;

float tipAmount = billAmount * tipPercent;

float totalAmount = billAmount + tipAmount;

// display the results with formatting

NumberFormat currency = NumberFormat.getCurrencyInstance();

tipTextView.setText(currency.format(tipAmount));

totalTextView.setText(currency.format(totalAmount));

}

Step 1: Import the interface for the listener

import android.view.View.OnClickListener;

Step 2a: Implement the interface for the listener

public class TipCalculatorActivity extends AppCompatActivity

implements OnEditorActionListener, OnClickListener {

Step 2b: Implement the interface for the listener

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.percentDownButton:

tipPercent = tipPercent - .01f;

calculateAndDisplay();

break;

case R.id.percentUpButton:

tipPercent = tipPercent + .01f;

calculateAndDisplay();

break;

}

}

Step 1: Import the interface for the listener

import android.view.View.OnClickListener;

Step 2a: Implement the interface for the listener

public class TipCalculatorActivity extends AppCompatActivity

implements OnEditorActionListener, OnClickListener {

Step 2b: Implement the interface for the listener

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.percentDownButton:

tipPercent = tipPercent - .01f;

calculateAndDisplay();

break;

case R.id.percentUpButton:

tipPercent = tipPercent + .01f;

calculateAndDisplay();

break;

}

}

Step 3: Set the listeners

percentUpButton.setOnClickListener(this);

percentDownButton.setOnClickListener(this);

Step 3: Set the listeners

percentUpButton.setOnClickListener(this);

percentDownButton.setOnClickListener(this);

State Description

Resumed This state is also known as the active state or the

running state. A running activity is visible and

has the focus.

Paused A paused activity has lost the focus and may be

partially hidden by other activities, or the device

has gone to sleep.

Stopped The activity has lost the focus and is completely

hidden by another activity.

State Description

Resumed This state is also known as the active state or the running state. A running activity is visible and has the focus.

Paused A paused activity has lost the focus and may be partially hidden by other activities, or the device has gone to sleep.

Stopped The activity has lost the focus and is completely hidden by another activity.

import android.content.SharedPreferences;

import android.content. SharedPreferences .Editor;

How to set up the instance variables

// define SharedPreferences object

private SharedPreferences savedValues;

@Override

public void onCreate(Bundle savedInstanceState) {

// other code goes here

// get SharedPreferences object

savedValues = getSharedPreferences(

"SavedValues" MODE_PRIVATE);

}

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

How to set up the instance variables

// define SharedPreferences object

private SharedPreferences savedValues;

@Override

public void onCreate(Bundle savedInstanceState) {

// other code goes here

// get SharedPreferences object

savedValues = getSharedPreferences(

"SavedValues" MODE_PRIVATE);

}

@Override

public void onPause() {

// save the instance variables

Editor editor = savedValues.edit();

editor.putString("billAmountString", billAmountString);

editor.putFloat("tipPercent", tipPercent);

editor.commit();

super.onPause();

}

@Override

public void onPause() {

// save the instance variables

Editor editor = savedValues.edit();

editor.putString("billAmountString", billAmountString);

editor.putFloat("tipPercent", tipPercent);

editor.commit();

super.onPause();

}

@Override

public void onResume() {

super.onResume();

// get the instance variables

billAmountString = savedValues.getString(

"billAmountString", "");

tipPercent = savedValues.getFloat("tipPercent", 0.15f);

}

@Override

public void onResume() {

super.onResume();

// get the instance variables

billAmountString = savedValues.getString(

"billAmountString", "");

tipPercent = savedValues.getFloat("tipPercent", 0.15f);

}

package com.murach.tipcalculator;

import java.text.NumberFormat;

import android.app.Activity;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.inputmethod.EditorInfo;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.TextView.OnEditorActionListener;

import android.content.SharedPreferences;

import android.content.Share dPreferences.Editor;

package com.murach.tipcalculator;

import java.text.NumberFormat;

import android.app.Activity;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.inputmethod.EditorInfo;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.TextView.OnEditorActionListener;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

public class TipCalculatorActivity extends A ctivity

implements OnEditorActionListener, OnClickListener {

// define variables for the widgets

private EditText billAmountEditText;

private TextView percentTextView;

private Button percentUpButton;

private Button percentDownButton;

private TextView tipTextView;

private TextView totalTextView;

// define SharedPreferences object

private SharedPreferences savedValues;

// define an instance variable for the tip percent

private String billAmountString = "";

private float tipPercent = 15f;

public class TipCalculatorActivity extends Activity

implements OnEditorActionListener, OnClickListener {

// define variables for the widgets

private EditText billAmountEditText;

private TextView percentTextView;

private Button percentUpButton;

private Button percentDownButton;

private TextView tipTextView;

private TextView totalTextView;

// define SharedPreferences object

private SharedPreferences savedValues;

// define an instance variable for the tip percent

private String billAmountString = "";

private float tipPercent = 15f;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_tip_calculator);

// get references to the widgets

billAmountEditText = (EditText)

findViewById(R.id.billAmountEditText);

percentTextView = (TextView)

findViewById(R.id.percentTextView);

percentUpButton = (Button)

findViewById(R.id.percentUpBut ton);

percentDownButton = (Button)

findViewById(R.id.percentDownButton);

tipTextView = (TextView)

findViewById(R.id.tipTextView);

totalTextView = (TextView)

findViewById(R.id.totalTextView);

// set the listeners

billAmountEditText.setOnEditorActionListener(this);

percentUpButton.setOnClickListener(this);

percentDownButton.setOnClickListener(this);

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_tip_calculator);

// get references to the widgets

billAmountEditText = (EditText)

findViewById(R.id.billAmountEditText);

percentTextView = (TextView)

findViewById(R.id.percentTextView);

percentUpButton = (Button)

findViewById(R.id.percentUpButton);

percentDownButton = (Button)

findViewById(R.id.percentDownButton);

tipTextView = (TextView)

findViewById(R.id.tipTextView);

totalTextView = (TextView)

findViewById(R.id.totalTextView);

// set the listeners

billAmountEditText.setOnEditorActionListener(this);

percentUpButton.setOnClickListener(this);

percentDownButton.setOnClickListener(this);

// get SharedPreferences object

savedValues = getSharedPreferences(

"SavedValues" MODE_PRIVATE);

}

@Override

public void onPause() {

// save the instance variables

Editor editor = savedValues.edit();

editor.putString("billAmountString", billAmountString);

editor.putFloat("tipPercent", tipPercent);

editor.commit();

super.onPause();

}

// get SharedPreferences object

savedValues = getSharedPreferences(

"SavedValues" MODE_PRIVATE);

}

@Override

public void onPause() {

// save the instance variables

Editor editor = savedValues.edit();

editor.putString("billAmountString", billAmountString);

editor.putFloat("tipPercent", tipPercent);

editor.commit();

super.onPause();

}

@Override

public void onResume() {

super.onResume();

// get the instance variables

billAmountString = savedValues.getString(

"billAmountString", "");

tipPercent = savedValues.getFloat("tipPercent", 0.15f);

// set the bill amount on its widget

billAmountEditText.setText(billAmountString);

// calculate and display

calculateAndDisplay();

}

@Override

public void onResume() {

super.onResume();

// get the instance variables

billAmountString = savedValues.getString(

"billAmountString", "");

tipPercent = savedValues.getFloat("tipPercent", 0.15f);

// set the bill amount on its widget

billAmountEditText.setText(billAmountString);

// calculate and display

calculateAndDisplay();

}

public void calculateAndDisplay() {

// get the bill amount

billAmountString = billAmountEditText.getText()

.toString();

float billAmount;

if (billAmountString.equals("")) {

billAmount = 0;

} else {

billAmount = Float.parseFloat(billAmountString);

}

// calculate tip and total

float tipAmount = billAmount * tipPercent;

float totalAmount = billAmount + tipAmount;

// display the other results with formatting

NumberFormat currency =

NumberFormat.getCurrencyInstance();

tipTextView.setText(currency.format(tipAmount));

totalTextView.setText(currency.format(totalAmount));

NumberFormat percent = NumberFormat.getPercentInstance();

percentTextView.setText(percent.format(tipPercent));

}

public void calculateAndDisplay() {

// get the bill amount

billAmountString = billAmountEditText.getText()

.toString();

float billAmount;

if (billAmountString.equals("")) {

billAmount = 0;

} else {

billAmount = Float.parseFloat(billAmountString);

}

// calculate tip and total

float tipAmount = billAmount * tipPercent;

float totalAmount = billAmount + tipAmount;

// display the other results with formatting

NumberFormat currency =

NumberFormat.getCurrencyInstance();

tipTextView.setText(currency.format(tipAmount));

totalTextView.setText(currency.format(totalAmount));

NumberFormat percent = NumberFormat.getPercentInstance();

percentTextView.setText(percent.format(tipPercent));

}

@Override

public boolean onEditorAction(TextView v, int actionId,

KeyEvent event) {

if (actionId == EditorInfo.IME_ACTION_DONE)

{

calculateAndDisplay();

}

return false;

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.percentDownButton:

tipPercent = tipPercent - .01f;

calculateAndDisplay();

break;

case R.id.percen tUpButton:

tipPercent = tipPercent + .01f;

calculateAndDisplay();

break;

}

}

}

@Override

public boolean onEditorAction(TextView v, int actionId,

KeyEvent event) {

if (actionId == EditorInfo.IME_ACTION_DONE)

{

calculateAndDisplay();

}

return false;

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.percentDownButton:

tipPercent = tipPercent - .01f;

calculateAndDisplay();

break;

case R.id.percentUpButton:

tipPercent = tipPercent + .01f;

calculateAndDisplay();

break;

}

}

}

apply plugin: 'com.android.application'

android {

compileSdkVersion 23

buildToolsVersion "23.0.1"

defaultConfig {

applicationId "com.murach.tipcalculator"

minSdkVersion 15

targetSdkVersion 23

versionCode 1

versionName "1.0"

}

buildTypes {

release {

minifyEnabled false

proguardFiles

getDefaultProguardFile('proguard -android.txt'),

'proguard-rules.pro'

}

}

}

apply plugin: 'com.android.application'

android {

compileSdkVersion 23

buildToolsVersion "23.0.1"

defaultConfig {

applicationId "com.murach.tipcalculator"

minSdkVersion 15

targetSdkVersion 23

versionCode 1

versionName "1.0"

}

buildTypes {

release {

minifyEnabled false

proguardFiles

getDefaultProguardFile('proguard-android.txt'),

'proguard-rules.pro'

}

}

}

dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat -v7:23.0.0'

}

dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:23.0.0'

}

<?xml version="1.0" encoding="utf -8"?>

<manifest xmlns:android=

"http://schemas.android.com/apk/res/android"

package="com.murach.tipcalculator" >

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name=".TipCalculatorActivity"

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>

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android=

"http://schemas.android.com/apk/res/android"

package="com.murach.tipcalculator" >

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name=".TipCalculatorActivity"

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>

<activity

android:name=".TipCalculatorActivity"

android:label="@string/app_name"

android:screenOrientation="portrait" >

The element that sets

the minimum and target SDKs

(not necessary with Android Studio)

<uses-sdk

android:minSdkVersion ="8"

android:targetSdkVersion="17" />

<activity

android:name=".TipCalculatorActivity"

android:label="@string/app_name"

android:screenOrientation="portrait">

The element that sets the minimum and target SDKs (not necessary with Android Studio)

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="17" />

1. Use a browser to find the Android Asset Studio by searching the

Internet.

2. Navigate to the page for generating launcher icons.

3. Generate the launcher icons, download them, and unzip them.

4. Copy the generated mipmap directories and their files into the

res directory of your app. This should overwrite any existing

launcher icons.

5. If necessary, modify the Android manifest file so it points to the

correct icon files.

1. Use a browser to find the Android Asset Studio by searching the Internet.

1. Navigate to the page for generating launcher icons.

2. Generate the launcher icons, download them, and unzip them.

3. Copy the generated mipmap directories and their files into the res directory of your app. This should overwrite any existing launcher icons.

4. If necessary, modify the Android manifest file so it points to the correct icon files.

app/src/main/res/ mipmap-hdpi/ic_launcher.png

mipmap-mdpi/ic_launcher.png

mipmap-xhdpi/ic_launcher.png

mipmap-xxhdpi/ic_launcher.png

mipmap-xxxhdpi/ic_launcher.png

A few attributes from the AndroidManifest.xml file

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

app/src/main/res/mipmap-hdpi/ic_launcher.png

mipmap-mdpi/ic_launcher.png

mipmap-xhdpi/ic_launcher.png

mipmap-xxhdpi/ic_launcher.png

mipmap-xxxhdpi/ic_launcher.png

A few attributes from the AndroidManifest.xml file

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"