Podcast
Questions and Answers
What is the first step in modifying the onCreate() method?
What is the first step in modifying the onCreate() method?
What is the purpose of creating a LayoutParams object?
What is the purpose of creating a LayoutParams object?
What is the final step in adding the Button view to the activity?
What is the final step in adding the Button view to the activity?
What is the purpose of adding some additional rules to the parameters?
What is the purpose of adding some additional rules to the parameters?
Signup and view all the answers
What is the result of modifying the onCreate() method?
What is the result of modifying the onCreate() method?
Signup and view all the answers
What modification needs to be made to the Button view?
What modification needs to be made to the Button view?
Signup and view all the answers
What is the primary purpose of overriding the onDraw method in a custom View subclass?
What is the primary purpose of overriding the onDraw method in a custom View subclass?
Signup and view all the answers
What is the direction of the y-axis in the Canvas object?
What is the direction of the y-axis in the Canvas object?
Signup and view all the answers
What is the purpose of the Paint object in the Canvas object?
What is the purpose of the Paint object in the Canvas object?
Signup and view all the answers
What is the method used to draw a circle in the Canvas object?
What is the method used to draw a circle in the Canvas object?
Signup and view all the answers
What is the purpose of using a thread in a custom View subclass?
What is the purpose of using a thread in a custom View subclass?
Signup and view all the answers
What is the method used to fill the window with a color in the Canvas object?
What is the method used to fill the window with a color in the Canvas object?
Signup and view all the answers
What is the data type of the x, y, and r parameters in the drawCircle method?
What is the data type of the x, y, and r parameters in the drawCircle method?
Signup and view all the answers
What is the method used to get the dimensions of the drawing area in the Canvas object?
What is the method used to get the dimensions of the drawing area in the Canvas object?
Signup and view all the answers
What is the purpose of the setARGB method in the Paint class?
What is the purpose of the setARGB method in the Paint class?
Signup and view all the answers
What is the Typeface style used to create a bold and italic font?
What is the Typeface style used to create a bold and italic font?
Signup and view all the answers
How can you create a Typeface from a file in the src/main/assets/ directory?
How can you create a Typeface from a file in the src/main/assets/ directory?
Signup and view all the answers
What is the method used to draw a Bitmap on a Canvas?
What is the method used to draw a Bitmap on a Canvas?
Signup and view all the answers
What is the purpose of the setTextSize method in the Paint class?
What is the purpose of the setTextSize method in the Paint class?
Signup and view all the answers
What is the method used to decode a Bitmap from a resource?
What is the method used to decode a Bitmap from a resource?
Signup and view all the answers
What is the purpose of the setStyle method in the Paint class?
What is the purpose of the setStyle method in the Paint class?
Signup and view all the answers
What is the goal of the target exercise?
What is the goal of the target exercise?
Signup and view all the answers
What is the primary benefit of using a Thread in Android?
What is the primary benefit of using a Thread in Android?
Signup and view all the answers
What is the purpose of using a Handler object in Android?
What is the purpose of using a Handler object in Android?
Signup and view all the answers
What is the recommended way to update a View in a separate thread?
What is the recommended way to update a View in a separate thread?
Signup and view all the answers
What is the purpose of the onTouchEvent method in a custom View class?
What is the purpose of the onTouchEvent method in a custom View class?
Signup and view all the answers
What is the requirement for updating the ball in the bouncing ball exercise?
What is the requirement for updating the ball in the bouncing ball exercise?
Signup and view all the answers
What is the constraint on the ball's velocity in the bouncing ball exercise?
What is the constraint on the ball's velocity in the bouncing ball exercise?
Signup and view all the answers
What is the purpose of the Looper object in the Handler constructor?
What is the purpose of the Looper object in the Handler constructor?
Signup and view all the answers
What is the background color of the screen in the bouncing ball exercise?
What is the background color of the screen in the bouncing ball exercise?
Signup and view all the answers
What is the percentage decrease in the size of each consecutive circle?
What is the percentage decrease in the size of each consecutive circle?
Signup and view all the answers
What is the purpose of calling the invalidate method on a view?
What is the purpose of calling the invalidate method on a view?
Signup and view all the answers
What is the primary purpose of using threads in a program?
What is the primary purpose of using threads in a program?
Signup and view all the answers
What is the relationship between threads within the same process?
What is the relationship between threads within the same process?
Signup and view all the answers
What is the purpose of the Paint object in the TargetView class?
What is the purpose of the Paint object in the TargetView class?
Signup and view all the answers
What is the effect of the loop in the onDraw method?
What is the effect of the loop in the onDraw method?
Signup and view all the answers
What is the challenge mentioned in the text?
What is the challenge mentioned in the text?
Signup and view all the answers
What is the purpose of the RectF object in the onDraw method?
What is the purpose of the RectF object in the onDraw method?
Signup and view all the answers
What is the purpose of the onTouchEvent method in a custom View class?
What is the purpose of the onTouchEvent method in a custom View class?
Signup and view all the answers
What is the return type of the onTouchEvent method?
What is the return type of the onTouchEvent method?
Signup and view all the answers
What is the purpose of the requestFocus() method in a View constructor?
What is the purpose of the requestFocus() method in a View constructor?
Signup and view all the answers
What is the name of the method used to detect collisions between sprites in a game?
What is the name of the method used to detect collisions between sprites in a game?
Signup and view all the answers
What is the purpose of the WakeLock?
What is the purpose of the WakeLock?
Signup and view all the answers
What is the name of the class used to represent objects of interest in a game?
What is the name of the class used to represent objects of interest in a game?
Signup and view all the answers
What is the purpose of the onKeyDown method in a custom View class?
What is the purpose of the onKeyDown method in a custom View class?
Signup and view all the answers
What is the purpose of using a smaller collision rectangle in collision detection?
What is the purpose of using a smaller collision rectangle in collision detection?
Signup and view all the answers
What is the primary purpose of overriding the onTouchEvent method in a custom View class?
What is the primary purpose of overriding the onTouchEvent method in a custom View class?
Signup and view all the answers
What is the purpose of using a WakeLock?
What is the purpose of using a WakeLock?
Signup and view all the answers
What is the purpose of the onKeyDown method in a custom View class?
What is the purpose of the onKeyDown method in a custom View class?
Signup and view all the answers
What is the primary purpose of the RectF class in Android?
What is the primary purpose of the RectF class in Android?
Signup and view all the answers
What is the primary purpose of the Sprite class in a game?
What is the primary purpose of the Sprite class in a game?
Signup and view all the answers
What is the primary purpose of using a smaller collision rectangle in collision detection?
What is the primary purpose of using a smaller collision rectangle in collision detection?
Signup and view all the answers
What is the name of the method used to detect collisions between sprites in a game?
What is the name of the method used to detect collisions between sprites in a game?
Signup and view all the answers
What is the primary purpose of the requestFocus method in a View constructor?
What is the primary purpose of the requestFocus method in a View constructor?
Signup and view all the answers
What is the primary purpose of the setFocusableInTouchMode method in a View?
What is the primary purpose of the setFocusableInTouchMode method in a View?
Signup and view all the answers
What is the primary purpose of the onKeyDown/Up methods in a custom View class?
What is the primary purpose of the onKeyDown/Up methods in a custom View class?
Signup and view all the answers
Study Notes
Creating the Example Project in Android Studio
- The
onCreate()
method is initially designed to use a resource layout file for the user interface. - To add views to an activity, the line related to the resource layout file must be deleted from the
onCreate()
method.
Adding Views to an Activity
- A
RelativeLayout
object with a singleButton
view child is created programmatically in theonCreate()
method. - The
Button
view is configured to display the text "Press Me".
Setting View Properties
- The background of the
RelativeLayout
view is set to blue. - The
Button
view is set to display the text "Press Me".
Adding Layout Parameters and Rules
- A
LayoutParams
object is created and initialized with appropriate values to position the button. - The
LayoutParams
object is used to center the button both horizontally and vertically within the layout. - The
LayoutParams
object is passed as an argument when adding the child view to the parent.
Drawing 2D Graphics
- To draw custom 2D graphics, create a custom View subclass with the drawing code. If the app is animated, use a thread to periodically update the graphics and redraw them.
- Use the
onDraw
method to draw on the view, which is called when the view is redrawn.
Custom View
- Create a custom view by extending the
View
class and overriding theonDraw
method. - Insert the custom view into an activity's layout XML.
Canvas Object Methods
-
drawARGB
- fill the window with a color (RGB values: 0-255) -
drawArc
- draw a partial ellipse -
drawBitmap
- draw an image -
drawCircle
- draw a circle -
drawLine
- draw a line segment -
drawOval
- draw an oval or circle (Android 5.0 and above) -
drawPoint
- color a single pixel -
drawRect
- draw a rectangle (Android 5.0 and above) -
drawRoundRect
- draw a rounded rectangle (Android 5.0 and above) -
drawText
- draw a text string -
getWidth
andgetHeight
- get the dimensions of the drawing area
Paint
- Create a Paint object by specifying an alpha (opacity) value and RGB integer values.
- Set the paint style, stroke width, and text size using various methods.
- Use the Paint object to set the color, font, and style of the drawn shapes.
Typeface
- In Android, a font is called a Typeface.
- Set a font inside a Paint object.
- Create a Typeface based on a specific font name or a general font family.
- Load a Typeface from a file in the assets directory.
Bitmap Images
- Draw an image using the Bitmap class.
- Decode a Bitmap from a resource using
BitmapFactory.decodeResource
. - Decode a Bitmap from an input stream using
BitmapFactory.decodeStream
.
Animation via Redrawing
- To animate a view, redraw it at regular intervals and change the variables or positions of the shapes.
- Force a view to redraw itself by calling its
invalidate
method. - Use a thread to redraw the view at regular intervals, but avoid locking up the app's UI.
Threads
- A thread is a lightweight process or isolated sub-task within a program.
- Use a thread to implement concurrency and perform multiple tasks simultaneously.
- Variables created in one thread can be seen by other threads.
Using a Thread
- Create a thread by passing a Runnable object with a
run
method. - Use the
start
method to start the thread. - Use the
stop
method to stop the thread. - Use the
sleep
method to pause the thread. - Use the
isRunning
method to check if the thread is running. - Use the
join
method to wait for the thread to finish.
Redrawing a View in a Thread
- Use a Handler object to make the
invalidate
call on the view from the thread. - Create a Handler object and post a Runnable to it to redraw the view.
Mouse Touch Events
- Handle finger presses from the user by writing an
onTouchEvent
method in the custom View class. - Use the
getAction
method to determine the type of event (e.g., ACTION_DOWN, ACTION_UP, ACTION_MOVE).
Keyboard Events
- Handle key presses by setting the app to receive keyboard focus in the View constructor.
- Write
onKeyDown
andonKeyUp
methods in the custom View class. - Use the
getKeyCode
method to determine the key pressed.
Sprite Class
- A sprite is an object of interest in a game.
- A sprite class can represent the sprites in the game.
- The sprite class can contain data such as location, size, velocity, shape/image, and points.
Collision Detection
- Collision detection is determining whether sprites in the game world are touching each other.
- Use RectF and other shapes to check for collisions using methods such as
contains
,intersects
, andcollide
. - Use a smaller collision rectangle to give the collisions a bit of slack.
Wake Lock
- Use a wake lock to prevent the screen from blanking.
List Adapter Example
- An
ArrayList
is loaded from a file and used to create anArrayAdapter
with the layoutandroid.R.layout.simple_list_item_1
. - The adapter is then set to a
ListView
with the IDmylist
.
Handling List Events
- List views respond to several events, including
setOnItemClickListener
,setOnItemLongClickListener
, andsetOnItemSelectedListener
. - Event listeners must be attached in Java code, not in XML, and require the use of anonymous inner classes.
Attaching Event Listeners in Java
- Event listeners can be attached using anonymous inner classes, which are declared and constructed on the fly.
- An example of attaching an event listener to a button is provided.
List Events
- List views respond to several events, including
setOnItemClickListener
,setOnItemLongClickListener
, andsetOnItemSelectedListener
. - An example of attaching a listener to a
ListView
is provided, which responds to item clicks.
Anatomy of an Android App and the App Lifecycle
Application Components
- Android apps consist of four primary components: Activities, Services, Content Providers, and Broadcast Receivers.
- Each component has a different purpose and lifecycle.
Activity
- An Activity is a single screen with a user interface, and an app may have several Activities.
- Activities are subclasses of the
Activity
class.
Service
- A Service is an application component that performs long-running operations in the background with no UI.
- An example of a Service is an app that automatically responds to texts when driving.
Content Providers
- A Content Provider is a bridge between applications to share data.
- An example of a Content Provider is the device's contacts information.
Broadcast Receivers
- A Broadcast Receiver is a component that responds to system-wide announcements.
- Examples of broadcasts include battery low, screen off, and date changed.
Intents
- Intents are used to pass information between applications.
Activity Stack
- The most recently created Activity is at the top of the stack.
- When the user presses the Back button or an Activity is destroyed, the previous Activity is brought to the top.
Saving and Restoring State
- The
onSaveInstanceState
method is used to save the state of an Activity. - The
onRestoreInstanceState
method is used to restore the state of an Activity.
Starting Your Own Activities
- New Activities can be started within an Activity to accomplish a task or get some data.
- An example of starting a new Activity to get a name is provided.
Drawing 2D Graphics
- Custom 2D graphics can be drawn on screen by creating a custom
View
subclass with the drawing code. - If the app is animated, a thread can be used to periodically update the graphics and redraw them.
Canvas Object Methods
- Several methods can be used to draw on a
Canvas
object, includingdrawARGB
,drawArc
,drawBitmap
, and more.
Paint
- A
Paint
object can be used to specify a color to use for drawing. - Many methods accept a
Paint
object as a parameter.
Touch Events
- The
onTouchEvent
method can be overridden to handle touch events.
Keyboard Events
- Keyboard events can be handled by setting the app to receive keyboard "focus" in the
View
constructor. - The
onKeyDown
andonKeyUp
methods can be overridden to handle key presses.
Sprite Class
- A
Sprite
class can be used to represent objects of interest in a game. - Possible data for a
Sprite
include location, size, velocity, shape/image, and points.
Collision Detection
- Collision detection involves determining whether sprites in the game world are touching each other.
- Android's
RectF
and other shapes have methods to check whether they touch. - Collision detection can be more complex for non-rectangular sprites.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Learn how to add views to an activity in Android Studio by creating a RelativeLayout and Button programmatically in the onCreate() method.