Displaying Text Fields with TextView and EditText
Author: Ian Darwin
Published? true --
You want to display text on the screen, either read-only or editable.
Use a TextView when you want the user to have read-only access to text; this includes what most other GUI API packages call a "Label", there being no explicit Label class in android.widget. Use an EditText when you want the user to have read-write access to text; this includes what other packages may call a TextField or a TextArea.
EditText is a direct subclass of TextView. Note that EditText has many direct and indirect subclasses, many of which are GUI controls in their own right, such as CheckBox, RadioButton, and the like. A further subclass is the AutoCompleteTextView which, as the name implies, allows for auto-completion when the user types the first few letters of some data item. As with the xref linkend="ch1873" ListView, there is an Adapter to provide the completable text items.
Placing an EditText or TextView is trivial using the XML layout. Assigning the initial values to be displayed is also simple using XML. It is possible to set the value directly using e.g.,
However it is preferable to use a value like "@+string/welcome_text" and define the string in strings.xml so it can be changed and internationalized more readily.
Since TextView and EditText are used throughout, we do not have a sample application using them. There is one provided with the Android API Examples, called LabelView, if you need it.