Android Programming Guide for Beginners

download Android Programming Guide for Beginners

of 308

Transcript of Android Programming Guide for Beginners

  • 8/13/2019 Android Programming Guide for Beginners

    1/308

    1

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

  • 8/13/2019 Android Programming Guide for Beginners

    2/308

    2

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    CCCC o n t e n t s o n t e n t s o n t e n t s o n t e n t s

    1 . S t a r t i n g A n d r o i d D e v e l o p m e n t - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2

    2 . A c t i v i t i e s A n d I n t e n t s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 4 1

    3 . U s e r I n t e r f a c e L a y o u t s a n d C o n t r o l s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 5 9

    4 . L i s t V i e w s a n d S Q L L i t e - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 9 8

    5 . S e r v i c e s i n A n d r o i d - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 3 7

    6 . C o n t e n t P r o v i d e r s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 5 5

    7 . A p p O r g a n i z a t i o n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 8 6

    8 . A d v a n c e U I C o m p o n e n t s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2 0 0

    9 . A n i m a t i o n a n d G r a p h i c s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2 4 5

    1 0 . G P S a n d S e n s o r s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2 9 4

  • 8/13/2019 Android Programming Guide for Beginners

    3/308

    3

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    C h a p t e r C h a p t e r C h a p t e r C h a p t e r 1 111 S t a r t i n g S t a r t i n g S t a r t i n g S t a r t i n g A n d r o i d A n d r o i d A n d r o i d A n d r o i d D e v e l o p m e n t D e v e l o p m e n t D e v e l o p m e n t D e v e l o p m e n t

  • 8/13/2019 Android Programming Guide for Beginners

    4/308

    4

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    W e l c o m eW e l c o m eW e l c o m eW e l c o m e t o t h e w o r l d o f A n d r o i d A p p l i c a t i o n D e v e l o p m e n t ! I n t h i s b o o k , w e ' l l e x p l o r e h o w t o w r i t e a p p s f o r A n d r o i d d e v i c e s u s i n g E c l i p s e a n d t h e A n d r o i d S D K . A s o f A u g u s t

    2 0 1 2 , A n d r o i d d e v i c e s h e l d 7 0 % o f t h e m a r k e t s h a r e o f a l l s m a r t p h o n e s , a n d s a l e s o f M a r c h t a b l e t s a r e i n c r e a s i n g a s w e l l . T h e r e h a s n e v e r b e e n a n e x p l o s i o n o f p e r s o n a l c o m p u t i n g d e v i c e s l i k e t h e o n e w e ' r e s e e i n g n o w . T h e o p p o r t u n i t i e s f o r d e v e l o p e r s i n t h i s m a r k e t a r e t r e m e n d o u s .

    B u t t o t a k e a d v a n t a g e o f t h e s e o p p o r t u n i t i e s , c e r t a i n k n o w l e d g e a n d s k i l l s a r e r e q u i r e d . T h a t ' s w h a t t h i s b o o k i s a l l a b o u t : t o p r o v i d e y o u w i t h t h e b a s i c s k i l l s y o u ' l l n e e d t o d e v e l o p c o m p e l l i n g a n d w e l l w r i t t e n A n d r o i d a p p l i c a t i o n s . W i t h j u s t a b i t o f e f f o r t o n y o u r p a r t , y o u ' l l b e w e l l o n y o u r w a y t o d e v e l o p i n g t h e n e x t g r e a t a p p ! T h e w o r l d w i l l b e a t a p a t h t o y o u r d o o r ! Y o u ' l l b e f a m o u s ! R i c h b e y o n d y o u r w i l d e s t i m a g i n i n g s !

    O K , b a c k t o t h e r e a l w o r l d . W e c a n ' t p r o m i s e f a m e o r w e a l t h , b u t w e c a n p r o m i s e t h a t i f y o u

    r e a d t h i s b o o k a n d c o m p l e t e t h e e x e r c i s e s a t t h e e n d o f e a c h c h a p t e r , y o u ' l l b e a b l e t o d e v e l o p a p p l i c a t i o n s t h a t c a n b e s o l d o n G o o g l e P l a y a n d r e a c h a w i d e r a u d i e n c e t h a n a n y d e s k t o p a p p l i c a t i o n w o u l d e v e r r e a c h .

    W h a tW h a tW h a tW h a t y o uy o uy o uy o u n e e d n e e d n e e d n e e d t o t ot ot o k n o w k n o w k n o w k n o w

    I n t h i s b o o k , w e a s s u m e t h a t y o u h a v e s o m e e x p e r i e n c e w i t h O b j e c t O r i e n t e d P r o g r a m m i n g u s i n g t h e J a v a p r o g r a m m i n g l a n g u a g e . T h e A n d r o i d S D K i s w r i t t e n i n J a v a , a n d t h e a p p l i c a t i o n s w e ' l l b e w r i t i n g i n t h i s b o o k w i l l b e ( f o r t h e m o s t p a r t ) i n J a v a a s w e l l . Y o u d o n ' t n e e d t o b e a n e x p e r t , b u t y o u w i l l n e e d t o b e f a m i l i a r w i t h c r e a t i n g c l a s s e s a n d o b j e c t s , a s w e l l a s t h e b a s i c p r i n c i p l e s o f o b j e c t o r i e n t e d d e s i g n : i n h e r i t a n c e , e n c a p s u l a t i o n , p o l y m o r p h i s m , a n d s o o n .

    W h a tW h a tW h a tW h a t y o uy o uy o uy o u n e e d n e e d n e e d n e e d t o t ot ot o h a v e h a v e h a v e h a v e

    T h e A n d r o i d d e v e l o p m e n t t o o l s a n d E c l i p s e s o f t w a r e r u n o n W i n d o w s , O S X , a n d L i n u x . Y o u w i l l n e e d a d e v e l o p m e n t c o m p u t e r r u n n i n g o n e o f t h e s e o p e r a t i n g s y s t e m s . I f y o u w i l l b e d e v e l o p i n g o n a M a c , m a k e s u r e t h a t i t h a s a n I n t e l p r o c e s s o r .

    T h e e a s i e s t w a y t o d e v e l o p a p p s f o r t h e A n d r o i d p l a t f o r m i s t o d o s o w i t h i n E c l i p s e : a n

    I n t e g r a t e d D e v e l o p m e n t E n v i r o n m e n t t h a t i s p a r t i c u l a r l y w e l l s u i t e d f o r w o r k i n g w i t h J a v a . I n t h e n e x t s e c t i o n , w e ' l l i n s t a l l E c l i p s e a n d t h e A n d r o i d S D K a n d A D T ( A n d r o i d D e v e l o p m e n t T o o l s ) p l u g i n . A f t e r t h a t , w e ' l l w a l k t h r o u g h t h e p r o c e s s o f k e e p i n g t h e t o o l s u p d a t e d . T h e n w e ' l l c r e a t e a n A V D ( A n d r o i d V i r t u a l D e v i c e : a t e m p l a t e f o r r u n n i n g a n

  • 8/13/2019 Android Programming Guide for Beginners

    5/308

  • 8/13/2019 Android Programming Guide for Beginners

    6/308

  • 8/13/2019 Android Programming Guide for Beginners

    7/308

    7

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    1 . 1 . 2 E c l i p s e

    I n t h i s b o o k , w e w i l l u s e t h e E c l i p s e I D E t o d e v e l o p o u r p r o j e c t s . I t i s f r e e , r e l a t i v e l y s t a b l e , a n d e a s y t o u s e . S e t t i n g i t u p f o r A n d r o i d d e v e l o p m e n t c a n b e a c h a l l e n g e , b u t o n c e p r o p e r l y

    i n s t a l l e d , i t s h o u l d g i v e y o u v e r y f e w p r o b l e m s . N a v i g a t e t o h t t p : / / w w w . e c l i p s e . o r g / d o w n l o a d s / . S e l e c t y o u r o p e r a t i n g s y s t e m a s s h o w n h e r e :

    F o r A n d r o i d d e v e l o p m e n t , c h o o s e e i t h e r t h e E c l i p s e C l a s s i c v e r s i o n , t h e E c l i p s e I D E f o r J a v a E E D e v e l o p e r s v e r s i o n , o r t h e E c l i p s e f o r M o b i l e D e v e l o p e r s v e r s i o n . ( T h e C l a s s i c v e r s i o n i s f i n e f o r m o s t u s e s . T h e M o b i l e D e v e l o p e r s v e r s i o n a d d s s u p p o r t f o r t h e C l a n g u a g e . )

    D o w n l o a d t h e a r c h i v e a n d e x t r a c t i t . C o p y t h e e c l i p s e f o l d e r t o y o u r h o m e f o l d e r ( o r a f o l d e r t h a t y o u o w n ) . W h e n f i n i s h e d , m a k e s u r e E c l i p s e i s p r o p e r l y i n s t a l l e d b y r u n n i n g t h e e x e c u t a b l e i n s i d e t h e e c l i p s e f o l d e r . I f y o u w a n t t o m a k e a s h o r t c u t t o t h e e x e c u t a b l e , d o s o .

    E a c h t i m e y o u r u n E c l i p s e , i t w i l l p r o m p t y o u t o e n t e r a w o r k s p a c e . C h o o s e a l o c a t i o n ( f o r

    http://www.eclipse.org/downloads/
  • 8/13/2019 Android Programming Guide for Beginners

    8/308

    8

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    t h i s f i r s t r u n , i t d o e s n ' t m a t t e r w h e r e ) :

    Y o u c a n n a v i g a t e t o a p a r t i c u l a r f o l d e r b y c l i c k i n g o n t h e B r o w s e . . . b u t t o n . D o n o t s e l e c t t h e U s e t h i s a s t h e d e f a u l t a n d d o n o t a s k a g a i n c h e c k b o x , b e c a u s e w e a l w a y s w a n t t h e a b i l i t y t o c h a n g e t h e w o r k s p a c e a t n e e d !

    A f t e r c l i c k i n g O K , Y o u s h o u l d s e e a w i n d o w s i m i l a r t o t h i s :

  • 8/13/2019 Android Programming Guide for Beginners

    9/308

    9

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    I f t h i s w i n d o w i s d i s p l a y e d , c o n g r a t u l a t i o n s . E c l i p s e i s i n s t a l l e d p r o p e r l y .

    1 . 1 . 3 A n d r o i d S D K

    O n c e e c l i p s e i s p r o p e r l y i n s t a l l e d , i t ' s t i m e t o b e g i n i n s t a l l i n g t h e A n d r o i d S D K a n d p l u g i n t o o l s . P o i n t y o u r b r o w s e r t o h t t p : / / d e v e l o p e r . a n d r o i d . c o m / s d k / i n d e x . h t m l . M y a d v i c e i s t o

    r e a d e v e r y t h i n g o n t h i s s i t e b e f o r e s t a r t i n g t o i n s t a l l a n y t h i n g . T h e r e i s a l o t o f g o o d i n f o r m a t i o n h e r e , b u t i t i s s p r e a d o v e r s e v e r a l p a g e s . I t p a y s t o g e t a g o o d o v e r v i e w o f t h e p r o c e s s b e f o r e p u s h i n g t h e b u t t o n !

    T h e s e p a g e s a r e a l s o t a i l o r e d t o y o u r o p e r a t i n g s y s t e m , s o i f y o u ' r e n o t i n s t a l l i n g o n a M a c , s o m e o f t h e i n s t r u c t i o n s m a y b e d i f f e r e n t t h a t w h a t i s s h o w n h e r e . A g a i n , r e a d t h e

    http://developer.android.com/sdk/index.html
  • 8/13/2019 Android Programming Guide for Beginners

    10/308

    1 0

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    i n f o r m a t i o n o n t h i s s i t e b e f o r e d o i n g a n y t h i n g .

    W h e n y o u ' v e r e a d a l l t h e i n f o r m a t i o n , c l i c k o n t h e D o w n l o a d b u t t o n o n t h e p a g e :

    Y o u ' l l w a n t t o s a v e t h i s . z i p f i l e t o a s a f e l o c a t i o n o n y o u r c o m p u t e r . E x t r a c t t h e . z i p f i l e t o a f o l d e r t h a t y o u h a v e r i g h t s t o ( a f o l d e r i n y o u r h o m e d i r e c t o r y ) , a n d n o t e t h e l o c a t i o n . W e ' l l n e e d t h i s l o c a t i o n t o t e l l E c l i p s e w h e r e t h e S D K i s .

    1 . 1 . 4 A n d r o i d A D T

    N e x t , w e ' l l i n s t a l l t h e A D T ( A n d r o i d D e v e l o p m e n t T o o l s ) . T h i s i s a p l u g i n f o r E c l i p s e t h a t s p e c i f i c a l l y t a r g e t s t h e A n d r o i d p l a t f o r m . S t a r t E c l i p s e ( a g a i n , a n y w o r k s p a c e l o c a t i o n i s f i n e ) , a n d n a v i g a t e t o

    H e l p | I n s t a l l N e w S o f t w a r e . . . :

  • 8/13/2019 Android Programming Guide for Beginners

    11/308

    1 1

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    T h e I n s t a l l a t i o n s c r e e n s h o w n h e r e w i l l a p p e a r . T y p e h t t p s : / / d l - s s l . g o o g l e . c o m / a n d r o i d / e c l i p s e / i n t h e W o r k w i t h : t e x t b o x a n d h i t t h e E n t e r k e y . Y o u s h o u l d s e e t h e D e v e l o p e r T o o l s l i s t e d a s s h o w n i n t h e v i e w b e l o w .

    https://dl-ssl.google.com/android/eclipse/https://dl-ssl.google.com/android/eclipse/
  • 8/13/2019 Android Programming Guide for Beginners

    12/308

    1 2

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    S e l e c t t h e m a i n D e v e l o p e r T o o l s c h e c k b o x t o c h o o s e a l l t h e d e v e l o p m e n t t o o l s , a n d c l i c k N e x t t o c o n t i n u e . E c l i p s e w i l l c a l c u l a t e r e q u i r e m e n t s a n d s h o w t h e c u r r e n t i n s t a l l a t i o n s t a t u s o f t h e t o o l s i n t h e D e t a i l s p a n e l . C l i c k N e x t , a c c e p t a l l t h e l i c e n s e a g r e e m e n t s f o r t h e t o o l s y o u a r e a b o u t t o i n s t a l l , a n d c l i c k F i n i s h .

  • 8/13/2019 Android Programming Guide for Beginners

    13/308

    1 3

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    T h e A D T w i l l t a k e a w h i l e t o d o w n l o a d , s o n o w i s a g o o d t i m e t o g o h a v e l u n c h .

    1 . 1 . 5 B i n d i n g t o t h e A n d r o i d S D K

    A f t e r t h e A D T h a s b e e n d o w n l o a d e d , y o u m u s t t e l l E c l i p s e w h e r e t o f i n d t h e A n d r o i d S D K . F o r t u n a t e l y , t h i s i s a v e r y s i m p l e p r o c e s s : i n E c l i p s e , n a v i g a t e t o t h e E c l i p s e | P r e f e r e n c e s m e n u s e l e c t i o n :

    Y o u w i l l s e e t h e P r e f e r e n c e s w i n d o w . S e l e c t A n d r o i d o n t h e l e f t o f t h e w i n d o w , a n d e n t e r t h e l o c a t i o n i n w h i c h y o u e x t r a c t e d t h e S D K f i l e s :

  • 8/13/2019 Android Programming Guide for Beginners

    14/308

    1 4

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    ( Y o u c a n a l s o n a v i g a t e t o t h e l o c a t i o n w h e r e t h e f i l e s a r e s t o r e d b y c l i c k i n g t h e B r o w s e . . . b u t t o n . ) C l i c k O K a t t h e b o t t o m o f t h e w i n d o w . N o t e t h a t t h e S D K T a r g e t s p a n e l w i l l n o t s h o w i n s t a l l e d t a r g e t s u n t i l t h e S D K i t s e l f i s u p d a t e d . A t t h i s p o i n t , i t ' s a g o o d i d e a t o r e s t a r t E c l i p s e t o m a k e s u r e t h e A D T p l u g i n i s p r o p e r l y i n s t a l l e d .

    1 . 1 . 6 R u n n i n g t h e S D K U p d a t e P r o c e s s

    T h e S D K w e d o w n s l o a d e d a n d i n s t a l l e d i n t h e l a s t s t e p o n l y c o n t a i n s t h e c o r e p a c k a g e , w h i c h a l l o w s u s t o i n s t a l l t h e r e s t o f t h e S D K p a c k a g e s . I n t h e E c l i p s e m e n u , n a v i g a t e t o W i n d o w

    |

    A n d r o i d S D K M a n a g e r a s s h o w n

  • 8/13/2019 Android Programming Guide for Beginners

    15/308

    1 5

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    Y o u w i l l s e e t h e A n d r o i d S D K M a n a g e r w i n d o w s h o w n b e l o w . A t t h e t o p o f t h e w i n d o w , t h e l o c a t i o n o f t h e S D K f i l e s i s s h o w n ; t h i s w i l l b e t h e s a m e l o c a t i o n y o u e n t e r e d i n t h e p r e v i o u s s t e p .

    F o r t h e w o r k w e a r e g o i n g t o d o i n t h e f i r s t p a r t o f t h e b o o k , i t i s o n l y n e c e s s a r y t o i n s t a l l t h e

  • 8/13/2019 Android Programming Guide for Beginners

    16/308

    1 6

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    A n d r o i d 2 . 3 . 3 S D K P l a t f o r m a n d G o o g l e A P I s , a s w e l l a s t h e l a t e s t v e r s i o n s o f t h e A n d r o i d S D K T o o l s a n d A n d r o i d S D K P l a t f o r m - t o o l s ( u n d e r t h e T o o l s f o l d e r a t t h e t o p o f t h e l i s t i n g ) . A f t e r m a k i n g t h e s e s e l e c t i o n s , c l i c k o n t h e I n s t a l l p a c k a g e s . . . b u t t o n a t t h e b o t t o m r i g h t o f t h e w i n d o w .

    T h i s p r o c e s s w i l l t a k e a c o n s i d e r a b l e a m o u n t o f t i m e . I t ' s a g r e a t t i m e t o t a k e a b r e a k , b u t k e e p

    a n e y e o n t h e p r o c e s s , b e c a u s e y o u w i l l b e a s k e d t o a c c e p t l i c e n s e a g r e e m e n t s , e t c . b e f o r e t h e a c t u a l i n s t a l l a t i o n p r o c e s s b e g i n s .

    T h e i n s t a l l a t i o n s h o u l d c o m p l e t e s u c c e s s f u l l y . O n c e a l l t h e f i l e s a r e i n p l a c e , r e s t a r t E c l i p s e t o m a k e s u r e t h e i n s t a l l a t i o n i s a s u c c e s s . I f y o u h a v e p r o b l e m s i n s t a l l i n g a n y c o m p o n e n t o f t h e t o o l s o r E c l i p s e i t s e l f , m a k e s u r e t h a t :

    E c l i p s e i s i n s t a l l e d i n a f o l d e r t o w h i c h y o u h a v e f u l l r i g h t s ( s u c h a s a s u b f o l d e r o f y o u r

    h o m e f o l d e r ) . I f o n W i n d o w s , d o n o t a t t e m p t t o i n s t a l l E c l i p s e f o r a l l u s e r s , a s n o t a l l u s e r s w i l l h a v e r i g h t s o n t h e i n s t a l l a t i o n t a r g e t f o l d e r . T h i s i s p r o b a b l y t h e n u m b e r o n e c a u s e o f i n s t a l l a t i o n p r o b l e m s w i t h E c l i p s e .

    T h e A n d r o i d S D K s h o u l d a l s o b e i n s t a l l e d i n a f o l d e r t o w h i c h y o u h a v e f u l l r i g h t s . I l i k e t o c r e a t e a D e v e l o p e r f o l d e r i n m y h o m e f o l d e r a n d t h e n c r e a t e b o t h a n E c l i p s e f o l d e r a n d a n A n d r o i d S D K f o l d e r i n s i d e o f t h a t . D o w n l o a d E c l i p s e t o t h e E c l i p s e f o l d e r , a n d e x t r a c t t h e A n d r o i d S D K i n t o t h e A n d r o i d S D K f o l d e r , t h e n b i n d t h e S D K t o E c l i p s e u s i n g t h e p r o c e s s d i s c u s s e d a b o v e .

    M a k e s u r e y o u h a v e i n s t a l l e d t h e A D T b e f o r e a t t e m p t i n g t o b i n d t o t h e S D K o r i n s t a l l u p d a t e s t o i t . T h e A D T p l u g i n a d d s t h e A n d r o i d s p e c i f i c m e n u i t e m s t o t h e E c l i p s e a p p l i c a t i o n .

    A f t e r i n s t a l l i n g E c l i p s e , t h e A D T p l u g i n , a n d t h e S D K , w e s h o u l d t e s t o u r i n s t a l l a t i o n b y w r i t i n g a s h o r t a p p l i c a t i o n a n d r u n n i n g i t . I t ' s t r a d i t i o n a l w h e n l e a r n i n g a n e w l a n g u a g e o r p l a t f o r m A P I t o s t a r t w i t h a H e l l o W o r l d p r o g r a m . I n f a c t , i f w e o m i t t h i s s t e p , w e ' r e l i k e l y t o c a l l d o w n t h e w r a t h o f t h e p r o g r a m m i n g g o d s , s o w e ' d b e t t e r g e t t o w o r k !

    1 . 21 . 21 . 21 . 2 H e l l o , H e l l o ,H e l l o ,H e l l o , W o r l d !W o r l d ! W o r l d ! W o r l d ! C r e a t i n g C r e a t i n g C r e a t i n g C r e a t i n g t h e t h et h et h e F i r s t F i r s tF i r s tF i r s t A p pA p pA p pA p p

    I f E c l i p s e i s s t i l l r u n n i n g , e x i t . W e ' r e g o i n g t o c r e a t e a f o l d e r i n w h i c h t o p u t t h e H e l l o , W o r l d

    p r o g r a m , t h e n r e s t a r t e c l i p s e w i t h t h i s n e w f o l d e r a s t h e W o r k s p a c e . M a k e a n e w f o l d e r f o r t h e e x a m p l e s a n d e x e r c i s e s c a l l e d E B o o k , a n d t h e n c r e a t e a C h a p t e r 1 f o l d e r i n s i d e i t . I n s i d e t h e C h a p t e r 1 f o l d e r , c r e a t e a H e l l o W o r l d f o l d e r . N o w w e ' r e r e a d y t o s t a r t u p E c l i p s e .

    W h e n t h e W o r k s p a c e L a u n c h e r w i n d o w a p p e a r s , u s e t h e B r o w s e . . . b u t t o n t o s e l e c t t h e f o l d e r

  • 8/13/2019 Android Programming Guide for Beginners

    17/308

    1 7

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    y o u j u s t c r e a t e d :

    C l i c k O K , a n d a l l o w E c l i p s e t o l o a d . C l i c k o n t h e G o T o W o r k b e n c h i c o n .

    T h e r e i s c u r r e n t l y n o p r o j e c t i n t h e H e l l o W o r l d f o l d e r , s o w e m u s t c r e a t e o n e . T h e r e a r e t w o w a y s t o c r e a t e a n e w A n d r o i d p r o j e c t : w e c a n c l i c k i n t h e F i l e | N e w . . . m e n u a n d s e l e c t A n d r o i d A p p l i c a t i o n P r o j e c t . . . o r s i m p l y r i g h t - c l i c k o n t h e e m p t y P a c k a g e E x p l o r e r p a n e l , a s s h o w n h e r e :

    E i t h e r w a y , t h e N e w A n d r o i d A p p w i n d o w i s d i s p l a y e d :

  • 8/13/2019 Android Programming Guide for Beginners

    18/308

    1 8

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    F i l l i n t h e t e x t b o x e s a s s h o w n i n t h e i m a g e a b o v e . T h e P a c k a g e N a m e s h o u l d b e s e p a r a t e d b y

    d o t s . F o r t h i s e b o o k , w e ' r e u s i n g c o m . e b o o k . , f o l l o w e d b y t h e n a m e o f t h e a p p l i c a t i o n w e ' r e w o r k i n g o n i n a l l l o w e r c a s e ( h e r e , h e l l o w o r l d ) . S i n c e w e ' v e o n l y i n s t a l l e d o n e S D K p l a t f o r m ( 2 . 3 . 3 ) , w e s h o u l d u s e t h a t f o r b o t h t h e B u i l d S D K a n d t h e M i n i m u m R e q u i r e d S D K . A l s o , t o

  • 8/13/2019 Android Programming Guide for Beginners

    19/308

    1 9

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    k e e p t h i n g s s i m p l e , w e ' v e u n c h e c k e d t h e C r e a t e C u s t o m L a u n c h e r I c o n c h e c k b o x . W h e n y o u a r e f i n i s h e d m a k i n g t h e s e s e l e c t i o n s , c l i c k N e x t > . W e w i l l b e u s i n g t h e h i g h e r v e r s i o n s o f S D K a s w e m o v e t o o t h e r c h a p t e r s l a t e r i n t h e b o o k .

    I n t h e n e x t w i n d o w , y o u w i l l b e p r o m p t e d t o c r e a t e a n A c t i v i t y , a n d a s k e d w h a t k i n d o f a c t i v i t y t o c r e a t e . F o r n o w , m a k e t h e s e l e c t i o n s a s s h o w n :

    C l i c k N e x t > t o c o n t i n u e .

    I n t h e f i n a l w i n d o w , w e ' r e g o i n g t o m a k e a f e w c h a n g e s :

  • 8/13/2019 Android Programming Guide for Beginners

    20/308

    2 0

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    I n t h i s w i n d o w , t h e A c t i v i t y N a m e w i l l b e t h e n a m e o f t h e c l a s s t o c r e a t e , a n d t h e L a y o u t N a m e w i l l b e c o m e t h e n a m e o f t h e . x m l f i l e t h a t d e s c r i b e s t h e v i e w . M a k e t h e c h a n g e s a s s h o w n a b o v e , a n d c l i c k F i n i s h .

    T h e A D T p l u g i n w i l l g e n e r a t e t h e a p p l i c a t i o n f i l e s f o r u s , u s i n g t h e c h o i c e s w e m a d e i n t h e s e t h r e e s c r e e n s t o f i l l o u t a b a s i c a p p l i c a t i o n t e m p l a t e . T h e f i r s t f i l e t h a t i s u s u a l l y d i s p l a y e d i s a g r a p h i c a l r e p r e s e n t a t i o n o f t h e a p p ' s l a y o u t .

    B e f o r e w e c o n t i n u e , l e t ' s t a k e s o m e t i m e t o g o o v e r t h e c o n c e p t o f a n A n d r o i d A c t i v i t y .

  • 8/13/2019 Android Programming Guide for Beginners

    21/308

    2 1

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    C h a p t e r 2 w i l l d i s c u s s a c t i v i t i e s i n s o m e d e t a i l , b u t f o r n o w w e n e e d a b a s i c o v e r v i e w o f t h e c o n c e p t .

    1 . 2 . 1 W h a t ' s a n A c t i v i t y ?

    W e ' r e g o i n g t o s t a r t w i t h a s i m p l e d e f i n i t i o n o f a n a c t i v i t y t h a t w e w i l l b u i l d o n ( a n d m o d i f y ) l a t e r . A n A c t i v i t y i s a s i n g l e u n i t o f u s e r i n t e r a c t i o n i n a n a p p l i c a t i o n . W h e n t h e u s e r v i e w s o r e n t e r s i n f o r m a t i o n i n a n a n d r o i d a p p , t h e y a r e d o i n g s o b y u s i n g a n a c t i v i t y .

    T h e H e l l o A c t i v i t y c l a s s w a s c r e a t e d f o r u s i n t h e s r c ( s o u r c e ) f o l d e r b y t h e A D T . L e t ' s t a k e a l o o k :

    D o u b l e - c l i c k o n t h e H e l l o A c t i v i t y . j a v a f i l e t o o p e n i t i n t h e e d i t o r :

  • 8/13/2019 Android Programming Guide for Beginners

    22/308

  • 8/13/2019 Android Programming Guide for Beginners

    23/308

    2 3

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    c a l l e d . o n C r e a t e d i s p l a y s t h e l a y o u t _ h e l l o l a y o u t , a n d t h e a p p s t a r t s i t s r u n l o o p . S i n c e w e ' v e d e f i n e d n o c o n t r o l s f o r t h e u s e r t o a c t u a l l y i n t e r a c t w i t h , t h e a p p j u s t l o o p s ( i n t h i s c a s e ) u n t i l i t i s c l o s e d .

    B u t i f w e t r y t o r u n t h e a p p l i c a t i o n n o w , w e ' l l f i n d t h a t w e ' r e m i s s i n g s o m e t h i n g . T h e a p p c a n ' t r u n b e c a u s e w e h a v e n ' t d e f i n e d a n A n d r o i d V i r t u a l D e v i c e ( A V D ) f o r i t t o r u n o n i n t h e E m u l a t o r . O o p s ! W e ' d b e t t e r t a k e c a r e o f t h a t . . .

    1 . 2 . 2 C r e a t i n g a n A V D

    A n A V D i s a n e m u l a t o r i m a g e f i l e . E a c h A V D w e c r e a t e s p e c i f i e s s e t t i n g s f o r t h e s p e c i f i c d e v i c e w e w a n t t o e m u l a t e . W h i l e i t w o u l d b e n i c e t o h a v e o n e o f e a c h p h y s i c a l d e v i c e t o t e s t o n , i n s i n g l e - d e v e l o p e r e n v i r o n m e n t s o r i n s m a l l t e a m s t h i s i s r a r e l y w i t h i n t h e b u d g e t .

    T o c r e a t e a n A V D , u s e t h e E c l i p s e m e n u t o n a v i g a t e t o W i n d o w | A V D M a n a g e r :

  • 8/13/2019 Android Programming Guide for Beginners

    24/308

    2 4

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    T h e A n d r o i d V i r t u a l D e v i c e M a n a g e r w i n d o w p o p s u p ( i s n ' t t h e A D T h e l p f u l ? ) :

    O b v i o u s l y , s i n c e y o u p r o b a b l y d o n ' t h a v e a n y A V D ' s c r e a t e d y e t , y o u w o n ' t s e e t h e l i s t s h o w n h e r e . C l i c k o n t h e N e w . . . b u t t o n , a n d c r e a t e a 2 . 3 . 3 A V D w i t h t h e o p t i o n s s h o w n h e r e :

    C l i c k o n C r e a t e A V D t o c o m p l e t e t h e p r o c e s s . T h e A D T i s v e r y s m a r t . . . w h e n w e a t t e m p t t o r u n a n A n d r o i d a p p l i c a t i o n a n d n o a c t u a l d e v i c e c o n f o r m i n g t o t h e t a r g e t A P I l e v e l i s

  • 8/13/2019 Android Programming Guide for Beginners

    25/308

    2 5

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    a t t a c h e d , t h e A D T g o e s t h r o u g h t h e l i s t o f v i r t u a l d e v i c e s a n d a t t e m p t s t o f i n d o n e t h a t w i l l r u n t h e a p p .

    C l o s e t h e A n d r o i d V i r t u a l D e v i c e M a n a g e r w i n d o w . C o n g r a t u l a t i o n s . . . n o w t h a t y o u h a v e a

    l a y o u t , a n a c t i v i t y , a n d a n A V D t o r u n i t i n , t h e a p p s h o u l d r u n i n t h e e m u l a t o r !

    1 . 2 . 4 R u n n i n g H e l l o , W o r l d !

    T h e R u n b u t t o n i s l o c a t e d i n t h e E c l i p s e b u t t o n b a r . W h e n i t i s c l i c k e d o n a n a p p t h a t h a s

    n o t b e e n r u n b e f o r e , i t b r i n g s u p t h e f o l l o w i n g d i a l o g b o x :

    S e l e c t A n d r o i d A p p l i c a t i o n a n d c l i c k O K . I f y o u s e e a C o n s o l e m e s s a g e l i k e t h i s o n e :

    i t i n d i c a t e s t h a t t h e A D T p l u g i n h a s l o s t t h e c o n n e c t i o n t o t h e A n d r o i d D e b u g B r i d g e . D o n ' t w o r r y , i t h a p p e n s . E c l i p s e i s a v e r y n i c e e n v i r o n m e n t t o w o r k i n , b u t i t s o m e t i m e s d i s p l a y s , s h a l l w e s a y , s t r a n g e b e h a v i o r . T h i s p r o b l e m i s a c t u a l l y v e r y o f t e n s o l v e d b y r e s t a r t i n g

    E c l i p s e .

  • 8/13/2019 Android Programming Guide for Beginners

    26/308

    2 6

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    T h i s i s t h e c o n s o l e g i v i n g g o o d m e s s a g e s p r i o r t o t h e a p p l i c a t i o n l a u n c h :

    D o n ' t w o r r y a b o u t t h e m e s s a g e t h a t s t a t e s t h a t N S Q u i c k D r a w V i e w h a s b e e n d e p r e c a t e d , t h i s i s a k n o w n i s s u e : G o o g l e h a s b e e n p r o m i s i n g t o f i x i t f o r y e a r s .

    I f a l l g o e s w e l l , t h e a p p l i c a t i o n w i l l r u n i n t h e e m u l a t o r . Y o u m a y h a v e t o u n l o c k t h e p h o n e b e f o r e y o u s e e t h e a p p :

  • 8/13/2019 Android Programming Guide for Beginners

    27/308

    2 7

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    A l l t h i s w i t h o u t w r i t i n g a s i n g l e l i n e o f c o d e ! Y e s , i t ' s p r e t t y b o r i n g , b u t i f y o u a r e a b l e t o r u n t h e a p p a n d s e e t h e a b o v e r e s u l t i n t h e e m u l a t o r , y o u ' v e a c t u a l l y a c c o m p l i s h e d q u i t e a b i t :

    E c l i p s e i s i n s t a l l e d w i t h t h e A D T p l u g i n , t h e S D K i s i n s t a l l e d a n d u p d a t e d w i t h t h e 2 . 3 . 3

    G i n g e r b r e a d A P I a n d p l a t f o r m t o o l s , y o u h a v e a w o r k i n g A V D , a n d e v e r y t h i n g i s r i g h t w i t h t h e w o r l d . N o w w e c a n b e g i n l e a r n i n g h o w t o w r i t e a p p s !

    1 . 31 . 31 . 31 . 3 A AAA P r e v i e w :P r e v i e w :P r e v i e w :P r e v i e w : H a n d l i n gH a n d l i n gH a n d l i n gH a n d l i n g E v e n t sE v e n t sE v e n t sE v e n t s

    H e l l o W o r l d i s a g r e a t l i t t l e a p p , b u t i t d o e s n ' t d o m u c h . L e t ' s a d d a b u t t o n t o t h e i n t e r f a c e t o

    a l l o w t h e u s e r t o c o n t r o l w h e n t h e m e s s a g e i s d i s p l a y e d . A s y o u ' l l s e e , t h i s d o e s n ' t r e q u i r e m u c h a d d i t i o n a l c o d e a t a l l .

    O p e n t h e l a y o u t _ h e l l o . x m l f i l e . I f t h e f i l e i s i n x m l v i e w , s w i t c h t o g r a p h i c a l v i e w u s i n g t h e t a b a t t h e b o t t o m o f t h e w i n d o w . D r a g a b u t t o n c o n t r o l f r o m t h e F o r m W i d g e t s p a n e l t o t h e

    i n t e r f a c e , t h e n m o v e t h e t e x t v i e w c o n t r o l ( t h a t c u r r e n t l y s a y s H e l l o W o r l d ) t o a p o s i t i o n a b o v e t h e b u t t o n . T h e i n t e r f a c e s h o u l d l o o k s o m e t h i n g l i k e t h i s :

    O p e n t h e x m l v i e w o f t h e f i l e b y c l i c k i n g t h e l a y o u t _ h e l l o . x m l t a b a t t h e b o t t o m o f t h e v i e w .

  • 8/13/2019 Android Programming Guide for Beginners

    28/308

    2 8

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    M a k t h e h i g h l i g h t e d c h a n g e s t o t h e f i l e :

    < / R e l a t i v e L a y o u t >

    W e s e t t h e v i s i b i l i t y a t t r i b u t e o f t h e T e x t V i e w c o n t r o l t o i n v i s i b l e b e c a u s e w e ' r e g o i n g t o c o n t r o l i t s v i s i b i l i t y t h r o u g h t h e b u t t o n . W e ' v e a l s o a d d e d a n a t t r i b u t e t o t h e B u t t o n a n d r o i d : o n C l i c k . T h i s a t t r i b u t e t e l l s t h e b u t t o n t o c a l l t h e s h o w T e x t m e t h o d ( t h a t w e m u s t d e f i n e i n t h e a c t i v i t y ) w h e n i t i s c l i c k e d . W e ' v e a l s o a l t e r e d t h e a n d r o i d : t e x t a t t r i b u t e t o p o i n t t o a s t r i n g r e s o u r c e n a m e d b u t t o n _ t e x t .

    W h i l e w e c o u l d h a v e e n t e r e d a h a r d c o d e d s t r i n g i n t h e a n d r o i d : t e x t a t t r i b u t e , i t i s b e s t t o g e t i n t o t h e h a b i t e a r l y t o u s e s t r i n g r e s o u r c e s i n s t e a d o f h a r d c o d e d s t r i n g s . O p e n u p t h e r e s / v a l u e s / s t r i n g s . x m l f i l e a n d a d d t h e h i g h l i g h t e d s t r i n g r e s o u r c e i n t h e x m l v i e w :

    H e l l o W o r l d < / s t r i n g >

  • 8/13/2019 Android Programming Guide for Beginners

    29/308

    2 9

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    H e l l o w o r l d ! < / s t r i n g >

    S e t t i n g s < / s t r i n g > H e l l o < / s t r i n g > S a y H e l l o < / s t r i n g >

    < / r e s o u r c e s >

    W h e n t h e l a y o u t s e e s @ S t r i n g / b u t t o n _ t e x t i n t h e t e x t a t t r i b u t e o f t h e b u t t o n , i t l o o k s i n t h e s t r i n g s . x m l f i l e a n d r e p l a c e s t h e t e x t o n t h e b u t t o n w i t h S a y H e l l o .

    S a v e y o u r w o r k , t h e n o p e n u p t h e H e l l o A c t i v i t y . j a v a f i l e . A d d t h e s h o w T e x t m e t h o d t o t h e H e l l o A c t i v i t y c l a s s , r i g h t b e l o w t h e o n C r e a t e ( ) m e t h o d :

    p a c k a g e c o m . e b o o k . h e l l o w o r l d ;

    i m p o r t a n d r o i d . o s . B u n d l e ;

    i m p o r t a n d r o i d . a p p . A c t i v i t y ;

    i m p o r t a n d r o i d . v i e w . M e n u ;

    i m p o r t a n d r o i d . v i e w . V i e w ;

    i m p o r t a n d r o i d . w i d g e t . T e x t V i e w ;

    p u b l i c c l a s s H e l l o A c t i v i t y e x t e n d s A c t i v i t y {

    @ O v e r r i d e

    p u b l i c v o i d o n C r e a t e ( B u n d l e s a v e d I n s t a n c e S t a t e ) {

    s u p e r . o n C r e a t e ( s a v e d I n s t a n c e S t a t e ) ;

    s e t C o n t e n t V i e w ( R . l a y o u t . l a y o u t _ h e l l o ) ;

    }

    p u b l i c v o i d s h o w T e x t ( V i e w v i e w ) {

    T e x t V i e w t v = ( T e x t V i e w ) f i n d V i e w B y I d ( R . i d . t e x t V i e w 1 ) ;

    t v . s e t V i s i b i l i t y ( V i e w . V I S I B L E ) ;

  • 8/13/2019 Android Programming Guide for Beginners

    30/308

    3 0

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    }

    @ O v e r r i d e

    p u b l i c b o o l e a n o n C r e a t e O p t i o n s M e n u ( M e n u m e n u ) { g e t M e n u I n f l a t e r ( ) . i n f l a t e ( R . m e n u . l a y o u t _ h e l l o , m e n u ) ;

    r e t u r n t r u e ;

    }

    }

    T h e r e a r e t w o w a y s t o h a n d l e b u t t o n c l i c k s : w e c a n u s e m e t h o d s o f t h e a c t i v i t y c o u p l e d w i t h t h e o n C l i c k a t t r i b u t e ( a s s h o w n h e r e ) , o r w e c a n a d d a b u t t o n h a n d l e r t o t h e a c t i v i t y d i r e c t l y i n t h e o n C r e a t e ( ) m e t h o d . T h i s m e t h o d i s s i m p l e r , b u t b o t h h a v e a d v a n t a g e s , a s w e s h a l l s e e l a t e r .

    W e ' v e c r e a t e d t h e s h o w T e x t m e t h o d . N o t i c e t h a t i t t a k e s o n e p a r a m e t e r : a V i e w o b j e c t . V i e w i s t h e r o o t c l a s s o f a l l l a y o u t s a n d c o n t r o l s i n a n A n d r o i d a p p . T h e V i e w t h a t g e t s p a s s e d t o t h i s m e t h o d i s t h e c o n t r o l t h a t w a s i n t e r a c t e d w i t h , i n t h i s c a s e , t h e b u t t o n . W e ' v e n a m e d t h i s V i e w v i e w .

    I n t h e m e t h o d , w e o b t a i n a T e x t V i e w o b j e c t ( n a m e d t v ) b y u s i n g f i n d V i e w B y I d . T h i s m e t h o d t a k e s a n i n t p a r a m e t e r , w h i c h i s t h e i d o f t h e T e x t V i e w i n t h e l a y o u t . W e g e t t h i s b y l o o k i n g a t t h e t e x t V i e w 1 p r o p e r t y o f t h e i d c l a s s w i t h i n t h e R c l a s s . R e c a l l t h a t w e n e v e r u s e t h e s e i n t i d s d i r e c t l y a s i n t s . H e r e ' s h o w w e c a n r e f e r e n c e t h e m u s i n g t h e v a l u e s d e c l a r e d i n R . j a v a .

    F i n a l l y , w e s e t t h e V i s i b i l i t y a t t r i b u t e o f t h e T e x t V i e w t o V i e w . V I S I B L E . T h e r e a r e t h r e e p o s s i b l e v i s i b i l i t y s t a t e s : V I S I B L E , I N V I S I B L E , a n d G O N E . T h e s e c o r r e s p o n d t o t h e a t t r i b u t e v a l u e s v i s i b l e , i n v i s i b l e , a n d g o n e i n t h e l a y o u t x m l . T h e y a r e d e f i n e d a s a n e n u m i n t h e V i e w c l a s s . V i s i b l e a n d I n v i s i b l e a r e e a s y t o u n d e r s t a n d , b u t G o n e m i g h t r e q u i r e s o m e e x p l a n a t i o n . W h e n a c o n t r o l i s s e t t o h a v e G o n e v i s i b i l i t y , n o t o n l y i s i t i n v i s i b l e , b u t t h e s p a c e i t o c c u p i e s i n t h e l a y o u t i s p o t e n t i a l l y r e c l a i m e d b y o t h e r c o n t r o l s i n t h e l a y o u t . T h i s r e c l a m a t i o n b e h a v i o r i s e s p e c i a l l y u s e f u l w h e n h i d i n g c o n t r o l s i n a l i s t v i e w .

    R u n t h e a p p l i c a t i o n a n d n o t i c e t h a t t h e H e l l o w o r l d ! T e x t n o w o n l y a p p e a r s a f t e r t h e b u t t o n i s c l i c k e d :

  • 8/13/2019 Android Programming Guide for Beginners

    31/308

    3 1

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    1 . 41 . 41 . 41 . 4 H e l l o , H e l l o ,H e l l o ,H e l l o , L i f e c y c l e !L i f e c y c l e !L i f e c y c l e !L i f e c y c l e !

    I n t h i s s e c t i o n , w e ' r e g o i n g t o e x p l o r e t h e a c t i v i t y l i f e c y c l e b y w r i t i n g a s m a l l a p p l i c a t i o n t h a t

  • 8/13/2019 Android Programming Guide for Beginners

    32/308

    3 2

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    ( c )( c )( c )( c ) E d u o n i x E d u o n i x E d u o n i x E d u o n i x T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s T e c h n o l o g i e s P v t P v tP v tP v t L t d . L t d . L t d . L t d . A l l A l lA l lA l l r i g h t s r i g h t s r i g h t s r i g h t s r e s e r v e d r e s e r v e d r e s e r v e d r e s e r v e d

    i n f o r m s u s a s t h e a c t i v i t y ' s s t a t e c h a n g e s . W e ' l l b e b u i l d i n g o n t h e H e l l o A c t i v i t y a p p l i c a t i o n ( w h i c h i s j u s t r e a l l y t h e t e m p l a t e a p p p r o v i d e d b y t h e A D T w h e n i t c r e a t e s a n e