170 likes | 179 Views
Digital Alarm Clock By Keith Lynn. Field Trip #32. Views. A View is the basic building block for a GUI component Almost every component we add to an application is a View Some views are simple Some are complex Some view allow us to contain other views. RelativeLayout.
E N D
Digital Alarm Clock By Keith Lynn Field Trip #32
Views • A View is the basic building block for a GUI component • Almost every component we add to an application is a View • Some views are simple • Some are complex • Some view allow us to contain other views
RelativeLayout • RelativeLayout is a View that contains other views • The RelativeLayout makes use of the id of a View • When we place a component on the RelativeLayout, we create an instance of a RelativeLayout.LayoutParams • We can use this to specify the width and height of a component
RelativeLayout.LayoutParams • In addition to specifying the width and height of a component, we can also specify where the component will be placed relative to other components • We do this by adding a rule to the layout params • There are several constants we can use: RelativeLayout.BELOW, RelativeLayout.ABOVE, etc.
LinearLayout • LinearLayout is another View that contains other Views • It lays them out next to each other • We can also create a LinearLayout.LayoutParams
EditText • An EditText is a View which allows the user to enter data • The EditText like all other views is created as part of a context • We can retrieve the text in an EditText by using the method getText()
Button • A Button is a simple component • Typically the button is a gray rectangle • We are not required to place a label in the Button • A Button like an EditText is created in a Context • When we want to detect and act on a button click, we attach an OnClickListener to it
Spinners • Another component we can use is the Spinner • The Spinner places text in a list and we can scroll through the list • We can determine which item was selected with the method getSelectedItem()
Events • Clicking a Button generates an OnClick event • We can detect and act on a button click by creating an instance of an OnClickListener • OnClickListener is an inner class of View • It contains the method onClick(View v) • When the button is clicked and an OnClickListener is attached to it, its onClick method is called and a reference to the source is sent to it
Threads • A thread is a single line of execution in a program • When we start an app, a thread is created that handles the app • It will detect events and call call the listeners that are listening for them • If we want time consuming work to be performed, then we cannot place that work in the main thread of the app • It would cause the app to freeze
Threads, cont'd • In order to do time consuming work, we create a parallel thread • Thread implements the Runnable interface • Typically we create a Runnable class and then use this Runnable class to create the Thread • The method run determines what the thread does • We do not call the run method directly • We allow the system to start the Thread (and call the run method) by calling start()
Threads, cont'd • We control the thread by using a special variable • We should never call the stop method on a thread • Instead of placing an infinite loop in a thread, we create a loop controlled by a variable • When we want the thread to stop, we change the variable
ImageView • In order to place images on a View, we create an ImageView • We place an image in the ImageView by creating an instance of a Drawable object and then place that in the ImageView with the method setImageDrawable • In order to create the Drawable object, we drag the image into one of the drawable folders • This generates an id in R.java • We then use this id to create the Drawable
Image • The image itself is a stadard image • Typically we use a gif, jpg, or png • When we drag the image into a drawable folder, an id is created in R.java • Typically the id is R.drawable.imagename • Notes that the extension is not included in the id
Sound • In order to add sound to an app, we create the folder raw under res • We then drag the sound file into raw • This will create an id in R.java • The id will be be R.raw.soundfilename • We play the sound by creating an instance of a MediaPlayer using the id • We start the sound playing by using the start method
Obtaining System Time • In order to obtain the system time, we use the Calendar class • We can create an instance of a Calendar with the method Calendar.getInstance() • We then use the get method to specify what information we want • We can send the constants Calendar.HOUR_OF_DAY, Calendar.MINUTE, and Calendar.SECOND
Creating an Alarm • In order to create the alarm, we will place EditTexts and a Spinner on the app to allow the user to specify what time the alarm is for • We then create a Thread which will obtain the system time and display it • After we set the alarm and we reach that time, an alarm goes off