This document provides instructions for building a simple temperature conversion application for Android. It guides the user to set up their development environment, create an Android project in Eclipse, and build a basic user interface with text fields, buttons, and labels to allow temperature conversion between Celsius, Fahrenheit and Kelvin scales. The instructions are step-by-step and include screenshots to illustrate each stage of creating the app layout and linking interface elements to code.
This document provides instructions for building a simple temperature conversion application for Android. It guides the user to set up their development environment, create an Android project in Eclipse, and build a basic user interface with text fields, buttons, and labels to allow temperature conversion between Celsius, Fahrenheit and Kelvin scales. The instructions are step-by-step and include screenshots to illustrate each stage of creating the app layout and linking interface elements to code.
This document provides instructions for building a simple temperature conversion application for Android. It guides the user to set up their development environment, create an Android project in Eclipse, and build a basic user interface with text fields, buttons, and labels to allow temperature conversion between Celsius, Fahrenheit and Kelvin scales. The instructions are step-by-step and include screenshots to illustrate each stage of creating the app layout and linking interface elements to code.
This document provides instructions for building a simple temperature conversion application for Android. It guides the user to set up their development environment, create an Android project in Eclipse, and build a basic user interface with text fields, buttons, and labels to allow temperature conversion between Celsius, Fahrenheit and Kelvin scales. The instructions are step-by-step and include screenshots to illustrate each stage of creating the app layout and linking interface elements to code.
!"#$% '()*+% ,-. /-.0 1*,## CcLober 2011 ln Lhls worksheeL you wlll develop a slmple LemperaLure converslon appllcaLlon LhaL you wlll run on Lhe Androld emulaLor. 1he appllcaLlon was demonsLraLed llve before you sLarLed Lhls worksheeL, and Lhls ls a screen-shoL of Lhe klnd of appllcaLlon you should see afLer bulldlng and deploylng Lhe appllcaLlon Lo Lhe emulaLor.
lf you are followlng Lhls worksheeL uslng your own compuLer raLher Lhan one of ours, Lhen you may need Lo lnsLall Lhe Lcllpse lnLegraLed developmenL envlronmenL, Lhe Androld Suk (SofLware uevelopmenL klL) eLc. lf Lhls ls Lhe case, Lhen sLarL wlLh Lhe secLlon: 5ettloq op tbe eovltoomeot. 1hls worksheeL assumes very llLLle programmlng knowledge (Lhe worksheeL was creaLed for a wlder audlence Lhan CS sLudenLs). We wlll gulde you sLep-by-sLep and provlde you wlLh screen-shoLs all Lhe way. lf you do geL sLuck Lhen please call one of us over. A good resource Lo bookmark ls: hLLp://developer.androld.com/lndex.hLml. ?ou wlll need Lo sLarL up Lcllpse. lf uslng lnformaLlon Servlces Wlndows Cs you can access Lcllpse Lhrough Lhe CompuLer Sclence courseware menu. 50"& 67 8-"30'() 3( 1(2-%'2 9'-0,3$ !"#'4" unforLunaLely, our lnsLallaLlon on lS Cs does noL provlde some defaulL Androld vlrLual uevlces (Lhe devlce LhaL ls run on Lhe emulaLor). ?ou wlll Lherefore have Lo creaLe a slngle vlrLual devlce every Llme you run Lcllpse on lS Cs. 1hls ls palnful, buL doesn'L Lake Loo long. See Lhe secLlon 8ulldlng Androld vlrLual devlces aL Lhe end of Lhls worksheeL. ?ou only need Lo creaLe one vlrLual devlce. 1here ls anoLher problem wlLh some lS Cs. Check Lhe Androld preferences (Wlndow->references- >Androld). Make sure Lhe Suk LocaLlon ls seL Lo c.\ltoqto1\AoJtolJ-sJk and noL c.\ltoqtom llles\ooJtolJ-sJk. 50"& :7 8-"30'() 3( ;4$'&/" 1(2-%'2 &-%<"40 1o make Lcllpse presenL some useful Androld-speclflc vlews selecL wloJow->5bow vlew->Otbet- >AoJtolJ and selecL loqcot and uevlces. loqcot ls useful durlng debugglng when you wanL Lo see Lhe evenLs belng generaLed by your appllcaLlon. uevlces ls useful when you wlsh Lo selecL beLween a connecLed Androld phone or Lhe emulaLor. Also selecL Lhe vlews Ceoetol->Ootlloe and Ceoetol- >ltopettles. 1hese are useful when dolng drag-and-drop sLyle Cul bulldlng. llnally, make sure Lhe ouLpuL console vlew ls vlslble by Ceoetol->coosole. ln Lcllpse selecL llle->new->CLher and Lhen selecL Androld->Androld ro[ecL:
SelecL nexL. Pere's Lhe Lop parL of Lhe dlalog screen afLer l have enLered a name and selecLed an approprlaLe bulld LargeL. ln Lhls case l have selecLed Androld 2.2
?our defaulL locaLlon wlll be dlfferenL. 1hls ls where Lcllpse wlll, by defaulL, sLore your pro[ecL flles. 1o remove Lhe lockoqe oome error, make changes as shown ln Lhe lower half of Lhe dlalog screen (change cwl Lo your user ldenLlfler):
!ava packages are used ln Lhe same way Lhe folders are used on Lhe flle sysLem. lL ls good pracLlce ln !ava Lo use your slLe domaln name Lo help name Lhe package paLh. 1 noLe LhaL tempetototecoovettet package ls lnslde Lhe cwl package whlch ls lnslde Lhe obet package... We wlll add our code under Lhe tempetototecoovettet package. Change from cwl Lo your uld. Cllck llnlsh.
1 1hls avolds !ava name clashes when reuslng somebody else's !ava code. Lcllpse should have creaLed an Androld pro[ecL LhaL has Lhe followlng sLrucLure:
noLlce LhaL a LemplaLe 1empConverLerAcLlvlLy.[ava flle has been creaLed for us. We wlll add our code Lo LhaL laLer. noLlce how Lhe package sLrucLure ls creaLed ([usL llke folders, and ln facL Lhese are represenLed by folders of Lhe same names on Lhe flle sysLem). 1here ls a tes secLlon LhaL conLalns all Lhe resources requlred by our appllcaLlon: such as Cul screen deslgns, and daLa needed by our appllcaLlon. 1he secLlons Jtowoble-bJpl eLc allow us Lo cusLomlse Culs for devlces wlLh hlgh-, low- and medlum-denslLy screens respecLlvely. 2 1he AndroldManlfesL.xml flle ls a conflguraLlon flle LhaL conLalns lnformaLlon such as Lhe name of Lhe appllcaLlon, Lhe mlnlmum LargeL Suk LhaL Lhe appllcaLlon can be deployed Lo eLc. 50"& =7 >,(('() 3/ 3 ?@"$$% A%-$2B 3&&$'430'%( nearly every programmer's flrsL program ln a language wlll dlsplay Lhe bello wotlJ message. usually, you have Lo wrlLe some code Lo do Lhls. noL so wlLh an Lcllpse generaLed Androld pro[ecL. We can [usL run lL. 1o run 1empConverLer on Lhe emulaLor rlghL cllck on Lhe 1empConverLer pro[ecL name and Lhen selecL koo os->AoJtolJ Appllcotloo. 1he emulaLor wlll be sLarLed and afLer some mlnuLes you should see Lhe followlng emulaLor screen. ln some cases Lhls does noL appear and Lhe 1empConverLer resulL
2 1he Androld devlce wlll use Lhe mosL approprlaLe resource dependlng on lLs screen resoluLlon. Many more posslblllLles exlsL. ls dlsplayed dlrecLly.
urag Lhe lock across Lo Lhe speaker Lo unlock Lhe emulaLor. Popefully, you wlll Lhen see:
lf you double cllck on Lhe Lcllpse Console Lab you should see Lhe followlng log ouLpuL:
uouble cllck Lhe Lab agaln Lo reduce lLs slze. lor Lhose wlLh some programmlng knowledge you can see where Lhe hello world sLrlng comes from. llrsL of all Lake a look aL 1empConverLerAcLlvlLy.[ava. 1hls calls setContentView(R.layout.main); 1hls ls Lelllng Lhe Androld supporL code Lo look ln Lhe molo.xml flle ln Lhe res/loyoot secLlon of Lhe pro[ecL and Lo follow Lhe lnsLrucLlons ln LhaL flle when creaLlng Lhe Cul. lf you double cllck on molo.xml lL wlll evenLually dlsplay Lhe followlng:
1hls ls where we can deslgn our user lnLerface. 1hls one ls preLLy slmple! lf you cllck on Lhe molo.xml Lab you wlll see Lhe llne: android:text="@string/hello" 1hls ls an lnsLrucLlon Lo Androld Lo look ln Lhe flle sLrlngs.xml ln Lhe tes/voloes secLlon of Lhe Lcllpse pro[ecL. 1hls ls where we deflne sLaLlc resources, such as messages Lo dlsplay. Slnce lL Lakes such a long Llme Lo load make sure you don'L deleLe Lhe emulaLor wlndow 50"& C7 !"/')('() 0D" EFG .%- 0D" 0"H&"-30,-" 4%(#"-0"- We wlll now creaLe Lhe Cul so LhaL we end up wlLh a deslgn slmllar Lo LhaL shown below. ?ou can be more creaLlve!
1hls ls dlsplaylng molo.xml uslng Lhe Ctopblcol loyoot tob. l have hlghllghLed Lhe box under Lhe coovett to kelvlo buLLon Lo show Lhe locaLlon of a 1extvlew componenL LhaL wlll hold Lhe LemperaLure afLer a converslon has occurred. 1. Cpen up Lhe tes/loyoot/molo.xml for your 1empcoovettet pro[ecL. 2. ueleLe Lhe exlsLlng Pello world LexL fleld by rlghL-cllcklng on Lhe LexL fleld and selecLlng Jelete from Lhe pop-up menu. 3. We need Lo creaLe a layouL conLalner. LayouL conLalners allow us Lo speclfy how wldgeLs are placed relaLlve Lo each oLher, for example, Lo make sure LhaL each new wldgeL ls placed Lo Lhe rlghL (where screen space allows) of Lhe lasL wldgeL, or lf Lhere ls lnsufflclenL space, Lo wrap Lo Lhe far lefL of Lhe nexL llne. We wanL Lo use Lhls laLLer klnd of layouL conLalner. ln Lhe LayouLs enLry ln Lhe aleLLe drag and drop Lhe Llnear LayouL (PorlzonLal) layouL on Lo Lhe canvas area. 4. CreaLe Lhe larenhelL LexL fleld by cllcklng on Lhe 1exL llelds enLry ln Lhe aleLLe and selecLlng Lhe number LexLdeclmal fleld. LefL-cllck hold and drag lL on Lo Lhe canvas, dropplng lL onLo Lhe layouL area. 8lghL cllck on Lhe LexL fleld and selecL Jlt lu. LnLer eJlt1extloteobelt1emp. We wlll refer Lo Lhls laLer ln our code when we need Lo geL hold of Lhe value Lyped by Lhe user.
3. We now need Lo add a 1exLvlew for Lhe !! label. Cpen lotm wlJqets and drag and drop a 1extvlew wldgeL on Lo Lhe end secLlon of Lhe exlsLlng LexL fleld. Change Lhe ld of Lhe 1extvlew wlLh rlghL-cllck Jlt lu and change Lo textvlewloteobeltlobel. 8lghL cllck on 1extvlew and selecL ltopettles->Ctovlty->ceotet. 8eslze Lhe 1extvlew so LhaL you end up wlLh Lhe followlng:
6. We now need Lo change Lhe 1extvlew LexL. 8lghL cllck and selecL LdlL 1exL. Cllck on Lhe new SLrlng buLLon and flll ln Lhe flelds as follows:
1he degree symbol on Wlndows 7 can be obLalned from coottol looel->Appeotooce...- >loots->lloJ o cbotoctet. 1hls ls noL avallable on lS Cs, so you can copy !! elecLronlcally from Lhls documenL. Cn Macs Lhe degree symbol can be obLalned Lhrough Lhe Jlt->5peclol cbotoctets menu lLem (e.g. from Safarl). 1he ldenLlfler ls textvlewloteobeltlobel and Lhls and lLs assoclaLed sLrlng wlll be placed ln Lhe sLrlngs.xml flle. Cllck Ck and Lhen Ck agaln. Cllck on Lhe xml Lab and change Lhe 1extvlew llne so LhaL ls conLalns Lhe textAppeotooce aLLrlbuLe: <TextView android:textAppearance="?android:attr/textAppearanceLarge" lf you go back Lo Lhe graphlcal vlew you should now see:
7. LeL's add Lhe converL Lo Celslus and converL Lo kelvln buLLons. lrom lorm WldgeLs drag and drop a 8uLLon under Lhe layouL conLalner. We don'L wanL Lo lnclude lL ln our conLalner (lloeotloyoot1), buL raLher Lo Lhe Lop-level defaulL conLalner (lloeotloyoot). 1o make lL cenLre, rlghL cllck on Lhe lloeotloyoot conLalner ln Lhe Ootlloe vlew and from ltopettles menu lLem seL lLs Ctovlty Lo ceotet notlzootol. Change Lhe LexL of Lhe buLLon by selecLlng Jlt 1ext and creaLlng a new resource sLrlng called 8ottoocoovett1ocelslos wlLh Lhe LexL coovett 1o celslos. llnally, change Lhe buLLon's ldenLlfler Lo be bottoocoovett1ocelslos!1he Cul should now look llke:
8. uo Lhe same for Lhe ConverL Lo kelvln buLLon, buL Lhls Llme wlLh a resource sLrlng called 8ottoocoovett1okelvlo and an ld of bottoocoovett1okelvlo.
9. llnally, for Lhe Cul deslgn leL's creaLe Lhe 1extvlew LhaL wlll dlsplay our converslon resulL. urag a 1exvlew and place under Lhe kelvlo buLLon. Change lLs ld Lo textvlewcoovetteJ1emp. 8emove lLs defaulL LexL (easlesL by edlLlng Lhe 1ext properLy ln Lhe ltopettles vlew as shown below). Make lL blgger by changlng lLs textAppeotooce properLy Lo "?ooJtolJ.ottt/textAppeotoocelotqe". ?ou should now see:
50"& I7 >,(('() 0D" 0"H&"-30,-" 3&&$'430'%( .%- 0D" .'-/0 0'H" 8un Lhe Androld appllcaLlon ln Lhe emulaLor (see earller SLep 2). ?ou should see:
Cf course when you cllck on Lhe buLLons noLhlng happens. ln Lhe nexL sLep we wlll wlre-up Lhe buLLons Lo some code LhaL does Lhe approprlaLe converslon and dlsplays Lhe resulL ln our textvlewcoovetteJ1emp LexL vlew. 8y Lhe way, lf you wanL Lo slmulaLe Lurnlng Lhe emulaLor on lLs slde (landscape) Lhen Lype CLrl-l11 (on Wlndows) or CLrl-fn-l11 (on Lhe Mac). 50"& J7 8-"30'() 0D" 4%2" 0D30 D3(2$"/ 0D" 8"$/',/ K,00%( 4$'4L ln Lcllpse, open up Lhe flle 1empConverLerAcLlvlLy.[ava. AL Lhe momenL all Lhls ls dolng ls geLLlng hold of our maln.xml flle and dlsplaylng Lhe Cul deflned ln LhaL flle. We need Lo add some meLhods Lo handle Lhe buLLon presses. 1. LeL's deal wlLh Lhe coovett 1o celslos buLLon. Copy Lhe followlng code lnLo Lhe flle: /** * The user has clicked the button to convert from * Farenheit to Celsius */ public void convertToCelsiusClicked(View v) { double farenheitValue = getEnteredFarenheitTemp(); double celsiusValue = convertFToC(farenheitValue);
// create the string or the Celsius temperature, // retrieving the 'units' from the string resources String celsiusSymbol = getString(R.string.celsiusLabel); String convertedText = String.format("%.1f%s", celsiusValue, celsiusSymbol);
// and write the output to the screen output.setText(convertedText);
} lace Lhe code afLer Lhe oocteote meLhod. ?ou can clean up Lhe formaLLlng by rlghL-cllcklng Lhe edlLor wlndow and selecLlng Source->lormaL (or cLrl SPll1-l). l won'L explaln Lhe !ava code here. lf you know !ava or programmlng Lhen you should be able Lo undersLand Lhe code. 1he red under-scorlng means we have some compllaLlon errors. LeL's deal wlLh each ln Lurn.
2. 8efore we creaLe Lhe mlsslng meLhods, leL's wlre up Lhls new meLhod wlLh Lhe coovett 1o celslos buLLon. Co back Lo Lhe Craphlcal ulsplay selecL Lhe buLLon and seL lLs oocllck properLy Lo Lhe name of our meLhod: coovett1ocelsloscllckeJ. 3. Co back Lo Lhe code edlLor and add Lhe qetoteteJloteobelt1emp meLhod: /** * Retrieve the value from the farenheit box as a double. * If no value is present, or there is an error, * this defaults to zero */ double getEnteredFarenheitTemp() { double farenheitValue = 0.0; try{ EditText et = (EditText)findViewById(R.id.editTextFarenheitTemp); farenheitValue = Double.valueOf(et.getText().toString());
}catch (Exception e) { // failed to get the farenheit value Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT); }
return farenheitValue; } lormaL Lhe code wlLh rlghL-cllck->source->formaL. 1he code Jlt1ext and 1oost are underllned ln red. We need Lo lmporL Lhese classes lnLo our code. 1he easlesL way ls Lo move Lhe cursor over Lhe offendlng lLem. ?ou should see a pop-up llsL wlLh Lhlngs you could Lry Lo solve Lhe problem. SelecL Lhe lmporL opLlon. lf you look aL Lhe Lop of Lhe flle you wlll see LhaL Lhe relevanL lmporL sLaLemenL has been lncluded. 4. LeL's add Lhe coovettl1oc meLhod. /** * Convert from Farenheit to Celsius */ static double convertFToC(double farenheitTemp) { return (farenheitTemp -32.0)*(5.0/9.0); } 3. We sLlll have Lwo errors ln our coovett1ocelsloscllckeJ "#$%&'( )%# k.sttloq.celsloslobel needs deflnlng ln our voloes/sttloqs.xml flle. uouble cllck on Lhls flle and add Lhe LexL: <string name="celsiusLabel">C</string> <string name="kelvinLabel">K</string> 6. 1o flnal error ls LhaL Lhe varlable ootpot ls noL deflned. llrsL add Lhe followlng code Lo Lhe oocteote meLhod ([usL afLer Lhe setcooteotvlew call): // get the object to which we will write the converted value output = (TextView)findViewById(R.id.textViewConvertedTemp); 7. llx Lhe 1extvlew lmporL problem. 8. llx Lhe ootpot error by placlng Lhe cursor over Lhe varlable and selecLlng cteote llelJ ootpot. All Lhe compllaLlon errors should now be gone. 50"& M7 >,( 3)3'( K,0 0D'/ 0'H" N'0D 3 4$'4L3K$" 8"$/',/ K,00%( 8un Lhe appllcaLlon agaln. 1hls Llme declmal values Lyped lnLo Lhe larenhelL fleld are concerLed Lo Celslus and dlsplayed. noLlce LhaL we can'L Lype negaLlve declmals. 1o flx Lhls go Lo Lhe Craphlcal LayouL and change Lhe LexL fleld's lopot1ype properLy Lo lnclude slgned numbers. 8un agaln. 1hls Llme you should be able Lo enLer negaLlve numbers. 50"& O7 8-"30'() 0D" 4%2" 0D30 D3(2$"/ 0D" P"$#'( K,00%( 4$'4L Much of Lhls ls slmllar Lo SLep 6, alLhough Lhe converslon code ls a llLLle dlfferenL. 1. Cope Lhe followlng code lnLo 1empcoovettetActlvlty.jovo: /** * The user has clicked the button to convert * from Farenheit to Kelvin */ public void convertToKelvinClicked(View v) { double farenheitValue = getEnteredFarenheitTemp(); double celsiusValue = convertFToC(farenheitValue);
// the Kelvin temperature is the difference // between the Celsius value and absolute zero. double kelvinValue = celsiusValue - ABSOLUTE_ZERO_IN_CELSIUS;
// create the string or the Celsius temperature, // retrieving the 'units' from the string resources String kelvinSymbol = getString(R.string.kelvinLabel); String convertedText = String.format("%.1f%s", kelvinValue, kelvinSymbol);
// and write the output to the screen output.setText(convertedText); } 2. 1he only error ls A8SCLu1L_ZL8C_ln_CLLSluS belng undeflned. Pover Lhe cursor over Lhls consLanL and selecL cteote coostoot... Co Lo Lhe Lop of Lhe flle and change Lhe llne Lo read: static final double ABSOLUTE_ZERO_IN_CELSIUS = -273.15; 3. Llnk Lhe coovett 1o kelvlo buLLon Lo Lhe coovett1okelvlocllckeJ meLhod. ?ou saw how Lo do Lhls for Lhe oLher buLLon earller. 4. 8un Lhe program agaln and LesL LhaL Lhe kelvln buLLon works. 50"& Q7 122'() /%H" 4%$%,- 2+(3H'43$$+ 0% 0D" 4%(#"-/'%( #3$," lL would be nlce Lo change Lhe colour of Lhe converslon LexL dependlng on Lhe LemperaLure, so LhaL a Celslus value less LhaL 10C ls dlsplayed ln blue, values beLween 10C and 26C ln yellow and LemperaLures above 26C ln red. 1o do Lhls we need Lo add some code and add some colour 8C8 values Lo our resources flle. 1. Add Lhe followlng meLhod Lo Lhe 1empcoovettetActlvlty.jovo flle: protected void setTextColor(double celsiusTemp) { if (celsiusTemp < 10.0) { output.setTextColor(getResources(). getColor(R.color.ColorCold)); }else if (celsiusTemp > 26.0) { output.setTextColor(getResources(). getColor(R.color.ColorHot)); } else { output.setTextColor(getResources(). getColor(R.color.ColorMidTemp)); } } 2. 1he Lhree colours are referred Lo by colotcolJ, colotnot and colotMlJ1emp. We need Lo deflne Lhese ln our tes/sttloqs.xml flle. Add Lhe llnes: <color name="ColorCold">#8888FF</color> <color name="ColorMidTemp">#FFFF44</color> <color name="ColorHot">#FF8888</color> 3. We sLlll need Lo call set1extcolot from our Lwo buLLon-cllck meLhods. Add Lhe followlng llne of code aL Lhe boLLom of coovett1ocelsloscllckeJ *+' coovett1okelvlocllckeJ (afLer Lhe ouLpuL.seL1exL(...) llne, setTextColor(celsiusValue); 4. 8un Lhe appllcaLlon agaln Lrylng ouL dlfferenL values Lo show Lhe converslon value changlng colour. 50"& 6R7 S-"/"-#'() 3&&$'430'%( /030" When you swlLch away from an appllcaLlon Lo anoLher appllcaLlon (anoLher screen) and Lhen swlLch back, or you change Lhe orlenLaLlon of your phone you expecL Lo see Lhe appllcaLlon ln lLs orlglnal sLaLe. So for lnsLance, lf you Lype ln a value lnLo Lhe LemperaLure converslon appllcaLlon and converL Lo Celslus and Lhen change Lhe orlenLaLlon of Lhe screen (on Lhe emulaLor use CLrl-l11 or CLrl-fn-l11 on Lhe Mac) Lhen you would expecL Lo see all Lhe same daLa. Powever, ln our currenL verslon Lhls does noL happen. 1he value ln Lhe LexL fleld ls preserved, buL Lhe converslon value ln Lhe 1extvlew wldgeL ls losL. 1hls ls because Androld re-bullds your appllcaLlon when you go away and come back. 1hls ls Lo reduce Lhe amounL of memory used when you vlew anoLher appllcaLlon and qulLe a blL of rebulldlng ls requlred anyway lf you change Lhe screen orlenLaLlon. So how do we preserve screen sLaLe? Cne way ls Lo sLore everyLhlng ln Lhe Androld sqllLe daLabase. 1hls ls noL approprlaLe for shorL-llved screen daLa. lor Lhls sLaLe we can geL Androld Lo save lL ln a flle [usL before lL removes our screen and Lhen glves lL back Lo us when lL decldes Lo re-bulld our screen (by calllng onCreaLe). LeL's see how you can Lake advanLage of Lhls. 1. ?ou wlll noLlce ln Lhe oocteote meLhod LhaL lL uses a soveJlostooce5tote parameLer. 1hls wlll conLaln any sLaLe LhaL we saved before Lhe screen was desLroyed. ?ou'll see how Lo save sLaLe ln a momenL. LeL's add some code Lo resLore Lhe sLaLe. Change Lhe onCreaLe meLhod so LhaL lL now has Lhe followlng code: super.onCreate(savedInstanceState); setContentView(R.layout.main);
// get the object to which we will write the converted value output = (TextView)findViewById(R.id.textViewConvertedTemp);
// retrieve and saved display values and re-set them if(savedInstanceState != null) {
if(savedInstanceState.containsKey(TEXT_STRING_KEY)) { output.setText(savedInstanceState. getString(TEXT_STRING_KEY)); } if(savedInstanceState.containsKey(TEXT_COLOR_KEY)) { output.setTextColor(savedInstanceState. getInt(TEXT_COLOR_KEY)); } } 2. 1here are several errors, slnce several consLanLs are undeflned. Add Lhe followlng code aL Lhe Lop of Lhe flle [usL afLer Lhe deflnlLlon of A8SCLu1L_ZL8C_ln_CLSluS: static final String TEXT_STRING_KEY = "SAVED_TEXT"; static final String TEXT_COLOR_KEY = "SAVED_COLOR"; 3. We now have Lo arrange for Lhe sLaLe Lo be saved. We wanL Lo save Lhe converslon LexL and lLs colour. Add Lhe followlng meLhod Lo your flle. robably, nearer Lhe boLLom of your flle, buL lL doesn'L really maLLer where: @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState);
outState.putInt(TEXT_COLOR_KEY, output.getCurrentTextColor()); outState.putString(TEXT_STRING_KEY, output.getText().toString()); } Androld calls Lhls llfecycle meLhod [usL before desLroylng your appllcaLlon's screen. 1hls glves your appllcaLlon Lhe chance Lo sLore daLa. ln Lhls case Lhe meLhod wlll obLaln Lhe currenL LexL colour and Lhe currenL converslon LexL and sLore Lhem ln ootstote. Androld wlll arrange for Lhls Lo be saved. 4. 8un Lhe appllcaLlon agaln and noLlce how Lhe screen sLaLe ls preserved when you swlLch orlenLaLlon. 50"& 66 TU&0'%(3$V7 >,(('() 0D" 3&&$'430'%( %( +%,- 1(2-%'2 &D%(" lf you have an Androld devlce and you have lLs uS8 cable Lhen you mlghL wanL Lo Lry deploylng Lo Lhe devlce (Lhls wlll noL work on lS Cs slnce devlce-speclflc drlvers wlll need Lo be lnsLalled). 1. ConnecL your devlce Lo Lhe C vla Lhe uS8 cable. 2. Some compuLer conflguraLlon may be requlred. none ls requlred for Mac CS x. lor Llnux you wlll need Lo creaLe a rules" flle for your devlce. lor Wlndows see !""#$%%&'(')*#'+,-.&+*/&,0*1%2&3%4/.5627,!"1) where you wlll flnd lnsLrucLlons and Lhe requlred devlce drlver. 3. 1urn on uS8 debugglng on Lhe devlce by selecLlng SeLLlngs->AppllcaLlons->uevelopmenL. Make sure uS8 debugglng ls seL. 4. ln Lhe uevlces vlew make sure you can see your devlce llsLed and LhaL lL ls selecLed. 3. 8un your appllcaLlon agaln. 1hls should deploy your appllcaLlon Lo your phone and Lhen run lL. lL wlll remaln as an AppllcaLlon LhaL you can launch even lf you dlsconnecL Lhe uS8 cable. 5"00'() ,& 0D" "(#'-%(H"(0 50"& 67 W3#3 ?ou wlll need !ava lnsLalled (we recommend verslon 6 or above). CeL Lhe laLesL !ava Suk from Cracle:
Check Lhe verslon lnsLalled wlLh Lhe command llne command: [avac -verslon 50"& :7 G(/03$$'() ;4$'&/" =
Lcllpse ls a wldely used lnLegraLed developmenL envlronmenL LhaL supporLs sofLware developmenL. 1here ls a plug-ln LhaL supporLs Androld developmenL. Androld appllcaLlons can be developed wlLhouL Lcllpse buL you wlll flnd LhaL Lcllpse wlll make your llfe easler. uownload Lcllpse from: !""#$%%444,'0)/#2',*+;%&*4.)*-&2
3 1here are alLernaLlves, such as lnLelll! and neLbeans, buL Lcllpse ls Lhe luL referred Lo Lhe mosL by Lhe Androld communlLy. Lcllpse Classlc ls probably sufflclenL. lf you already have a very old verslon of Lcllpse, Lhen now mlghL be Lhe Llme Lo upgrade (e.g. for Lcllpse Classlc, Androld requlres verslon 3.3 or laLer). 1he Lcllpse slLe should deLermlne whlch download ls approprlaLe for your compuLer. uownload Lhe archlve flle and unpack lL ln a locaLlon LhaL you have wrlLe permlsslon, e.g. your home folder. uo noL use your sysLem's package manager Lo lnsLall Lcllpse. Launch Lhe ecllpse execuLable from Lhe ecllpse folder. ?ou should see someLhlng llke Lhe followlng, alLhough Lhe sLyle wlll vary across operaLlng sysLems.
G(/03$$'() 0D" 1(2-%'2 5!P uownload Lhe approprlaLe verslon for your operaLlng sysLem from: !""#$%%&'(')*#'+,-.&+*/&,0*1%2&3%/.&':,!"1) 1hls ls also a good slLe Lo seL a bookmark Lo, slnce lL conLalns loLs of reference maLerlals. LxLracL Lhe archlve lnLo your home folder. 1he lnsLallaLlon folder has Lwo folders (tools and plotfotm- tools) LhaL you wlll need Lo add Lo your sysLem search paLh. Cn Llnux and Macs seL Lhls ln Lhe .ptoflle (ubunLu) or .bosb_ptoflle (Mac CSx) flle ln your home folder. lor example use Lhe llne: !"#$%& ()*+,-()*+./0123%$435426&1771&4$258$73!%0&$$76./0 5426&1771&4$258$73!% 0#71&8$%95&$$76 Cn Wlndows seL Lhe sysLem paLh envlronmenL varlable vla ConLrol anel->SysLem and SecurlLy- >SysLem->Advanced sysLem seLLlngs->LnvlronmenL varlables wlLh Lhe user aLh varlable lncludlng: :.;123%$435426&1771&4$25#1&<;&$$76=:.;123%$435426&1771&4$25#1&<;#71&8$%95&$$76 lnserL your lnsLallaLlon paLh ln Lhe above. noLe Lhe use of ',' Lo separaLe paLhs! Close and command llne wlndows and reopen Lo plck up Lhe new potb value. 122'() K,'$2 03-)"0/ 0% +%,- 1(2-%'2 '(/03$$30'%( Androld devlces vary ln age and ln capablllLy. 1hese devlces wlll have dlfferenL verslons of Androld supporLed. ConsequenLly, when you develop an appllcaLlon you musL conslder whlch verslons of Androld you wlll LargeL (Lhe bulld LargeLs). ?ou wlll need Lo lnsLall Lhe Androld Suks LhaL bulld appllcaLlons LhaL wlll run on Lhe devlces you wlsh Lo LargeL. 1o lnsLall Suks you use Lhe Androld Suk and Avu 4 Manager Lool. 1o lnvoke Lhls, Lype ooJtolJ on Lhe command llne.
1he above shows Lhe Suks l lnsLalled by cllcklng upJote All. ?ou are Lhen glven an opporLunlLy Lo selecL Lhe packages you wlsh Lo lnsLall. 1he Coogle Als are also good, lf you wanL Lo bulld uslng exLra feaLures such as supporLlng Coogle maps. lnsLalllng Lhem all ls probably a good ldea, buL lL wlll Lake a whlle! Make sure Lhe Androld Suk laLform-Lools package ls lnsLalled and you probably should lnsLall Lhe example appllcaLlons package.
4 Androld vlrLual uevlce G(/03$$'() 0D" 1(2-%'2 !"#"$%&H"(0 X%%$L'0 ;4$'&/" &$,)Y'( 1hls Lool allows you Lo run your !ava Androld appllcaLlons ln an Androld emulaLor or deploy Lo a real phone (whlch ls much fasLer Lhan Lhe emulaLor), Lo debug appllcaLlons, Lo bulld Culs graphlcally, and Lo log evenLs as your appllcaLlon runs and perform oLher Androld-speclflc Lasks. ln Lcllpse selecL nelp->lostoll New 5oftwote. ln Lhe Work wlLh LexL fleld enLer: !""#2$%%&)522),;**;)',0*1%-.&+*/&%'0)/#2'% 1hen cllck Add Lo add Lhe u8L Lo Lhe llsL of slLes from where plug-lns are obLalned. ?ou should Lhen see Lhe ueveloper 1ools lLem as follow. SelecL lL and lnsLall by cllcklng nexL.
8esLarL Lcllpse Lo compleLe Lhe lnsLallaLlon. ?ou wlll Lo Lell Lcllpse where Lo flnd Lhe Androld Suk. Cpen Lhe wloJow->ltefeteoces->AoJtolJ. lor 5uk locotloo, enLer Lhe paLh Lo your Androld lnsLallaLlon.
Z,'$2'() 1(2-%'2 #'-0,3$ 2"#'4"/ 1he lasL Lhlng you need Lo do ls creaLe a vlrLual devlce (or several) LhaL can be used when runnlng your appllcaLlon on Lhe emulaLor. 1he emulaLor uses a vlrLual devlce Lo provlde Lhe same envlronmenL your appllcaLlon would encounLer lf lL was runnlng on a real phone wlLh supporL for Lhe same Suk verslon. 1hls ls parLly why sLarLlng Lhe emulaLor ls very slow (as you wlll flnd ouL). 8un Lhe Suk and Avu Manager (Lype ooJtolJ ln a command wlndow, or cllck Lhe Lcllpse Loolbar Suk and Avu Manager buLLon). 1he vlrLual devlces llsL wlll lnlLlally be empLy (on lS Cs Lhey wlll conLaln lnvalld enLrles). Pere's one l've populaLed wlLh a few reasonable vlrLual devlces:
1o add a vlrLual devlce cllck New. 1he maln flelds Lo enLer are: name (add someLhlng meanlngful such as 1LS1-Avu-Level-8), 1argeL (selecL Lhe Suk LargeL, l suggesL Androld 2.2), Slze (100Ml8 should be sufflclenL), Skln (use Lhe defaulL). AfLer creaLlng Lhe Avu, on lS Cs Lhe flles generaLed wlll be sLored, by defaulL, ln u:/ecllpse/.ooJtolJ folder. normally, on your own C Lhey are sLored ln Lhe .androld folder under you home folder. 1haL's lL for lnsLallaLlon. now you're ready Lo follow Lhe flrsL parL of Lhls worksheeL!