Lesson 3.3 Support Libraries
The Android SDK includes the Android Support Library, which is a collection of several libraries. These libraries provide features that aren’t built into the Android framework, including the following:
- Backward-compatible versions of framework components, so that apps running on older versions of the Android platform can support features made available in newer versions of the platform
- Additional layout and user interface elements
- Support for different device form factors, such as TV devices or wearables
- Components to support Material Design elements
- Other features, including palette support, annotations, percentage-based layout dimensions, and preferences
What you’ll do
- Create a new app with one
TextView
and oneButton
. - Verify that the Android Support Repository (containing the Android Support Library) is available in your Android Studio installation.
- Explore the
build.gradle
files for your app project. - Manage class or method calls that are unavailable for the version of Android your app supports.
- Use a compatibility class from the support library to provide backward-compatibility for your app.
1.1 Verify that the Android Support Repository is available
- In Android Studio, select Tools > Android > SDK Manager, or click the SDK Manager Icon
The Android SDK Default Preferences pane appears.
Click the SDK Tools tab and expand Support Repository,
If Installed appears in the Status column, you’re all set. Click Cancel.
1.2 Set up the project and examine build.gradle
- Create a new project called HelloCompat.
On the Target Android Devices page, API 15: Android 4.0.3 (IceCreamSandwich) is selected for the minimum SDK. As you’ve learned in previous lessons, this is the oldest version of the Android platform your app will support.
Explore build.gradle (Module:app)
- In Android Studio, make sure the Project > Android pane is open.
- Expand Gradle Scripts and open the build.gradle (Module: app) file.
- Locate the
compileSdkVersion
line near the top of the file. For example:
compileSdkVersion 26
Locate the minSdkVersion
line in the defaultConfig
section a few lines down.
Locate the targetSdkVersion
line in the defaultConfig
section. For example:
targetSdkVersion 26
Locate the dependencies
section of build.gradle, near the end of the file. For example:
- Update the
compileSdkVersion
number, if necessary - Update the version numbers, if necessary.
Task 2: Implement the layout and MainActivity
2.1 Change the layout and colors
- Open activity_main.xml in the Project > Android pane.
- Click the Design tab (if it is not already selected) to show the layout editor.
- Select the “Hello World”
TextView
in the layout and open the Attributes pane.
Delete the constraint that stretches from the bottom of the hello_textview
TextView
to the bottom of the layout, so that the TextView
snaps to the top of the layout, and choose 8 (8dp) for the top margin as shown below.
Drag a Button
to the bottom of the layout, and add constraints to the left and right sides and bottom of the layout, as shown in the figure below.
- Change the layout_width attribute in the Attributes pane for the
Button
to match_constraint. - Change the other attributes in the Attributes pane for the
Button
as follows:
Add the following android:onClick
attribute to the Button
:
android:onClick="changeColor"
To add colors, expand res and values in the Project > Android pane, and open colors.xml.
<color name="red">#F44336</color>
<color name="pink">#E91E63</color>
<color name="purple">#9C27B0</color>
<color name="deep_purple">#673AB7</color>
<color name="indigo">#3F51B5</color>
<color name="blue">#2196F3</color>
<color name="light_blue">#03A9F4</color>
<color name="cyan">#00BCD4</color>
<color name="teal">#009688</color>
<color name="green">#4CAF50</color>
<color name="light_green">#8BC34A</color>
<color name="lime">#CDDC39</color>
<color name="yellow">#FFEB3B</color>
<color name="amber">#FFC107</color>
<color name="orange">#FF9800</color>
<color name="deep_orange">#FF5722</color>
<color name="brown">#795548</color>
<color name="grey">#9E9E9E</color>
<color name="blue_grey">#607D8B</color>
<color name="black">#000000</color>
2.2 Add behavior to MainActivity
Open MainActivity.
Add a private
variable at the top of the class to hold the TextView
object.
private TextView mHelloTextView;
Add the following color array just after the private variable:
private String[] mColorArray = {"red", "pink", "purple", "deep_purple",
"indigo", "blue", "light_blue", "cyan", "teal", "green",
"light_green", "lime", "yellow", "amber", "orange", "deep_orange",
"brown", "grey", "blue_grey", "black" };
Each color name corresponds to the name of a color resource in color.xml
.In the onCreate()
method, use findViewById()
to get a reference to the TextView
instance and assign it to that private variable:
mHelloTextView = findViewById(R.id.hello_textview);
Also in onCreate()
, restore the saved instance state, if any:
// restore saved instance state (the text color)
if (savedInstanceState != null) {
mHelloTextView.setTextColor(savedInstanceState.getInt("color"));
}
Add the onSaveInstanceState()
method to MainActivity
to save the text color:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save the current text color
outState.putInt("color", mHelloTextView.getCurrentTextColor());
}
he final code would be like this :
Task 3: Implement Button behavior
2.1 Add the changeButton() click handler
- Open activity_main.xml, if it is not already open. Click the Text tab to show the XML code.
- Click on
"changeColor"
in theandroid:onClick
attribute inside theButton
element. - Press
Alt+Enter
(Option+Enter
on a Mac), and select Create onClick event handler. - Choose MainActivity and click OK.
2.2 Implement the Button action
- Switch to MainActivity.
- In the
changeColor()
method, create a random number object by using theRandom
class (a Java class) to generate simple random numbers.
Random random = new Random();
Use the random
instance to pick a random color from the mColorArray
array:
String colorName = mColorArray[random.nextInt(20)];
The nextInt()
method with the argument 20 gets another random integer between 0 and 19. You use that integer as the index of the array to get a color name.
Get the resource identifier (an integer) for the color name from the resources:
int colorResourceName = getResources().getIdentifier(colorName,
"color", getApplicationContext().getPackageName());
Get the integer ID for the actual color from the resources and assign it to a colorRes
variable, and use the getTheme()
method to get the theme for the current application context.
int colorRes =
getResources().getColor(colorResourceName, this.getTheme());
Change the colorRes
assignment line to use the ContextCompat
class:
int colorRes = ContextCompat.getColor(this, colorResourceName);
Set the color of the TextView
to the color resource ID:
mHelloTextView.setTextColor(colorRes);
Run the app on a device or emulator, and click the Change Color Button
.
The final code would be like this :
Run :
yeah :))