🚩
Cyber Explained
  • WHOAMI
  • Technologies
    • Docker
      • Setup Docker
      • Terminology
      • Docker Hub
      • Docker Images
      • Docker Containers
      • Working with Containers
      • Virtualization vs Containerization
      • Nutshell
      • Troubleshoot
    • Android Application
      • Application File Structure
      • Layout and Resources for UI
      • Activities
      • Intents
      • Activity lifecycle and state
      • Implicit intents
    • Active Directory
      • Attacking Active Directory: 0 to 0.9
      • Resources
    • Kerberos
  • RED TEAMING
    • Attacking Kerberos
      • User Enum and Brute Force
      • AS-REP Roasting
      • Kerberoasting
    • MITRE ATT&CK
    • Resources
  • PenTesting
    • Android Pentesting
      • Re-Build App
      • Using Frida on Android without root
    • Web Pentesting
      • XSS
      • SQLi
      • Authentication Vulnerabilities
      • Session Security
      • CSRF
      • Path Traversal
      • File Inclusion
      • Business Logic Vulnerabilities
      • HTTP Host header attacks
      • SSRF
      • HTTP Request Smuggling
      • OS command injection
      • Access control vulnerabilities
    • OWASP Testing Guide
      • 1.0 Information Gathering
      • 2.0 Configuration and Deployment Management Testing
      • 3.0 Identity Management Testing
      • 4.0 Authentication Testing
      • 5.0 Authorization Testing
      • 6.0 Session Management Testing
      • 7.0 Input Validation Testing
      • 8.0 Testing for Error Handling
      • 9.0 Testing for Weak Cryptography
      • 10.0 Business Logic Testing
      • 11.0 Client-side Testing
      • 12.0 API Testing
  • Programming
    • Python
      • Hello World !
        • Variables and Data Types
        • Lists, Tuple, Sets and Dictionaries
        • If Statement
        • While Loops
        • For Loops
        • Functions
        • Classes and Objects
        • Creating Modules
        • Creating Packages
        • Exception Handling
      • System Pogramming
        • File Handling
        • OS Interaction with OS Library
        • Multithreading
        • Signals
        • Subprocess
        • Code Examples
      • Network Programming
        • Socket Programming
        • Packet Injection with Raw Sockets
        • SocketServer Framework
        • Packet Sniffing with Scapy
        • Creating a Web Server
        • Packet Injection with Scapy
        • Packet Sniffing with Raw Sockets
        • Programming with Scapy
  • Operating Systems
    • Windows*
    • Linux
      • System Structure
      • VI Text Editor
      • Working with the Linux Shell
      • Managing Users and Groups
      • Managing Files and Directories
  • Networks
    • Page 1
Powered by GitBook
On this page
  • Views
  • ViewGroup groups
  • Layout ViewGroup groups
  • Resource files
  • Values resource files
  • Strings
  1. Technologies
  2. Android Application

Layout and Resources for UI

PreviousApplication File StructureNextActivities

Last updated 3 years ago

Views

The UI consists of a hierarchy of objects called views — every element of the screen is a . The View class represents the basic building block for all UI components, and the base class for classes that provide interactive UI components such as buttons, checkboxes, and text entry fields.

A View has a location, expressed as a pair of left and top coordinates, and two dimensions, expressed as a width and a height. The unit for location and dimensions is the density-independent pixel (dp).

The Android system provides hundreds of predefined View subclasses. Commonly used View subclasses described over several lessons include:

  • for displaying text

  • to enable the user to enter and edit text

  • and other clickable elements (such as , , and ) to provide interactive behavior

  • and to display scrollable items

  • for displaying images

  • and for containing other views and positioning them

You can define a View to appear on the screen and respond to a user tap. A View can also be defined to accept text input, or to be invisible until needed.

You can specify View elements in layout resource files. Layout resources are written in XML and listed within the layout folder in the res folder in the Project > Android pane.

ViewGroup groups

View elements can be grouped inside a , which acts as a container. The relationship is parent-child, in which the parent is a ViewGroup, and the child is a View or another ViewGroup. The following are commonly used ViewGroup groups:

  • : A group that places UI elements (child View elements) using constraint connections to other elements and to the layout edges (parent View).

  • : A group that contains one other child View element and enables scrolling the child View element.

  • : A group that contains a list of other View elements or ViewGroup groups and enables scrolling them by adding and removing View elements dynamically from the screen.

Layout ViewGroup groups

In the figure above:

  1. The root ViewGroup.

  2. The first set of child View elements and ViewGroup groups whose parent is the root.

Some ViewGroup groups are designated as layouts because they organize child View elements in a specific way and are typically used as the root ViewGroup. Some examples of layouts are:

A simple example of a LinearLayout with child View elements is shown below as a diagram of the layout file (activity_main.xml), along with a hierarchy diagram (top right) and a screenshot of the actual finished layout (bottom right).

