Podcast
Questions and Answers
What is a characteristic of external storage?
What is a characteristic of external storage?
What is required to read/write the device's external storage?
What is required to read/write the device's external storage?
What is a difference between internal and external storage?
What is a difference between internal and external storage?
What is used to read a file in internal storage?
What is used to read a file in internal storage?
Signup and view all the answers
What is used to write a short text file to internal storage?
What is used to write a short text file to internal storage?
Signup and view all the answers
What happens to the external storage when the app is uninstalled?
What happens to the external storage when the app is uninstalled?
Signup and view all the answers
What is the size of internal storage?
What is the size of internal storage?
Signup and view all the answers
What is the purpose of openFileInput and openFileOutput methods?
What is the purpose of openFileInput and openFileOutput methods?
Signup and view all the answers
What is the purpose of passing a unique ID number to startActivityForResult?
What is the purpose of passing a unique ID number to startActivityForResult?
Signup and view all the answers
What happens when startActivityForResult is called?
What happens when startActivityForResult is called?
Signup and view all the answers
What is the purpose of the onActivityResult method?
What is the purpose of the onActivityResult method?
Signup and view all the answers
How does the called activity send a result back?
How does the called activity send a result back?
Signup and view all the answers
What is the purpose of the Intent in the onActivityResult method?
What is the purpose of the Intent in the onActivityResult method?
Signup and view all the answers
Why is it necessary to modify the called activity to send a result back?
Why is it necessary to modify the called activity to send a result back?
Signup and view all the answers
What is the purpose of an adapter in a ListView?
What is the purpose of an adapter in a ListView?
Signup and view all the answers
What is the difference between a static list and a dynamic list?
What is the difference between a static list and a dynamic list?
Signup and view all the answers
What is the purpose of the android:entries attribute in a ListView?
What is the purpose of the android:entries attribute in a ListView?
Signup and view all the answers
What is the default layout for lists in a ListView?
What is the default layout for lists in a ListView?
Signup and view all the answers
What is the method used to attach an adapter to a ListView?
What is the method used to attach an adapter to a ListView?
Signup and view all the answers
What is the type of array that can be used to create an ArrayAdapter?
What is the type of array that can be used to create an ArrayAdapter?
Signup and view all the answers
What is the purpose of the android:id attribute in a ListView?
What is the purpose of the android:id attribute in a ListView?
Signup and view all the answers
What is the location of the file that contains the list of options for a dynamic list?
What is the location of the file that contains the list of options for a dynamic list?
Signup and view all the answers
What method is used to check if the external storage is writable?
What method is used to check if the external storage is writable?
Signup and view all the answers
What permission is required to access web data in Android?
What permission is required to access web data in Android?
Signup and view all the answers
How are sound files referred to in Java code?
How are sound files referred to in Java code?
Signup and view all the answers
What is the purpose of the setLooping method in MediaPlayer?
What is the purpose of the setLooping method in MediaPlayer?
Signup and view all the answers
What is the purpose of the Scanner class in reading web data?
What is the purpose of the Scanner class in reading web data?
Signup and view all the answers
What is the purpose of the getCurrentPosition method in MediaPlayer?
What is the purpose of the getCurrentPosition method in MediaPlayer?
Signup and view all the answers
What is the purpose of the isPlaying method in MediaPlayer?
What is the purpose of the isPlaying method in MediaPlayer?
Signup and view all the answers
How do activities communicate with each other in Android?
How do activities communicate with each other in Android?
Signup and view all the answers
What is the primary difference between internal and external storage?
What is the primary difference between internal and external storage?
Signup and view all the answers
What is the main purpose of the java.io.File class?
What is the main purpose of the java.io.File class?
Signup and view all the answers
What is the purpose of the getFilesDir() method?
What is the purpose of the getFilesDir() method?
Signup and view all the answers
What is the purpose of the java.io.InputStream and OutputStream classes?
What is the purpose of the java.io.InputStream and OutputStream classes?
Signup and view all the answers
What happens to the internal storage when an app is uninstalled?
What happens to the internal storage when an app is uninstalled?
Signup and view all the answers
What is the purpose of the getResources().openRawResource(R.raw.id) method?
What is the purpose of the getResources().openRawResource(R.raw.id) method?
Signup and view all the answers
How much storage is typically available in internal storage?
How much storage is typically available in internal storage?
Signup and view all the answers
What is the purpose of the openFileInput("name", mode) method?
What is the purpose of the openFileInput("name", mode) method?
Signup and view all the answers
Study Notes
ListView
- An ordered collection of selectable choices
- Key attributes in XML:
-
android:clickable
to set the list to be clickable or not -
android:id
to assign a unique ID for use in Java code -
android:entries
to set the options to appear in the list, which must match an array instrings.xml
-
Static Lists
- Content is fixed and known before the app runs
- Declare the list elements in the
strings.xml
resource file - Example: Android, iPhone, Max OS X
Dynamic Lists
- Content is read or generated as the program runs
- Comes from a data file, internet, etc.
- Must be set in Java code
- Example: reading a file
oses.txt
to create a list
List Adapters
- Helps turn list data into list view items
- Common adapters:
ArrayAdapter
,CursorAdapter
- Syntax for creating an adapter:
ArrayAdapter name = new ArrayAdapter(activity, layout, array);
- Attach the adapter to the list by calling the
setAdapter
method of theListView
object in Java code
Waiting for a Result
- If the calling activity wants to wait for a result from the called activity:
- Call
startActivityForResult
instead ofstartActivity
- Pass a unique ID number to represent the action being performed
- Write an
onActivityResult
method that will be called when the second activity is done - Check for the unique ID as was passed to
startActivityForResult
- Ask the intent for any extra data
- Modify the called activity to send a result back using
setResult
andfinish
methods
- Call
Files and Storage
- Android can read/write files from two locations:
- Internal storage
- External storage
Internal Storage
- Built into the device
- Guaranteed to be present
- Typically smaller (~1-4 GB)
- Can't be expanded or removed
- Specific and private to each app
- Wiped out when the app is uninstalled
File and Streams
-
java.io.File
represents a file or directory - Methods:
canRead
,canWrite
,create
,delete
,exists
,getName
,getParent
,getPath
,isFile
,isDirectory
,lastModified
,length
,listFiles
,mkdir
,mkdirs
,renameTo
-
java.io.InputStream
,OutputStream
represent flows of data bytes from/to a source or destination - Stream objects are often passed as a parameter to other objects like
java.util.Scanner
,java.io.BufferedReader
,java.io.PrintStream
to do the actual reading/writing
Using Internal Storage
- An activity has methods to read/write files:
-
getFilesDir()
returns internal directory for the app -
getCacheDir()
returns a "temp" directory for scrap files -
getResources().openRawResource(R.raw.id)
reads an input file fromres/raw/
-
openFileInput("name", mode)
opens a file for reading -
openFileOutput("name", mode)
opens a file for writing
-
- Can use these methods to read/write files on the device
- Many methods return standard
java.io.File
objects - Some return
java.io.InputStream
orOutputStream
objects, which can be used with standard classes likeScanner
,BufferedReader
, andPrintStream
to read/write files
Internal Storage Examples
- Example 1: read a file and put its contents into a
TextView
- Example 2: write a short text file to the internal storage and read it back
External Storage
- External storage: Card that is inserted into the device (such as a MicroSD card)
- Can be much larger than internal storage (~8-32 GB)
- Can be removed or transferred to another device if needed
- May not be present, depending on the device
- Read/writable by other apps and users; not private to the app
- Not wiped when the app is uninstalled, except in certain cases
External Storage Permission
- If the app needs to read/write the device's external storage, it must explicitly request permission to do so in the
AndroidManifest.xml
file - On install, the user will be prompted to confirm the app's permissions
Checking if Storage is Available
-
isExternalStorageWritable()
method to check if external storage is writable -
isExternalStorageReadable()
method to check if external storage is readable
Accessing Web Data
- To read data from the web, request the
INTERNET
permission in theAndroidManifest.xml
file - Use the
java.net.URL
class to connect to a file or page at a given URL and read its data
Playing Sound Effects
- Find sound files such as
.WAV
,.MP3
- Put sound files in the project folder
app/src/main/res/raw
- Refer to audio files in Java code as
R.raw.filename
- Load and play clips using Android's
MediaPlayer
class - Methods:
create
,start
,stop
,pause
,isLooping
,isPlaying
,getCurrentPosition
,release
,seekTo
,setDataSource
,setLooping
Multiple Activities
- Many apps have multiple activities
- Example: address book app with main activity for listing contacts and another activity for viewing details
- An activity can launch another activity in response to an event
- The activity can pass data to the other activity
- The second activity can send data back to the first activity when it is done
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Understanding the attributes and functionality of ListView in Android development.