9. In the styles.xml file, modify the TextView style so it sets the layout_width
layout height, and padding attributes for TextView and Edit Text widgets to
the values that are currently stored in the XML file for the layout. Then, delete
these attributes from the XML file for the layout. This should reduce code
10. In the styles.xml file, change the padding attribute of the TextView style to
10dp. This should set the space between the widgets.
11. In the styles.xml file, modify the Button style so it sets the layout_width and
layout_height attributes to 40dp. Then, switch to the XML for the layout and
delete these attributes from that file. This should make both buttons a little
smaller, and it should reduce code duplication.
12. Add two new TextView widgets to the form for a Per Person label and amount
Set the id, text, and alignment attributes for these widgets appropriately.
Then, apply the TextView.Label style to the label and the TextView style to
the amount. This is an easy way to apply consistent formatting to these new
Exercise 7-3 Modify a theme
In this exercise, you'll modify the theme that's used by the Tip Calculator app
presented in this chapter.
1. Open the project named ch07_ex3_TipCalculator.
2. Open the styles.xml file in the values directory. This file modifies the theme
to automatically apply custom styles for the TextView, Edit Text, and Button
widgets. This file also includes a custom style named TextView.Label that can
be applied manually.
3. Open the layout for the app and view its XML. This XML only applies the
4. Add a RadioGroup.widget to the layout. Note that the radio buttons within the
group use a different text size than the other widgets in the layout.
5. Modify the styles.xml file so it modifies the TextAppearance styles for the
theme instead of modifying the styles for individual widgets. This should
set the textSize property to 20sp for the TextAppearance style and its Small
Medium, and Inverse variations. When you're done, all widgets should use a
textSize of 20sp
Use built-in themes
2. Open the styles.xml files in the values and values-v21 directories. These files
In this exercise, you'll modify the Tip Calculator app so it uses built-in themes
to display a dark background.
1. Open the project named cho7_exl_TipCalculator
each specify a built-in theme for the API level.
3. Open the AndroidManifest.xml file and review its code. It sets the custom
theme named AppTheme as the theme for the application.
4. Open the layout for the app and view it in the graphical layout editor. Use
the Android Version button to view the layout for API levels 16 and 23. This
theme should look different for both of these APIs.
5. Edit the two styles.xml files so the app uses the theme named Theme.Holo
prior to API 21 and uses Theme. Material for API 21 and later.
6. Open the layout for the app and view it in the graphical layout editor. These
themes should display a dark background for all APIs.
In this exercise, you'll modify the styles for the Tip Calculator app presented in
1. Open the project named ch07_ex2_TipCalculator.
2. Open the Gradle build script for this file. Note that it includes a dependency
for the v7 appcompat library.
3. Open the class for the activity. Note that it extends the AppCompatActivity
4. Open the styles.xml file in the values directory. Note that this style uses the
AppCompat theme. Note also that it defines several custom styles.
5. Open the layout for the activity and view its XML. Each widget applies one of
the custom styles specified in the styles.xml file.
6. For the first TextView widget, delete the style attribute that applies the style.
This should remove the style's formatting from that widget. Then, restore this
7. For the last TextView widget, change the style from TextView.Label to
TextView.Label.Indent. This should indent the Total label. Then, change the
style back to the TextView.Label style.
8. In the styles.xml file, change the textSize attribute of the TextView style to
16sp. This should change the text size for all widgets on the layout.
Purchase answer to see full