Common Views & Widgets
Common Views & Widgets
Every visible element on an Android screen is a subclass of android.view.View. Android ships dozens of ready-made View subclasses — called widgets — that cover the vast majority of UI needs. In this lesson you will work closely with the four most fundamental ones: TextView, Button, EditText, and ImageView. You will learn how each one is declared in XML, how it is wired up in Java, and the key attributes and API calls that every Android developer reaches for daily.
Declaring Views in XML vs Instantiating in Java
Android strongly encourages declaring the UI in XML layout files (inside res/layout/) and then referencing those views in Java via findViewById(). This separation keeps design concerns out of business logic and lets layout editors and accessibility tools introspect the UI independently of the running code.
Every XML view element must provide android:layout_width and android:layout_height. The two most common values are wrap_content (size to the content) and match_parent (fill the available space).
TextView — Displaying Text
TextView is the most-used widget in Android. It renders a string of text and supports rich formatting, clickable links, compound drawables, and HTML-tagged spans.
Key attributes: android:text sets the static label (prefer string resources: @string/greeting). android:textSize must use sp units so it respects the user's system font-size preference. android:textColor accepts hex colours or colour resources.
In Java, you obtain a reference and manipulate it at runtime:
sp for text sizes, dp for everything else. sp (scale-independent pixels) scales with the user's accessibility font settings. Hard-coding px or dp for text will make your app inaccessible to users who need larger type.
Button — Handling Taps
Button extends TextView, so every text attribute applies. Its primary job is to fire an action when the user taps it. The preferred way to listen for taps is to attach an OnClickListener in code — do not use the deprecated android:onClick XML attribute in new projects.
The lambda form is more concise once you are comfortable with anonymous classes:
btnSave.setEnabled(false) before starting a network or database operation and btnSave.setEnabled(true) when it completes. This prevents duplicate submissions — one of the most common bugs in Android forms.
EditText — Accepting User Input
EditText is a TextView with editing enabled. It is the primary way users type into your app. The android:inputType attribute is critical: it tells the soft keyboard what kind of input to present and enables automatic validation hints.
Common inputType values: text, textPassword, textEmailAddress, phone, number, numberDecimal, textMultiLine. Each changes the keyboard layout shown to the user.
Use setError() to show an inline validation message directly below the field — Android draws a red icon and a popup tooltip automatically. Pair it with requestFocus() to scroll the user to the problem field.
EditText content inside onResume() or a timer. Only read it when the user triggers an action (button tap, IME action). Reading it continuously defeats the purpose of user-initiated input and wastes CPU.
ImageView — Displaying Images
ImageView renders a drawable resource, a bitmap loaded at runtime, or a remote image (via a library like Glide or Picasso). The two most important attributes are android:src (the image source) and android:scaleType (how the image is scaled/cropped inside the view bounds).
Common scaleType values:
centerCrop— scale uniformly until both dimensions fill the view; crop the excess. Best for profile pictures.fitCenter— scale uniformly until the whole image fits inside the view with letterboxing. Good for logos and icons.centerInside— likefitCenterbut never enlarges an image smaller than the view.fitXY— stretch to fill exactly, ignoring aspect ratio. Almost always looks wrong.
Changing the image at runtime:
android:contentDescription on every ImageView. Screen readers (TalkBack) announce this string to visually impaired users. If the image is purely decorative, set it to an empty string ("") so TalkBack skips it. Omitting it generates a lint warning and fails accessibility audits.
Wiring Everything Together — A Realistic Example
Here is a compact registration form that demonstrates all four widgets working together:
Summary
TextView displays text; control its size with sp and its style with the many text attributes. Button fires an OnClickListener — attach it in Java, not in XML. EditText collects typed input; always specify inputType and validate with setError(). ImageView renders images; pick the right scaleType and always supply a contentDescription. In the next lesson you will handle user interaction events in depth.