Android App Development 2 Flashcards
Features of the onCreate() method
- All subclasses of Activity must implement it.- Receives an intent if started from another activity- Must define the activity layout with the setContentView() method.- Performs the initial setup of the activity components
AndroidManifest.xml
The manifest file defines the app package and defines things such as the launcher icon, a theme and definitions of the app’s activities.It may also define permissions.
The element within the element in XML
The element defines how the activity can be used.
How to identify in XML which activity is the first activity the user sees when they launch the app.
An element which has an action of MAIN and category of LAUNCHER
Intent
An object which provides runtime binding between separate components (such as two activities).Represents an app’s ‘intent to do something’.
Parameters for creating a new intent
1). A Context- In most cases you will pass ‘this’ since you are passing the current acitivity and the Activity is a subclass of Context.2). The Class of the app component to which the system should deliver the intent: activityName.class
How to retrieve an element in the source code
findViewById(R.id.my_id)Remember to cast it to the appropriate type.
How to add additional stuff to an intent
Use the putExtra() method
Parameters of the putExtra() method
It takes a key-value pair (called an extra).Key (1st param): It’s good practice to use your package name as a prefix for this. E.g. com.mycompany.myapp.MESSAGE. The key can be declared as a public constant in the class so it can be used multiple times.Value (2nd param): This is the stuff you want to send.
How to start a new activity
Most commonly done by using intents.startActivity(intentObj)
All activities are invoked by an intent. How would you retrieve this intent?
Call the getIntent() function- returns the intent object:Intent myIntent = getIntent();
An intent sends an extra which is a string data type. How would you retrieve this from the activity which was invoked via this intent (Called MyActivity)?
Intent intent = getIntent();String msg = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);(Here EXTRA_MESSAGE is a static string defined in MyActivity which is how this is possible- otherwise you’d need to create an object of the class first).
Why should you put strings in the strings.xml file instead of hardcoding them?
So you can support multiple languages in the long run.
How to add support for more langauges?
Create additional values directories inside the res/ that includes a hyphen and the ISO language code at the end of the directory name.Example:MyProject/res/values/strings.xml (This is the default)MyProject/res/values-es/strings.xml (Spanish)MyProject/res/values-fr/strings.xml (French)
How to refer to a string resource
“IN CODE:R.string.Example:TextView textView = new TextView(this);textView.setText(R.string.hello_world);IN XML:@string/Example:android:layout_width=”“wrap_content”” android:layout_height=”“wrap_content”” android:hint=”“@string/edit_message”” />(Note the string resource name attribute can be called anything- in the example it is the same as the ID just to make it clear what element it’s for).”
Generalised screen sizes
- Small- Normal- Large- Xlarge
Generalised screen densities
- Low (ldpi)- Medium (mdpi)- High (hdpi)- Extra high (xhdpi)
How to support different screen sizes.
Create a unique layout XML file for each screen size you want to support. These should go in the relevant directories similar to the structure the string resources were.General resources directory syntax: layout-Example:MyProject/res/layout/main.xml (default)MyProject/res/layout/layout-large/main.xml (Large screens)
What to consider when supporting different screen sizes.
Focus on the layout structure that affects the user experience (such as the size or position of important views relative to sibling views).Android automatically scales your layout in order to properly fit the screen, thus your layouts for different screen sizes don’t need to worry about the absolute size of UI elements.Note: Portrait and landscape are considered as two different screen ‘sizes’ so you should have different layout files for these as well.
How to generate images for different screen densities (which you should do!)
Start with your raw resource in vector format and generate the images for each density using the appropriate scales:- xhdpi: 2.0- hdpi: 1.5- mdpi: 1.0 (baseline)- ldpi: 0.75Place each image resource in the appropriate drawable resource directory.
Factor to scale an image for extra-high density screen
2.0
Factor to scale an image for high density screen
1.5