Controls in ApiDemos

Source Code

  1. src/com.example.pis.android.view/Controls1.java loads the layout file res/layout/controls_1.xml.

  2. src/com.example.pis.android.view/Controls2.java. This class exists only because each menu item of ApiDemos has to lead to a different class.

  3. res/layout/controls_1.xml. The first EditText hogs all the width and pushes the second one off the screen.

  4. res/values/strings.xml

  5. AndroidManifest.xml. The activity element that has the attribute android:name=".view.Controls2" also has the attribute android:theme="@android:style/Theme".

TextView Attributes in controls_1.xml

For the question mark, see Referencing style attributes. The “currently-applied theme” is set in AndroidManifest.xml.

  1. android:textAppearance="?android:attr/textAppearanceLarge"
  2. android:textColor="?android:attr/textColorSecondary"
  3. android:textColor="?android:attr/textColorTertiary"

The theme Theme is defined in android-sdks/platforms/android-14/data/res/values/themes.xml. This file contains

<resources>
	<style name="Theme">

		<!-- etc. -->

		<item name="textColorSecondary">@android:color/secondary_text_dark</item>

		<!-- etc. -->

	</style>
</resources>

Spinner

Most of the controls can be set up in the .xml file, but the spinner requires some Java code as well. A Spinner is a pop-up menu that displays a series of lines. Each line counts as a little view in its own right, so the spinner is a view that contains a group of views. Class Spinner is derived from class ViewGroup, which is derived from class View. See Summary of Important View Groups.

The spinner gets the little views from an Adapter that is plugged into the spinner. The spinner is therefore also an AdapterView. See Binding to Data with AdapterView.

Now where does the adapter get the little views from? There are several types of adapter. Ours is an ArrayAdapter, which gets the string in each little view from an array of strings that is plugged into the array adapter. Our array of strings is the field mStrings in class Controls1. Our array adapter creates each little view by plugging a string into a predefined layout named android.R.layout.simple_spinner_dropdown_item. For the difference between the two layouts, see paragraph 4.

Class ArrayAdapter is a Java generic because it has <angle brackets>. In C++, we would have called it a template.

The adapter in the spinner tutorial gets the list of strings from a string-array element in the strings.xml file.

C:\Users\myname\android-sdks\platforms\android-14\data\res\layout\simple_spinner_item.xml

For the question mark, see Referencing Style Attributes and Applying Styles and Themes.

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@android:id/text1"
	style="?android:attr/spinnerItemStyle"
	android:singleLine="true"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:ellipsize="marquee"
/>

spinnerItemStyle is defined in C:\Users\myname\android-sdks\platforms\android-14\data\res\values\themes.xml:

	<item name="spinnerItemStyle">@android:style/Widget.TextView.SpinnerItem</item>

Widget.TextView.SpinerItem is defined in C:\Users\myname\android-sdks\platforms\android-14\data\res\values\styles.xml

	<style name="Widget.TextView.SpinnerItem">
		<item name="android:textAppearance">@style/TextAppearance.Widget.TextView.SpinnerItem</item>
	</style>

TextApearance.Widget.TextView.SpinerItem is defined in C:\Users\myname\android-sdks\platforms\android-14\data\res\values\styles.xml

	<style name="TextAppearance.Widget.TextView.SpinnerItem">
		<item name="android:textColor">@android:color/primary_text_light_disable_only</item>
	</style>

primary_text_light_disable_only is defined in…

C:\Users\myname\android-sdks\platforms\android-14/data\res\layout\simple_spinner_dropdown_item.xml

<?xml version="1.0" encoding="utf-8"?>

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@android:id/text1"
	style="?android:attr/spinnerDropDownItemStyle"
	android:singleLine="true"
	android:layout_width="match_parent"
	android:layout_height="?android:attr/dropdownListPreferredItemHeight"
	android:ellipsize="marquee"
/>