More on Layout Attributes

Let’s add a few more tweaks to the design of list_item_crime.xml and, in the process, answer some lingering questions you might have about widgets and attributes.

Navigate back to the Design view of list_item_crime.xml. Select crime_title and adjust some of the attributes in the properties view.

Click the disclosure arrow next to textAppearance to reveal a set of text and font attributes. Update the textColor attribute to @android:color/black (Figure 9.27).

Figure 9.27  Updating the title color

Figure shows textcolour reading, @android:color/black.

Next, set the textSize attribute to 18sp. Run CriminalIntent and be amazed at how much better everything looks with a fresh coat of paint.

Screen pixel densities and dp and sp

In list_item_crime.xml, you have specified attribute values in terms of sp and dp units. Now it is time to learn what they are.

Sometimes you need to specify values for view attributes in terms of specific sizes (usually in pixels, but sometimes points, millimeters, or inches). You see this most commonly with attributes for text size, margins, and padding. Text size is the pixel height of the text on the device’s screen. Margins specify the distances between views, and padding specifies the distance between a view’s outside edges and its content.

As you saw in the section called Adding an Icon in Chapter 2, Android automatically scales images to different screen pixel densities using density-qualified drawable folders (such as drawable-xhdpi). But what happens when your images scale, but your margins do not? Or when the user configures a larger-than-default text size?

To solve these problems, Android provides density-independent dimension units that you can use to get the same size on different screen densities. Android translates these units into pixels at runtime, so there is no tricky math for you to do (Figure 9.28).

Figure 9.28  Dimension units in action on TextView

Set of three screenshots are shown.

px

Short for pixel. One pixel corresponds to one onscreen pixel, no matter what the display density is. Because pixels do not scale appropriately with device display density, their use is not recommended.

dp (or dip)

Short for density-independent pixel and usually pronounced dip. You typically use this for margins, padding, or anything else for which you would otherwise specify size with a pixel value. One dp is always 1/160th of an inch on a device’s screen. You get the same size regardless of screen density: When your display is a higher density, density-independent pixels will expand to fill a larger number of screen pixels.

sp

Short for scale-independent pixel. Scale-independent pixels are density-independent pixels that also take into account the user’s font size preference. You will almost always use sp to set display text size.

pt, mm, in

These are scaled units, like dp, that allow you to specify interface sizes in points (1/72 of an inch), millimeters, or inches. However, we do not recommend using them: Not all devices are correctly configured for these units to scale correctly.

In practice and in this book, you will use dp and sp almost exclusively. Android will translate these values into pixels at runtime.

Margins vs padding

In both GeoQuiz and CriminalIntent, you have given widgets margin and padding attributes. Beginning developers sometimes get confused about these two. Now that you understand what a layout parameter is, the difference is easier to explain.

Margin attributes are layout parameters. They determine the distance between widgets. Given that a widget can only know about itself, margins must be the responsibility of the widget’s parent.

Padding, on the other hand, is not a layout parameter. The android:padding attribute tells the widget how much bigger than its contents it should draw itself. For example, say you wanted the date button to be spectacularly large without changing its text size (Figure 9.29).

Figure 9.29  I like big buttons and I cannot lie...

Screenshot shows Title screen in Android.

You could add the following attribute to the Button.

Listing 9.4  Padding in action (fragment_crime.xml)

  <Button android:id="@+id/crime_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:padding="80dp" />

Alas, you should probably remove this attribute before continuing.

Styles, themes, and theme attributes

A style is an XML resource that contains attributes that describe how a widget should look and behave. For example, the following is a style resource that configures a widget with a larger-than-normal text size:

  <style name="BigTextStyle">
    <item name="android:textSize">20sp</item>
    <item name="android:padding">3dp</item>
  </style>

You can create your own styles (and you will in Chapter 22). You add them to a styles file in res/values/ and refer to them in layouts like this: @style/my_own_style.

Take another look at the TextView widgets in fragment_crime.xml; each has a style attribute that refers to a style created by Android. This particular style makes the TextViews look like list separators and comes from the app’s theme. A theme is a collection of styles. Structurally, a theme is itself a style resource whose attributes point to other style resources.

Android provides platform themes that your apps can use. When you created CriminalIntent, the wizard set up a theme for the app that is referenced on the application tag in the manifest.

You can apply a style from the app’s theme to a widget using a theme attribute reference. This is what you are doing in fragment_crime.xml when you use the value ?android:listSeparatorTextViewStyle.

In a theme attribute reference, you tell Android’s runtime resource manager, Go to the app’s theme and find the attribute named listSeparatorTextViewStyle. This attribute points to another style resource. Put the value of that resource here.

Every Android theme will include an attribute named listSeparatorTextViewStyle, but its definition will be different depending on the overall look and feel of the particular theme. Using a theme attribute reference ensures that the TextViews will have the correct look and feel for your app.

You will learn more about how styles and themes work in Chapter 22.

Android’s design guidelines

Notice that for your margins, Android Studio defaulted to either a 16dp or a 8dp value. This value follows Android’s material design guidelines. You can find all of the Android design guidelines at developer.android.com/​design/​index.xhtml.

Your Android apps should follow these guidelines as closely as possible. However, you should know that the guidelines rely heavily on newer Android SDK functionality that is not always available or easy to achieve on older devices. Many of the design recommendations can be followed using the AppCompat library, which you have seen and will read more about in Chapter 13.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
54.163.221.133