Files
freeCodeCamp/guide/english/android-development/user-interface-design/index.md
Fahed 35da096e42 Added articles: Material Design and User interface design for the English and Spanish language (#32779)
* Added article Material Design

* Spanish translation of Material Design article.

* Added article User interface design

* Spanish translation of User interface design.

* Fixed more information and courses of the article material design (guide english and spanish)

* Fixed more information and courses of the article user interface design (guide english and spanish)

* Fixed localTitle of the article material design (guide spanish)

* Fixed localTitle of the article user interface design (guide spanish)

* fix/suggested-changes

* fix/suggested-changes
2019-07-01 18:57:53 -05:00

4.7 KiB

title
title
User Interface Design

User Interface Design

The layout defines the user interface (UI) of an activity or widget of an app. You can declare the layout in 2 ways:

  1. Declare elements in the UI in XML.
  2. Create instances of design elements programmatically.

Programmers usually use default app layouts in XML, then we modify the state of the objects on the screen using the code, including those declared in XML.

The Android SDK includes many design elements, which you can configure to achieve the desired appearance and behavior. Each of these elements is an instance of the View class or one of its subclasses, for example, TextView or Button.

ViewGroup vs View

ViewGroup vs View The view is the superclass of all Android components. These are some of the components derived from View: TextView, EditText, ImageView, ProgressBar, Button, ImageButton, CheckBox, DatePicker, and others

ViewGroup is a collection of Views, similar to a container. It can contain Views and other ViewGroups. Some of these components derived from ViewGroup are the following: LinearLayout, RelativeLayout, CoordinatorLayout, ListView, GridView, ConstraintLayout, RecyclerView.

The hierarchy of views

The design elements are in a hierarchy of View objects called the view hierarchy. The hierarchy of views The root element of this hierarchical view shown in the image is a LinearLayout object. LinearLayout inherits from a subclass of View called ViewGroup. We use LinearLayout to sort the elements in a single row or column.

Attributes

Each View and ViewGroup object supports its own variety of XML attributes. Some attributes are common among all elements because they inherit from View, such as the following:

android:id="@+id/button"
  • The attribute android:id allows to have any element an ID to identify it exclusively in the hierarchy of views
android:layout_width="wrap_parent"
android:layout_height="match_parent"
  • The attributes android:layout_width and android:layout_height. Any design element uses them. Its typical values are match_parent or wrap_content.
    • match_parent: The view will be as big as your father.
    • wrap_content: The view will be as large as you need the elements that contain it.

Other attributes are specific, which vary depending on the element:

android:orientation="horizontal"
  • The android:orientation attribute present in the LinearLayout element determines whether your children appear horizontally or vertically.
android:textSize="10sp"
  • The android:textsize attribute present in Button or TextView. It tells the elements the size of the text to be used.

Character string resources

In each Android project, you have the file called string.xml, to store the character string. Recommended to put all the strings of characters in this file and then we refer to it from each particular element, this allows us to easily find its location.

Although by default the file with the character string resources is called string.xml we can call it as we want and even have several in the project, as long as we maintain its format.

From layout.xml to View

Every Activity created needs a UI to manage, to provide it we call the setContentView method of the Activity.

setContentView(R.layout.main_activity);

The setContentView method displays a layout, that is, an interface and displays it on the screen. When deploying a layout, an instance of each design element contained in the layout file is created, according to the definition of its attributes. To specify which layout is deployed, we pass to the method the resource Id of that layout.

Button button = (Button) findViewById(R.id.button);

To connect an element of the layout file to the Activity we use the findViewById method. This method accepts the resource Id of a widget as an argument and returns a View object.

More Information

Courses