Académique Documents
Professionnel Documents
Culture Documents
< CS193A Android Programming Today: anonymous inner classes, starting intents, animation, toast notifications. Today's example code is all in an activity called Example1Activity -- the key bits of code are pasted in below.
Alternate Resources
The "drawable-hdpi" etc. folders provide alternate resources depending on runtime details -- screen size, locale, etc. For example, the selector "-hdpi" is for high pixel devices, "-es" for Spanish. Plain "drawable" folder is the base default. Your code can just refer to R.drawable.abbysmile or whatever, and the system does the logic to select the right one automatically. More on this later.
p r i v a t ei n tm B a l a n c e ; / /1 .C l a s s i cm e m b e rv a r i a b l e( a l s ok n o w na n" i n s t a n c ev a r i a b l e " ) / /C a nu s e" m "a tn a m es t a r t / /E x i s t si nt h eA c c o u n to b j e c t-o u t l i v e sa n yo n em e t h o dc a l l . p u b l i cA c c o u n t ( ){ m B a l a n c e=0 ; } p u b l i cv o i dd e p o s i t ( i n ta m t ){ S t r i n gl o g M s g=" d e p o s i t : "+a m t ; / /2 .l o g M s gi sal o c a l( s t a c k )v a r i a b l e/ /i te x i s t sa st e m p o r a r ys t o r a g ej u s tw h i l ed e p o s i t ( )i sr u n n i n g . / /C o n t r a s tt ot h em B a l a n c ev a r i a b l e . m B a l a n c e=m B a l a n c e+a m t ; } / * V a r i a b l er u l e s : U s eam e m b e rv a r i a b l ei fy o uw a n ts t o r a g ei nt h eo b j e c tt h a to u t l i v e s a n yo n em e t h o d .O f t e ns e tu pi no n C r e a t e ( ) ,m o d i f i e db ym e t h o d so v e rt i m e ( m B a l a n c ei sag o o de x a m p l eo ft h i sp a t t e r n ) . U s eal o c a lv a r i a b l et oh o l da n yt e m p o r a r yr e s u l to rp o i n t e rn e e d e d* d u r i n g * am e t h o dr u n ,b u tn o tn e e d e da f t e ri te x i t s .L o c a lv a r i a b l e sa r es i m p l e ra n d h a v eb e t t e rp e r f o r m a n c e ,s ot h e ya r eag o o dd e f a u l ts t r a t e g y . * /
d e p o s i t ( t e m p ) ; / /C a nc a l lm e t h o d so nt h eo u t e ro b j e c t } } ; / * C o d ei n s i d ea nA I Cm e t h o d : 1 .T h eA I Cc r e a t e sas e p a r a t eo b j e c tw i t hi t so w nm e t h o d s . H o w e v e r ,t h eA I Ck e e p sap o i n t e rt oi t so w n i n g ,o u t e ro b j e c tt or e f e r t oi t sv a r i a b l e sa n dc a l li t sm e t h o d st o o( e . g .d e p o s i t ( ) ) . 2 ." t h i s "h e r er e f e r st ot h eA I Ci t s e l f .U s et h es y n t a x" O u t e r . t h i s "t og e t ap o i n t e rt ot h eo u t e ro b j e c t . 3 .C a n n o tr e f e rt ol o c a lv a r i a b l e sw h e r et h eA I Cw a sc r e a t e d( e . g .t e m p ) u n l e s st h e ya r ed e c l a r e d" f i n a l " . F u nF a c t :i nJ a v a8 ,A I C ' sa r eb e i n gr e p l a c e dw i t ham u c hs i m p l e r" l a m b d a " s y n t a x-y a y ! * / } }
Button OnClickListener
Last bit from previous lecture -- OnClickListener is an AIC.
p u b l i cv o i do n C r e a t e ( B u n d l es 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 . m a i n ) ; / /S t o r eap o i n t e rt oe d i t T e x t 1 .S e tu pb u t t o n 1t oa d da! . e d i t T e x t 1=( E d i t T e x t )f i n d V i e w B y I d ( R . i d . e d i t T e x t 1 ) ; B u t t o nb u t t o n=( B u t t o n )f i n d V i e w B y I d ( R . i d . b u t t o n 1 ) ; b u t t o n . s e t O n C l i c k L i s t e n e r ( / /C r e a t eaO n C l i c k L i s t e n e rA I C n e wO n C l i c k L i s t e n e r ( ){ p u b l i cv o i do n C l i c k ( V i e wv ){ e d i t T e x t 1 . s e t T e x t ( e d i t T e x t 1 . g e t T e x t ( )+" ! " ) ; } } ) ; . . .
Intents
Intents -- the mechanism by which various activities/components invoke each other. This is just a first, simple example. Intent has an action and data. Here we specify the "open url" intent .. which will launch the appropriate new activity for that. The "back button" from there comes back to us.
Complaint Button
Write as a separate doComplain() method, call it from a one line AIC. Write the animation in a separate res/anim/swirl.xml file
p u b l i cv o i dd o C o m p l a i n ( ){ A n i m a t i o na n i m a t i o n=A n i m a t i o n U t i l s . l o a d A n i m a t i o n ( t h i s ,R . a n i m . s w i r l ) ; / /T h i sw h o l em i d d l ep i e c ei so p t i o n a l
e d i t T e x t 3 . s t a r t A n i m a t i o n ( a n i m a t i o n ) ; }
Animation XML
< < ? x m lv e r s i o n = " 1 . 0 "e n c o d i n g = " u t f 8 " ? > < s e tx m l n s : a n d r o i d = " h t t p : / / s c h e m a s . a n d r o i d . c o m / a p k / r e s / a n d r o i d " a n d r o i d : s h a r e I n t e r p o l a t o r = " f a l s e " > < ! - t h i si si nr e s / a n i m / s w i r l . x m l> < ! - m a k ei ts m a l l e rf o r8 0 0 m s> < s c a l e a n d r o i d : i n t e r p o l a t o r = " @ a n d r o i d : a n i m / a c c e l e r a t e _ d e c e l e r a t e _ i n t e r p o l a t o r " a n d r o i d : f r o m X S c a l e = " 1 . 0 " a n d r o i d : t o X S c a l e = " 0 . 5 " a n d r o i d : f r o m Y S c a l e = " 1 . 0 " a n d r o i d : t o Y S c a l e = " 0 . 1 " a n d r o i d : p i v o t X = " 5 0 % "
a n d r o i d : p i v o t Y = " 5 0 % " a n d r o i d : f i l l A f t e r = " f a l s e " a n d r o i d : d u r a t i o n = " 8 0 0 "/ > < ! - T h e nr o t a t ea n df a d eo u t( a l p h a=0 )f o r8 0 0m s> < r o t a t e a n d r o i d : f r o m D e g r e e s = " 0 " a n d r o i d : t o D e g r e e s = " 7 2 0 " a n d r o i d : p i v o t X = " 5 0 % " a n d r o i d : p i v o t Y = " 5 0 % " a n d r o i d : s t a r t O f f s e t = " 8 0 0 " a n d r o i d : d u r a t i o n = " 8 0 0 "/ > < a l p h a a n d r o i d : f r o m A l p h a = " 1 . 0 " a n d r o i d : t o A l p h a = " 0 . 0 " a n d r o i d : s t a r t O f f s e t = " 8 0 0 " a n d r o i d : d u r a t i o n = " 8 0 0 "/ > < / s e t >
Homework 2
Add to your homework1 app -- you don't need to turn this in, it's just for practice. Start at least one intent based on a user action (web or otherwise) Add an animation to some widget on screen Use AnimationListener to do something on the start or end of the animation