Java Development for beginners

155
Android Development for Beginners © 2012 Edureka.in Pvt. Ltd, All rights reserved. 1 Android Development for Beginners ©2012 by Edureka.in, All rights reserved. No part of this document may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of Edureka.in, Incorporated.

description

Learn java easily

Transcript of Java Development for beginners

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    1

    Android Development for Beginners

    2012 by Edureka.in, All rights reserved. No part of this document may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of Edureka.in, Incorporated.

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    2

    TABLE OF CONTENTS

    CHAPTER 1: JAVA REVIEW ............................................................................... 5

    1.1 Creating Basic Java Applications .................................................................................................. 6

    1.2 Creating Applications in Packages .............................................................................................. 7

    1.3 Java Variables ...................................................................................................................................... 8

    1.4 Java Conditionals and Loops ....................................................................................................... 10

    1.5 Java Arrays ......................................................................................................................................... 13

    1.6 Java Array Lists ................................................................................................................................ 15

    Chapter 1 Lab Exercise ......................................................................................................................... 18

    CHAPTER 2: JAVA OBJECT ORIENTED CONCEPTS REVIEW ................................ 20

    2.1 Creating a Java Class .................................................................................................................. 21

    2.2 Improving the Java Class ......................................................................................................... 23

    2.3 Using Inheritance ........................................................................................................................ 26

    2.4 Understanding Interfaces ........................................................................................................ 31

    2.5 The Static Context ....................................................................................................................... 37

    Chapter 2 Lab Exercise ......................................................................................................................... 41

    CHAPTER 3: CREATING YOUR FIRST ANDROID APPLICATION ........................... 43

    3.1 The Hello World Application .................................................................................................. 44

    3.2 Working with the Emulator..................................................................................................... 46

    3.3 Strings ............................................................................................................................................. 51

    3.4 Drawables ...................................................................................................................................... 54

    3.5 Introducing the Manifest .......................................................................................................... 57

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    3

    3.6 Understanding the Activity Lifecycle ................................................................................... 59

    Chapter 3 Lab Exercise ......................................................................................................................... 61

    CHAPTER 4: CREATING LISTENERS .................................................................. 62

    4.1 Listeners Using an Inner Class ............................................................................................... 63

    4.2 Listeners Using an Interface ................................................................................................... 65

    4.3 Listeners By Variable Name .................................................................................................... 68

    4.4 Long Clicks ..................................................................................................................................... 71

    4.5 Keyboard Listeners .................................................................................................................... 75

    Chapter 4 Lab Exercise ......................................................................................................................... 77

    CHAPTER 5: UNDERSTANDING ANDROID VIEW CONTAINERS .......................... 79

    5.1 Linear Layout ............................................................................................................................... 80

    5.2 Relative Layout ............................................................................................................................ 83

    5.3 Table Layout ................................................................................................................................. 86

    5.4 List View ......................................................................................................................................... 89

    Chapter 5 Lab Exercise ......................................................................................................................... 92

    CHAPTER 6: ANDROID WIDGETS PART I .......................................................... 93

    6.1 Custom Buttons ............................................................................................................................ 94

    6.2 Toggle Buttons ............................................................................................................................. 96

    6.3 Checkboxes and Radio Buttons .............................................................................................. 99

    6.4 Spinners ........................................................................................................................................ 103

    Chapter 6 Lab Exercise ....................................................................................................................... 106

    CHAPTER 7: ANDROID WIDGETS PART II ....................................................... 108

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    4

    7.1 Autocomplete Text Box ......................................................................................................... 109

    7.2 Map View ...................................................................................................................................... 111

    7.3 Web Views ................................................................................................................................... 113

    7.4 Time and Date Pickers ............................................................................................................ 115

    Chapter 7 Lab Exercise ....................................................................................................................... 119

    CHAPTER 8: COMMUNICATING BETWEEN ACTIVITIES ................................... 120

    8.1 Switching Activities .................................................................................................................. 121

    8.2 Putting Extra ............................................................................................................................... 125

    8.3 Using Shared Preferences ...................................................................................................... 129

    Chapter 8 Lab Exercise ....................................................................................................................... 134

    CHAPTER 9: STORING INFORMATION ON THE DEVICE ................................... 135

    9.1 Internal Storage ......................................................................................................................... 136

    9.2 External Storage ........................................................................................................................ 141

    9.3 Web Communication and Storage ....................................................................................... 145

    Chapter 9 Lab Exercise ....................................................................................................................... 148

    CHAPTER 10: AUDIO AND VIDEO .................................................................. 149

    10.1 Playing Audio with the MediaPlayer ............................................................................... 150

    10.2 Playing Video with the MediaPlayer ............................................................................... 153

    Chapter 10 Lab Exercise ..................................................................................................................... 155

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    5

    Chapter 1: Java Review

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    6

    1.1 Creating Basic Java Applications public class HelloWorld

    {

    public static void main(String[] args)

    {

    System.out.println("Hello World from Java");

    }

    }

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    7

    1.2 Creating Applications in Packages package edureka.in.androidcourse;

    public class HelloWorld

    {

    public static void main(String[] args)

    {

    System.out.println("Hello World from Java");

    }

    }

    Notes _____________________________________________________________ _____________________________________________________________

    _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    8

    1.3 Java Variables package edureka.in.androidcourse;

    public class Variables

    {

    public static void main(String[] args)

    {

    // Variables

    int age; //Variable Declaration

    age= 37;

    float gpa = 3.77f; //Delcaration and Initialization

    double preciseNumner = 1.000005;

    byte score = 12;

    short tennisScore = 30;

    long socialSecNumber = 650162727;

    boolean isPlaying = true;

    char letterGrade = 'A';

    System.out.println("Our integer value is: " + age);

    System.out.println("Our floating point value is: " +

    gpa);

    System.out.println("Out boolean value is: " + isPlaying);

    /*

    Aritmatic Operators

    +, - , *, /

    ++ Increment Operator adds One

    -- Decrement Operator subtracts One

    % Modulus Operator

    Combined Assignment Operators

    += Add (or concatenate) and then assign

    -= Subtract then Assign

    *= Multiply then Assign

    /= Divide then Assign

    */

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    9

    System.out.println("Next year I will be " + (++age));

    System.out.println("17 % 3 = " + (17%3)); tennisScore += 10; //tennisScore = tennisScore+10;

    System.out.println("Tennis Score= " + tennisScore);

    }

    }

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    10

    1.4 Java Conditionals and Loops Conditionals.java package edureka.in.androidcourse;

    public class Conditionals

    {

    public static void main(String[] args)

    {

    int age = 37;

    boolean citizen = true;

    if(age>=18 && citizen)

    {

    //True

    System.out.println("You are eligible to vote.");

    } else

    {

    //False

    System.out.println("You are ineligible to vote.");

    }

    }

    }

    ComplexConditionals.java package edureka.in.androidcourse;

    public class ComplexConditionals

    {

    public static void main(String[] args)

    {

    int age =60;

    if(age

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    11

    System.out.println("These are your money-making

    years.");

    } else if(age

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    12

    }

    }

    Loops.java package edureka.in.androidcourse;

    public class Loops

    {

    public static void main(String[] args)

    {

    /*

    int x = 0;

    while(x < 101)

    {

    System.out.println(x);

    x++;

    }

    int y=300;

    do

    {

    System.out.println(y);

    y+=5;

    }while(y < 251);

    */

    for(int z=100; z > -1; z=z-5)

    {

    System.out.println(z);

    }

    }

    }

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    13

    1.5 Java Arrays package edureka.in.androidcourse;

    public class Arrays

    {

    public static void main(String[] args)

    {

    int[] agesOfFamily;

    agesOfFamily = new int[6];

    agesOfFamily[0] = 37;

    agesOfFamily[1] = 69;

    agesOfFamily[2] = 65;

    agesOfFamily[3] = 31;

    agesOfFamily[4] = 4;

    agesOfFamily[5] = 2;

    System.out.println("My adorable nephew is " +

    agesOfFamily[5] + " years old");

    String[] familyMembers;

    familyMembers = new String[5];

    familyMembers[0] = "Mark";

    familyMembers[1] = "Joan";

    familyMembers[2] = "Rick";

    familyMembers[3] = "Brett";

    familyMembers[4] = "Rose";

    System.out.println("My grandmother's name is " +

    familyMembers[4]);

    for(int i=0; i < familyMembers.length; i++)

    {

    System.out.println(familyMembers[i]);

    }

    }

    }

    Notes _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    14

    _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    15

    1.6 Java Array Lists package edureka.in.androidcourse;

    import java.util.*;

    public class ArrayLists

    {

    public static void main(String[] args)

    {

    ArrayList airlines = new ArrayList();

    System.out.println("Array list airline initial size: " +

    airlines.size());

    airlines.add("American");

    airlines.add("Delta");

    airlines.add("United");

    airlines.add("US Airways");

    airlines.add("jetBlue");

    airlines.add("Southwest");

    System.out.println("Array list airline initial size: " +

    airlines.size());

    System.out.println("Airlines in the list: " + airlines);

    System.out.println("The first airline: " +

    airlines.get(0));

    System.out.println("The last airline: " +

    airlines.get(5));

    airlines.remove(3);

    System.out.println("The third airlines is now: " +

    airlines.get(3));

    }

    }

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    16

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    17

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    18

    Chapter 1 Lab Exercise 1. Create a new Java class and create an ArrayList called

    players that has the following members:

    Joey

    Thomas

    Joan

    Sarah

    Freddie

    Aaron

    2. Create a second ArrayList called battingAverages. Populate the ArrayList with the

    following values:

    .333

    .221

    .401

    .297

    .116

    .250

    3. By accessing each element of both ArrayLists output each name and batting average in row. Place the

    next name and batting average in the subsequent row.

    4. Calculate and output the team average batting average.

    5. Add the name Horace and the batting average .232 to the appropriate ArrayLists. (Use the appropriate

    ArrayList method.) Re-output the names and batting

    averages as instructed in step 3. Also recalculate

    the team average. You should refactor the code at

    this point so that the routine to output the

    information and calculate the team average are is

    not repeated. Instead, use function calls.

    6. Sort the ArrayList by name. (You may need to investigate the methods associated with the

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    19

    ArrayList class.) Note that now that you have done

    this the batting averages no longer correspond to

    the correct players. Look up the HashTable class in

    the java documentation.

    (http://docs.oracle.com/javase/1.4.2/docs/api/java/u

    til/Hashtable.html) Use this documentation to

    recreate the inital data as a HashTable.

    7. Access each element pair in the HashTable and output the name and batting average to the console.

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    20

    Chapter 2: Java Object Oriented Concepts Review

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    21

    2.1 Creating a Java Class Animal.java package edureka.in.android;

    public class Animal_driver {

    /**

    * @param args

    */

    public static void main(String[] args) {

    Animal doggie = new Animal();

    doggie.name = "Rover";

    doggie.age = 3;

    doggie.length = 36;

    doggie.weight = 17;

    doggie.breathe();

    doggie.eat("Vittles");

    doggie.sleep();

    System.out.println("The animal's name is: " +

    doggie.name);

    System.out.println("The animal's age is: " +

    doggie.age);

    System.out.println("The animal's length is: " +

    doggie.length);

    }

    }

    Animal_driver.java package edureka.in.android;

    public class Animal_driver {

    /**

    * @param args

    */

    public static void main(String[] args) {

    Animal doggie = new Animal();

    doggie.name = "Rover";

    doggie.age = 3;

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    22

    doggie.length = 36;

    doggie.weight = 17;

    doggie.breathe();

    doggie.eat("Vittles");

    doggie.sleep();

    System.out.println("The animal's name is: " +

    doggie.name);

    System.out.println("The animal's age is: " +

    doggie.age);

    System.out.println("The animal's length is: " +

    doggie.length);

    }

    }

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    23

    2.2 Improving the Java Class Animal.java package edureka.in.android;

    public class Animal {

    /*

    * Private- Accessible only within the class

    * Public- Accessible within the class and to other classes

    * Protected- Accessible within the class and to children

    of the class (subclassess)

    */

    //Properties of Animal

    //Known as "members"

    private int age;

    private int length;

    private String name;

    private int weight;

    public Animal(int age, int length, String name, int weight)

    {

    this.age = age;

    this.length = length;

    this.name = name;

    this.weight = weight;

    }

    public Animal()

    {

    }

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    if(age>0){

    this.age = age;

    }

    }

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    24

    public int getLength() {

    return length;

    }

    public void setLength(int length) {

    if(length>0){

    this.length = length;

    }

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public int getWeight() {

    return weight;

    }

    public void setWeight(int weight) {

    if(weight >0)

    {

    this.weight = weight;

    }

    }

    //Methods

    void eat(String food)

    {

    System.out.println("Animal is eating " + food);

    }

    void sleep()

    {

    System.out.println("Animal is sleeping");

    }

    void breathe()

    {

    System.out.println("Animal is breathing");

    }

    }

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    25

    Animal_driver.java package edureka.in.android;

    public class Animal_driver {

    /**

    * @param args

    */

    public static void main(String[] args) {

    Animal doggie = new Animal();

    doggie.setName("Rover");

    doggie.setAge(3);

    doggie.setLength(36);

    doggie.setWeight(17);

    doggie.breathe();

    doggie.eat("Vittles");

    doggie.sleep();

    System.out.println("The animal's name is: " +

    doggie.getName());

    System.out.println("The animal's age is: " +

    doggie.getAge());

    System.out.println("The animal's length is: " +

    doggie.getLength());

    Animal kittie = new Animal(1, 23, "Kitty", 5);

    System.out.println("The second animal's name is " +

    kittie.getName());

    kittie.eat("shrimp");

    }

    }

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    26

    2.3 Using Inheritance Animal.java package edureka.in.android;

    public class Animal {

    /*

    * Private- Accessible only within the class

    * Public- Accessible within the class and to other classes

    * Protected- Accessible within the class and to children

    of the class (subclassess)

    */

    //Properties of Animal

    //Known as "members"

    private int age;

    private int length;

    private String name;

    private int weight;

    public Animal(int age, int length, String name, int weight)

    {

    this.age = age;

    this.length = length;

    this.name = name;

    this.weight = weight;

    }

    public Animal()

    {

    }

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    if(age>0){

    this.age = age;

    }

    }

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    27

    public int getLength() {

    return length;

    }

    public void setLength(int length) {

    if(length>0){

    this.length = length;

    }

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public int getWeight() {

    return weight;

    }

    public void setWeight(int weight) {

    if(weight >0)

    {

    this.weight = weight;

    }

    }

    //Methods

    public void eat(String food)

    {

    System.out.println("Animal is eating " + food);

    }

    public void sleep()

    {

    System.out.println("Animal is sleeping");

    }

    public void breathe()

    {

    System.out.println("Animal is breathing");

    }

    }

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    28

    Fish.java package edureka.in.android;

    public class Fish extends Animal {

    boolean scales;

    public Fish(int age, int length, String name, int weight,

    boolean scales)

    {

    super(age, length, name, weight);

    this.scales = scales;

    }

    public Fish()

    {

    }

    //New Method specific to the fish class

    public void swim()

    {

    System.out.println("Fish is swimming");

    }

    //Overriding the method in the parent

    public void breathe()

    {

    System.out.println("Fish is breathing through its

    gills");

    }

    }

    Animal_driver.java package edureka.in.android;

    public class Animal_driver {

    /**

    * @param args

    */

    public static void main(String[] args) {

    Animal doggie = new Animal();

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    29

    doggie.setName("Rover");

    doggie.setAge(3);

    doggie.setLength(36);

    doggie.setWeight(17);

    doggie.breathe();

    doggie.eat("Vittles");

    doggie.sleep();

    System.out.println("The animal's name is: " +

    doggie.getName());

    System.out.println("The animal's age is: " +

    doggie.getAge());

    System.out.println("The animal's length is: " +

    doggie.getLength());

    Animal kittie = new Animal(1, 23, "Kitty", 5);

    System.out.println("The second animal's name is " +

    kittie.getName());

    kittie.eat("shrimp");

    Fish goldfish = new Fish(1, 2, "Goldie", 1, true);

    System.out.println("The fish's name is: " +

    goldfish.getName());

    goldfish.setWeight(2);

    System.out.println("The fish's weight is: " +

    goldfish.getWeight());

    }

    }

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    30

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    31

    2.4 Understanding Interfaces Animal.java package edureka.in.android;

    public class Animal {

    /*

    * Private- Accessible only within the class

    * Public- Accessible within the class and to other classes

    * Protected- Accessible within the class and to children

    of the class (subclassess)

    */

    //Properties of Animal

    //Known as "members"

    private int age;

    private int length;

    private String name;

    private int weight;

    public Animal(int age, int length, String name, int weight)

    {

    this.age = age;

    this.length = length;

    this.name = name;

    this.weight = weight;

    }

    public Animal()

    {

    }

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    if(age>0){

    this.age = age;

    }

    }

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    32

    public int getLength() {

    return length;

    }

    public void setLength(int length) {

    if(length>0){

    this.length = length;

    }

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public int getWeight() {

    return weight;

    }

    public void setWeight(int weight) {

    if(weight >0)

    {

    this.weight = weight;

    }

    }

    //Methods

    public void eat(String food)

    {

    System.out.println("Animal is eating " + food);

    }

    public void sleep()

    {

    System.out.println("Animal is sleeping");

    }

    public void breathe()

    {

    System.out.println("Animal is breathing");

    }

    }

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    33

    Pray.java package edureka.in.android;

    public interface Pray {

    void getChased();

    void getEaten();

    }

    Predator.java package edureka.in.android;

    public interface predator {

    void chasePray();

    void eatPray();

    }

    Lion.java package edureka.in.android;

    public class Lion extends Animal implements predator {

    public Lion(int age, int length, String name, int weight) {

    super(age, length, name, weight);

    // TODO Auto-generated constructor stub

    }

    public Lion() {

    // TODO Auto-generated constructor stub

    }

    @Override

    public void chasePray() {

    System.out.println("The Lion is chasing some pray");

    }

    @Override

    public void eatPray() {

    System.out.println("The Lion is eating it's pray");

    }

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    34

    public void roar(){

    System.out.println("ROARRRR!");

    }

    }

    Fish.java package edureka.in.android;

    public class Fish extends Animal implements Pray {

    boolean scales;

    public Fish(int age, int length, String name, int weight,

    boolean scales)

    {

    super(age, length, name, weight);

    this.scales = scales;

    }

    public Fish()

    {

    }

    //New Method specific to the fish class

    public void swim()

    {

    System.out.println("Fish is swimming");

    }

    //Overriding the method in the parent

    public void breathe()

    {

    System.out.println("Fish is breathing through its

    gills");

    }

    @Override

    public void getChased() {

    System.out.println("Fish is being chased.");

    }

    @Override

    public void getEaten() {

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    35

    System.out.println("Fish is being eaten. Goodbye");

    }

    }

    Animal_driver.java package edureka.in.android;

    public class Animal_driver {

    /**

    * @param args

    */

    public static void main(String[] args) {

    Animal doggie = new Animal();

    doggie.setName("Rover");

    doggie.setAge(3);

    doggie.setLength(36);

    doggie.setWeight(17);

    doggie.breathe();

    doggie.eat("Vittles");

    doggie.sleep();

    System.out.println("The animal's name is: " +

    doggie.getName());

    System.out.println("The animal's age is: " +

    doggie.getAge());

    System.out.println("The animal's length is: " +

    doggie.getLength());

    Animal kittie = new Animal(1, 23, "Kitty", 5);

    System.out.println("The second animal's name is " +

    kittie.getName());

    kittie.eat("shrimp");

    Fish goldfish = new Fish(1, 2, "Goldie", 1, true);

    System.out.println("The fish's name is: " +

    goldfish.getName());

    goldfish.setWeight(2);

    System.out.println("The fish's weight is: " +

    goldfish.getWeight());

    goldfish.getChased();

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    36

    Lion leo = new Lion(4, 240, "Leo The Lion", 420);

    leo.eatPray();

    }

    }

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    37

    2.5 The Static Context Animal.java package edureka.in.android;

    public class Animal {

    /*

    * Private- Accessible only within the class

    * Public- Accessible within the class and to other classes

    * Protected- Accessible within the class and to children

    of the class (subclassess)

    */

    //Properties of Animal

    //Known as "members"

    //Instance Member

    private int age;

    private int length;

    private String name;

    private int weight;

    public static int numAnimals;

    public Animal(int age, int length, String name, int weight)

    {

    this.age = age;

    this.length = length;

    this.name = name;

    this.weight = weight;

    numAnimals++;

    }

    public Animal()

    {

    numAnimals++;

    }

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    if(age>0){

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    38

    this.age = age;

    }

    }

    public int getLength() {

    return length;

    }

    public void setLength(int length) {

    if(length>0){

    this.length = length;

    }

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public int getWeight() {

    return weight;

    }

    public void setWeight(int weight) {

    if(weight >0)

    {

    this.weight = weight;

    }

    }

    //Methods

    public void eat(String food)

    {

    System.out.println("Animal is eating " + food);

    }

    public void sleep()

    {

    System.out.println("Animal is sleeping");

    }

    public void breathe()

    {

    System.out.println("Animal is breathing");

    }

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    39

    }

    Animal_driver.java package edureka.in.android;

    public class Animal_driver {

    /**

    * @param args

    */

    public static void main(String[] args) {

    Animal doggie = new Animal();

    doggie.setName("Rover");

    doggie.setAge(3);

    doggie.setLength(36);

    doggie.setWeight(17);

    doggie.breathe();

    doggie.eat("Vittles");

    doggie.sleep();

    System.out.println("The animal's name is: " +

    doggie.getName());

    System.out.println("The animal's age is: " +

    doggie.getAge());

    System.out.println("The animal's length is: " +

    doggie.getLength());

    Animal kittie = new Animal(1, 23, "Kitty", 5);

    System.out.println("The second animal's name is " +

    kittie.getName());

    kittie.eat("shrimp");

    Fish goldfish = new Fish(1, 2, "Goldie", 1, true);

    System.out.println("The fish's name is: " +

    goldfish.getName());

    goldfish.setWeight(2);

    System.out.println("The fish's weight is: " +

    goldfish.getWeight());

    goldfish.getChased();

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    40

    Lion leo = new Lion(4, 240, "Leo The Lion", 420);

    leo.eatPray();

    System.out.println("You produced " +

    Animal.numAnimals + " animals");

    }

    }

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    41

    Chapter 2 Lab Exercise

    1. Create the design for an object oriented soda

    machine. You may use any type of drawing, diagramming,

    notation or recording system youd like to create the

    design-- However do NOT write any Java code. The soda

    machine must have the following features:

    It Must Accept Money

    It Must Make Change if the user inserts more than the

    price of a soda ($1.50 initially)

    It Must Allow You (Administrator) Stock the Machine with

    Soda

    It Must Allow the User to Choose a Soda After Accepting

    Money

    It Must Indicate If a Choice is Out of stock

    It Must Refund Money Upon Request After a User Has

    Inserted Money

    It Must Dispense the Soda After Money is Inserted and

    Choice is Made

    It Must Display The Types of Soda Available (Cola, Diet

    Cola, Lemon-Lime, Diet Lemon-Lime and Root Beer.

    It must allow you (Administrator) to

    Your design document should represent the classes needed,

    fields needed, and the methods of each class. You must

    use at least two distinct classes for this exercise.

    Once your design document is complete, you may want to

    send it to the instructor for comments. This will be the

    basis of you constructing the classes that you will code

    in the following steps.

    2. Construct the soda machine and related classes based

    on your design document. If you need to change anything

    about your design, first map out the change in your

    design document and then make the change to your code.

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    42

    3. Create a driver class that will instantiate your soca

    machine and related classes. It is through this class

    that you should allow your soda machine in to interact

    with the user and administrator.

    Feel free to send your code to the instructor for

    feedback or comments. You may also post your solution in

    the forum to share with others.

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    43

    Chapter 3: Creating Your First Android Application

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    44

    3.1 The Hello World Application HelloWorld.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    public class HelloWorld extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    }

    }

    main.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    45

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    46

    3.2 Working with the Emulator Emulator

    DDMS View

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    47

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    48

    Emulator Control: Telephony

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    49

    Emulator Control: Geolocation

    Logcat Monitoring

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    50

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    51

    3.3 Strings L2pStringsActivity.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;

    import android.widget.TextView;

    public class L2pStringsActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    final Button btnPush =

    (Button)findViewById(R.id.button1);

    final TextView tv =

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

    btnPush.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

    tv.setText(R.string.thanks);

    }

    });

    }

    }

    main.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    52

    android:id="@+id/TextView"/>

    strings.xml

    Hello World,

    L2pStringsActivity!

    L2pStrings

    Please push the button for a big

    surprise!

    Push Me!

    Thank you for pushing the

    button

    Notes

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    53

    _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    54

    3.4 Drawables L2pDrawablesActivity.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;

    import android.widget.ImageView;

    public class L2pDrawablesActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    final Button pressMe =

    (Button)findViewById(R.id.buttonShowMark);

    final ImageView iv =

    (ImageView)findViewById(R.id.imageViewMark);

    pressMe.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

    iv.setImageResource(R.drawable.mark);

    }

    });

    }

    }

    main.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    55

    android:text="Drawables" />

    Notes _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    56

    _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    57

    3.5 Introducing the Manifest Manifest GUI

    Manifest.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    58

    android:label="@string/app_name" >

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    59

    3.6 Understanding the Activity Lifecycle L2pLifeCycleActivity.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    import android.util.Log;

    public class L2pLifeCycleActivity extends Activity {

    public static final String TAG = "LIFECYCLE";

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    Log.i(TAG, "in onCreate()");

    }

    @Override

    public void onStart()

    {

    super.onStart();

    Log.i(TAG, "in onStart()");

    }

    @Override

    public void onResume()

    {

    super.onResume();

    Log.i(TAG, "in onResume()");

    }

    @Override

    public void onRestart()

    {

    super.onRestart();

    Log.i(TAG, "in onRestart()");

    }

    @Override

    public void onPause()

    {

    Log.i(TAG, "in onPause()");

    super.onPause();

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    60

    }

    @Override

    public void onStop()

    {

    Log.i(TAG, "in onStop()");

    super.onStop();

    }

    @Override

    public void onDestroy()

    {

    Log.i(TAG, "in onDestroy()");

    super.onDestroy();

    }

    }

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    61

    Chapter 3 Lab Exercise

    1. Create a new Android 2.3.3 Application as demonstrated in the video lecture.

    2. Open main.xml in the layout folder and make sure you are in the Graphical Layout mode. Drag three

    TextView controls on to the application

    surface. They should appear one on top of another.

    3. Make sure they are IDed as follows: @+id/TVName

    @+id/TVEmail

    @+id/TVPhone

    4. Using the properties of the fields you have created populate them with your Name, Email and Phone

    number. (If you wish you can create String

    resources in the strings.xml and apply that strong

    resource to the fields.)

    5. Find an image of you and (if necessary) resize it so it appears no more than 100px by 100px. Make sure

    it is a .png, .jpg or .gif and drag it in to the

    Drawables folders. (Make sure the filename is all

    lowercase or an error will occur)

    6. Move back to the Graphical Layout mode with main.xml selected. Drag an ImageView object on to the

    application surface. Give the ImageView the id

    @+id/IVPictureOfMe.

    7. Below the ImageView place a Button. Put the text Show Picture on the button.

    8. Move to the Java code file and, as demonstrated in the video lecture, write the code to make local

    references to the Button and image view. (You will

    use the findViewById() method for this).

    9. As demonstrated in the video lecture create a listener on the button reference and code it so the

    image of you appears when the button is clicked.

    10. If you havent already created an AVD (Android Virtual Device) create one that is compatible with

    version 2.3.3 of Android. Test your application and

    find and correct any errors in your code.

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    62

    Chapter 4: Creating Listeners

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    63

    4.1 Listeners Using an Inner Class L2pListenersActivity.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.view.View.OnClickListener;

    import android.widget.Button;

    import android.widget.TextView;

    public class L2pListenersActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    final Button inner =

    (Button)findViewById(R.id.buttonInner);

    final TextView tv =

    (TextView)findViewById(R.id.textViewResult);

    inner.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View v) {

    //What does the button do?

    tv.setText("Thanks for pressing the inner

    button. ID: " + v.getId());

    }

    });

    }

    }

    main.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    64

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    65

    4.2 Listeners Using an Interface L2pListenersActivity.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.view.View.OnClickListener;

    import android.widget.Button;

    import android.widget.TextView;

    public class L2pListenersActivity extends Activity implements

    OnClickListener {

    Button interfaceB;

    Button interfaceB2;

    TextView tv;

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    final Button inner =

    (Button)findViewById(R.id.buttonInner);

    tv = (TextView)findViewById(R.id.textViewResult);

    interfaceB = (Button)findViewById(R.id.buttonInterface);

    interfaceB2 =

    (Button)findViewById(R.id.buttonInterface2);

    interfaceB.setOnClickListener(this);

    interfaceB2.setOnClickListener(this);

    inner.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View v) {

    //What does the button do?

    tv.setText("Thanks for pressing the inner

    button. ID: " + v.getId());

    }

    });

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    66

    }

    @Override

    public void onClick(View v) {

    int callerId = v.getId();

    switch(callerId)

    {

    case R.id.buttonInterface:

    tv.setText("Thanks for pressing the interface

    button. ID: " + v.getId());

    break;

    case R.id.buttonInterface2:

    tv.setText("Thanks for pressing interface

    Button 2. ID: " + v.getId());

    break;

    default:

    tv.setText("I don't know what you pushed!");

    }

    }

    }

    main.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    67

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    68

    4.3 Listeners By Variable Name L2pListenersActivity.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.view.View.OnClickListener;

    import android.widget.Button;

    import android.widget.TextView;

    public class L2pListenersActivity extends Activity implements

    OnClickListener {

    Button interfaceB;

    Button interfaceB2;

    Button variableB;

    TextView tv;

    private OnClickListener myOnClickListener = new

    OnClickListener()

    {

    @Override

    public void onClick(View v) {

    tv.setText("Thanks for press the variable

    button. ID: " + v.getId

    }

    };

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    final Button inner =

    (Button)findViewById(R.id.buttonInner);

    tv = (TextView)findViewById(R.id.textViewResult);

    interfaceB = (Button)findViewById(R.id.buttonInterface);

    interfaceB2 =

    (Button)findViewById(R.id.buttonInterface2);

    variableB = (Button)findViewById(R.id.buttonVariable);

    variableB.setOnClickListener(myOnClickListener);

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    69

    interfaceB.setOnClickListener(this);

    interfaceB2.setOnClickListener(this);

    inner.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View v) {

    //What does the button do?

    tv.setText("Thanks for pressing the inner

    button. ID: " + v.getId());

    }

    });

    }

    @Override

    public void onClick(View v) {

    int callerId = v.getId();

    switch(callerId)

    {

    case R.id.buttonInterface:

    tv.setText("Thanks for pressing the interface

    button. ID: " + v.getId());

    break;

    case R.id.buttonInterface2:

    tv.setText("Thanks for pressing interface

    Button 2. ID: " + v.getId());

    break;

    default:

    tv.setText("I don't know what you pushed!");

    }

    }

    }

    main.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    70

    android:text="@string/hello" />

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    71

    4.4 Long Clicks L2pListenersActivity.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.view.View.OnClickListener;

    import android.view.View.OnLongClickListener;

    import android.widget.Button;

    import android.widget.TextView;

    import android.widget.Toast;

    public class L2pListenersActivity extends Activity implements

    OnClickListener, OnLongClickListener {

    Button interfaceB;

    Button interfaceB2;

    Button variableB;

    TextView tv;

    private OnClickListener myOnClickListener = new

    OnClickListener()

    {

    @Override

    public void onClick(View v) {

    tv.setText("Thanks for press the variable

    button. ID: " + v.getId());

    }

    };

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    final Button inner =

    (Button)findViewById(R.id.buttonInner);

    tv = (TextView)findViewById(R.id.textViewResult);

    interfaceB = (Button)findViewById(R.id.buttonInterface);

    interfaceB2 =

    (Button)findViewById(R.id.buttonInterface2);

    variableB = (Button)findViewById(R.id.buttonVariable);

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    72

    variableB.setOnClickListener(myOnClickListener);

    interfaceB.setOnClickListener(this);

    interfaceB2.setOnClickListener(this);

    interfaceB.setOnLongClickListener(this);

    interfaceB2.setOnLongClickListener(this);

    inner.setOnLongClickListener(new OnLongClickListener() {

    @Override

    public boolean onLongClick(View v) {

    Toast.makeText(getApplicationContext(),

    "Thanks for longclicking inner", Toast.LENGTH_SHORT).show();

    return false;

    }

    });

    inner.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View v) {

    //What does the button do?

    tv.setText("Thanks for pressing the inner

    button. ID: " + v.getId());

    }

    });

    }

    @Override

    public void onClick(View v) {

    int callerId = v.getId();

    switch(callerId)

    {

    case R.id.buttonInterface:

    tv.setText("Thanks for pressing the interface

    button. ID: " + v.getId());

    break;

    case R.id.buttonInterface2:

    tv.setText("Thanks for pressing interface

    Button 2. ID: " + v.getId());

    break;

    default:

    tv.setText("I don't know what you pushed!");

    }

    }

    @Override

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    73

    public boolean onLongClick(View v) {

    Toast.makeText(getApplicationContext(), "Thanks for

    long clicking the implements buttons",

    Toast.LENGTH_SHORT).show();

    return false;

    }

    }

    main.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    74

    android:id="@+id/buttonVariable"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Variable" />

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    75

    4.5 Keyboard Listeners L2pKeyboardListenersActivity.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.KeyEvent;

    import android.view.View;

    import android.view.View.OnKeyListener;

    import android.widget.EditText;

    import android.widget.TextView;

    public class L2pKeyboardListenersActivity extends Activity {

    EditText userEntry;

    TextView tvResult;

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    userEntry =

    (EditText)findViewById(R.id.editTextUserEntry);

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

    userEntry.setOnKeyListener(new OnKeyListener() {

    @Override

    public boolean onKey(View v, int keyCode,

    KeyEvent event) {

    if(event.getAction() ==

    KeyEvent.ACTION_DOWN)

    {

    if(keyCode ==

    KeyEvent.KEYCODE_ENTER)

    {

    tvResult.setText(userEntry.getText());

    }

    }

    return false;

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    76

    }

    });

    }

    }

    main.xml

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    77

    Chapter 4 Lab Exercise

    1. Create a new Android 2.3.3 Application as demonstrated in the video lecture.

    2. Open the main.xml file and drag a TextView on to the application surface. Below the TextView drag three

    buttons. Make the text label on the first button

    say Inner Class. Label the second Text button

    Interface and the third button Variable.

    3. Switch to the Java code. Write the necessary code to give each button a local reference via

    findElementById(). Give the text view a local

    reference as well

    4. On the button labeled Inner Class create a listener and call back function via an inner class

    as demonstrated in the video lecture. When the

    button is clicked, the words Listener via Inner

    Class should appear in the TextView.

    5. On the button labeled Interface create a listener and call back function via an interface as

    demonstrated in the video lecture. When the button

    is clicked, the words Listener via Interface

    should appear in the TextView. Dont forget to

    extend your activity class with the appropriate

    interface.

    6. On the button labeled Variable create a listener and call back function via a variable as

    demonstrated in the video lecture. When the button

    is clicked, the words Listener via Variable should

    appear in the TextView.

    7. On each button implement a long click listener. Use the method that is labeled on the button to respond

    to the long click. Make the messages in the Text

    box Long Click via inner class, Long Click via

    interface, and Long click via variable.

    8. Below the third button add an EditText field in the main.xml file. Create a local reference in the Java

    file.

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    78

    9. As demonstrated in lecture, create a listener on the EditText so that when the user types a character, it

    appears the existing TextView clears and echos the

    character(s) in the EditText.

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    79

    Chapter 5: Understanding Android View Containers

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    80

    5.1 Linear Layout L2pLinearLayoutActivity.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    public class L2pLinearLayoutActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    }

    }

    main.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    81

    android:textAppearance="?android:attr/textAppearanceLarge"

    android:background="@color/Green"

    android:gravity="center_horizontal"

    android:layout_weight=".16"/>

    colors.xml

    #FFFFFF

    #00FF00

    #FF0000

    #FFFF00

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    82

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    83

    5.2 Relative Layout L2pRelativeLayoutActivity.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    public class L2pRelativeLayoutActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    }

    }

    main.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    84

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    85

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    86

    5.3 Table Layout L2pTableLayoutActivity package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    public class L2pTableLayoutActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    }

    }

    main.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    87

    android:src="@drawable/cat2" />

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    88

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    89

    5.4 List View L2pListViewActivity.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.AdapterView;

    import android.widget.AdapterView.OnItemClickListener;

    import android.widget.ArrayAdapter;

    import android.widget.ListView;

    import android.widget.Toast;

    public class L2pListViewActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    ListView bandList =

    (ListView)findViewById(R.id.bandList);

    final String[] bands = new String[] { "Journey",

    "Reo Speedwagon",

    "Heart",

    "Styx",

    "Foreigner",

    "Kansas",

    "Cheap Trick",

    "Kiss"

    };

    ArrayAdapter adapter = new

    ArrayAdapter(this, android.R.layout.simple_list_item_1,

    bands);

    bandList.setAdapter(adapter);

    bandList.setOnItemClickListener(new OnItemClickListener() {

    @Override

    public void onItemClick(AdapterView parent, View

    view, int position,

    long id) {

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    90

    Toast.makeText(getApplicationContext(),

    bands[position], Toast.LENGTH_SHORT).show();

    }

    });

    }

    }

    main.xml

    Notes

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    91

    _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    92

    Chapter 5 Lab Exercise

    1. Create a new Android 2.3.3 Application as demonstrated in the video lecture.

    2. As demonstrated in the video lecture add a ListView to the interface and make a local reference in the

    Java code.

    3. Create a String array called months of the year and populate with the names of the months.

    4. Create an ArrayAdapter object called adapter that uses the simple_list_item_I layout and uses your

    months array as a data source.

    5. Associate the adapter with your ListView reference as demonstrated in the video lecture.

    6. Implement a click listener on the ListView so that a Toast appears with each click indicating the name of

    the month clicked.

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    93

    Chapter 6: Android Widgets Part I

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    94

    6.1 Custom Buttons L2pCustomButton.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;

    import android.widget.Toast;

    public class L2pCustomButtonActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    final Button button = (Button)findViewById(R.id.button1);

    button.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

    Toast.makeText(getApplicationContext(),

    "You pressed the custom button!", Toast.LENGTH_SHORT).show();

    }

    });

    }

    }

    main.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    95

    custom_button.xml

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    96

    6.2 Toggle Buttons L2pToggleButtonsActivity.java package edureka.in.android;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;

    import android.widget.Toast;

    import android.widget.ToggleButton;

    public class L2pToggleButtonsActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    final ToggleButton toggleOne =

    (ToggleButton)findViewById(R.id.toggleButtonEngineOne);

    final ToggleButton toggleTwo =

    (ToggleButton)findViewById(R.id.toggleButtonEngineTwo);

    final Button statusButton =

    (Button)findViewById(R.id.buttonCheckEngineStatus);

    statusButton.setOnClickListener(new

    View.OnClickListener() {

    @Override

    public void onClick(View v) {

    if(toggleOne.isChecked() &&

    toggleTwo.isChecked())

    {

    Toast.makeText(getApplicationContext(), "Both Engines are

    running", Toast.LENGTH_SHORT).show();

    } else

    {

    if(toggleOne.isChecked())

    {

    Toast.makeText(getApplicationContext(), "Engine One is

    running", Toast.LENGTH_SHORT).show();

    } else

    {

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    97

    if(toggleTwo.isChecked())

    {

    Toast.makeText(getApplicationContext(), "Engine Two is

    running", Toast.LENGTH_SHORT).show();

    } else

    {

    Toast.makeText(getApplicationContext(), "Neither engine is

    running", Toast.LENGTH_SHORT).show();

    }

    }

    }

    }

    });

    }

    }

    main.xml

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    98

    Notes _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________

  • Android Development for Beginners

    2012 Edureka.in Pvt. Ltd, All rights reserved.

    99

    6.3 Checkboxes and Radi