In the figure above:

  1. LinearLayout, the root ViewGroup, contains all the child View elements in a vertical orientation.

  2. Button (button_toast). The first child View element appears at the top in the LinearLayout.

  3. TextView (show_count). The second child View element appears under the first child View element in the LinearLayout.

  4. Button (button_count). The third child View element appears under the second child View element in the LinearLayout.

The layout hierarchy can grow to be complex for an app that shows many View elements on a screen. It's important to understand the hierarchy, as it affects whether View elements are visible and how efficiently they are drawn.

Resource files

Resource files are a way of separating static values from code so that you don't have to change the code itself to change the values. You can store all the strings, layouts, dimensions, colors, styles, and menu text separately in resource files.

Resource files are stored in folders located in the res folder when viewing the Project > Android pane. These folders include:

  • drawable: For images and icons

  • layout: For layout resource files

  • menu: For menu items

  • mipmap: For pre-calculated, optimized collections of app icons used by the Launcher

  • values: For colors, dimensions, strings, and styles (theme attributes)

The syntax to reference a resource in an XML layout is as follows:

@package_name:resource_type/resource_name

  • package_name is the name of the package in which the resource is located. The package name is not required when you reference resources that are stored in the res folder of your project, because these resources are from the same package.

  • resource_name is either the resource filename without the extension, or the android:name attribute value in the XML element.

For example, the following XML layout statement sets the android:text attribute to a string resource:

android:text="@string/button_label_toast"
  • No package_name is included, because the resource is stored in the strings.xml file in the project.

  • The resource_type is string.

  • The resource_name is button_label_toast.

Another example: this XML layout statement sets the android:background attribute to a color resource, and since the resource is defined in the project (in the colors.xml file), the package_name is not specified:

android:background="@color/colorPrimary"

In the following example, the XML layout statement sets the android:textColor attribute to a color resource. However, the resource is not defined in the project but supplied by Android, so you need to specify the package_name, which is android, followed by a colon:

android:textColor="@android:color/white"

Values resource files

Keeping values such as strings and colors in separate resource files makes it easier to manage them, especially if you use them more than once in your layouts.

For example, it is essential to keep strings in a separate resource file for translating and localizing your app, so that you can create a string resource file for each language without changing your code. Resource files for images, colors, dimensions, and other attributes are handy for developing an app for different device screen sizes and orientations.

Strings

String resources are located in the strings.xml file (inside res > values in the Project > Android pane). You can edit this file directly by opening it in the editor pane:

<resources>
    <string name="app_name">Hello Toast</string>
    <string name="button_label_count">Count</string>
    <string name="button_label_toast">Toast</string>
    <string name="count_initial_value">0</string>
</resources>

The name (for example, button_label_count) is the resource name you use in your XML code, as in the following attribute:

android:text="@string/button_label_count"

The string value of this name is the word (Count) enclosed within the <string></string> tags. (You don't use quotation marks unless the quotation marks are part of the string value.)

Extracting strings to resources

To extract a hard-coded string in an XML layout, follow these steps, as shown in the figure above:

  1. Click the hard-coded string and press Alt-Enter in Windows, or Option-Return in Mac OS X.

  2. Select Extract string resource.

  3. Edit the Resource name for the string value.

You can then use the resource name in your XML code. Use the expression "@string/resource_name" (including quotation marks) to refer to the string resource:

android:text="@string/button_label_count"

The View elements for a screen are organized in a hierarchy. At the root of this hierarchy is a that contains the layout of the entire screen. The ViewGroup can contain child View elements or other ViewGroup groups as shown in the following figure.

: A group of child View elements using constraints, edges, and guidelines to control how the elements are positioned relative to other elements in the layout. ConstraintLayout was designed to make it easy to click and drag View elements in the layout editor.

: A group of child View elements positioned and aligned horizontally or vertically.

: A group of child View elements in which each element is positioned and aligned relative to other elements within the ViewGroup. In other words, the positions of the child View elements can be described in relation to each other or to the parent ViewGroup.

: A group of child View elements arranged into rows and columns.

: A group of child View elements in a stack. FrameLayout is designed to block out an area on the screen to display one View. Child View elements are drawn in a stack, with the most recently added child on top. The size of the FrameLayout is the size of its largest child View element.

: A group that places its child View elements in a rectangular grid that can be scrolled.

Tip: Learn more about different layout types in .

resource_type is the R subclass for the resource type. See for more about the resource types and how to reference them.

Tip: For more about accessing resources from code, see . For Android color constants, see the .

You should also extract hard-coded strings in an XML layout file to string resources.

Naming the string resource
View
TextView
EditText
Button
RadioButton
CheckBox
Spinner
ScrollView
RecyclerView
ImageView
ConstraintLayout
LinearLayout
ViewGroup
ConstraintLayout
ScrollView
RecyclerView
ViewGroup
ConstraintLayout
LinearLayout
RelativeLayout
TableLayout
FrameLayout
GridLayout
Common Layout Objects
Resource Types
Accessing Resources
Android standard R.color resources
Extracting a string resource