What is Activity_main XML in Android

Back to the main Android page

The design of a display is implemented by two things:

  • The activity triggers the display of the elements and reacts to the user events. In principle, the activity could also design the display in Java. Usually this is not done, however.
  • Instead, the activity unfolds by calling the method setContentView an XML file in the directory res / layout is defined. How a screen is designed with it is the subject of this website.
@Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_xxx); The xxx in the listing stands for the display of the activity. Replace xxx with main on the main display.

A fragment also uses an XML file for design. The fragment calls in its handler method onCreateView The method inflate on.

After creating a project, Android Studio creates the program framework for the main activity and the file at the same time res / layout / activity_main.xml.

The editor for the layout

If you click on this file, it is displayed in three ways, which can be toggled using the symbol buttons at the top right:
  • A picture (rectangle with mountains) is the design mode. The picture is divided into
    • a palette on the left: it contains many, many control elements (views) that can be dragged onto the display.
    • the display view in the middle: Here you can collect elements and combine them with one another.
    • The attribute settings on the right: The control elements can be labeled or colored or are given identifiers via which they can be accessed by other elements but also by activity.
  • A symbol made up of lines and rectangles stands for the split mode. This allows you to see the XML code and the effects on the display side by side.
  • The symbol, in which there are several lines below each other, stands for the XML code editor. Sometimes it is easier, sometimes inevitable, to edit the generated code.

Arrangement in a ViewGroup

An XML file combines one or more control elements that are saved as View are designated. The framework forms a ViewGroupwhich also determines how the children are arranged.
  • LinearLayout: All children are arranged next to or on top of each other.
  • RelativeLayout: The children can indicate how they want to be positioned next to or below the other children.
  • ConstraintLayout: In the current versions, ConstraintLayout is used as the standard. Although this is more complex than the others, it requires less nesting of ViewGroups.
When creating a project, Android Studio creates an XML for the main activity that contains a TextView control element in a ViewGroup with ConstraintLayout and looks something like this: < androidx.constraintlayout.widget.ConstraintLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" xmlns : tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". MainActivity"> expansionandroid: layout_width and android: layout_height indicate the width and height of the element.
  • The ViewGroup uses the entire space (match_parent) of the parent element. In this case it is the frame of the display.
  • The TextView, on the other hand, only encloses the content (wrap_content) and leaves the rest of the space to other views that need it.

distances

The views can create space in two ways:
  • margin is the distance to the outside. The view keeps a distance to the edge or to the neighbor: andoid: layout_marginLeft, andoid: layout_marginRight, andoid: layout_marginTop and andoid: layout_marginBottom.
  • padding is the distance to the inside, i.e. to its content: andoid: paddingLeft, andoid: paddingRight, andoid: paddingTop and andoid: paddingBottom.

units

Common units for example for distances are

  • dp (Density-independent pixel): This size ensures that the expansion is roughly the same on every device, even if the resolution is higher.
  • sp (Depending on text): On very small screens, an edge of one centimeter is a bit unfortunate. That is why sp is preferred in such cases, as the font is smaller on smaller devices.
Both units mean that applications on different devices do not cause problems, but look about the same.

The units mm, in, pt (point - a unit from the pressure range) are also possible. However, these are not independent of the device hardware and should therefore be avoided.

ID

Each element of a layout can be identified by an ID. This is done by adding an android: id. android: id = "@ + id / meinElement" This ID is important if you want to read out or set the control element from the Java program, but also if you want to define the relationship between elements within the layout: app: layout_constraintStart_toEndOf = "@ id / myElement "

LinearLayout

In a linear layout, the elements are always arranged one below the other or next to one another. The direction is determined by the Orientation attribute. Its value can or be. ... All child elements must define android: layout_width and android: layout_height: The LinearLayout is often used because of its easy handling to be nested with other layouts. A high level of nesting makes rebuilding the screen time-consuming. In this respect, it can be worthwhile to use more complex layouts for performance reasons.

RelativeLayout

In the RelativeLayout, the elements are created in relation to other elements, e.g. under a button or to the left of a TextView. ... ... The following relationships can be specified in the elements.
android: layout_above The element is on top of the other element.
android: layout_below The element lies below the other element.
android: layout_toLeftOf The element is to the left of the other element.
android: layout_toRightOf The element is to the right of the other element.
android: layout_toStartOf The element is based on the beginning of the other element. So the elements start in the same vertical position.
android: layout_toEndOf The element aligns with the end of the other element.

ConstraintLayout

The ConstraintLayout organizes its elements through constraints that describe the relationship between elements. In this way it is even more flexible than the RelativeLayout. ... The elements can have relationships between its edges Start or End or Top Define edges (bottom to start or end or top) of other elements, including that of the parent element.
app: layout_constraintStart_toStartOf = "parent"
app: layout_constraintStart_toEndOf = "@ id / cancel_button"
app: layout_constraintTop_toTopOf = "parent"
app: layout_constraintTop_toBottomOf = "@ id / recyclerView"
app: layout_constraintEnd_toEndOf = "parent"
app: layout_constraintBottom_toTopOf = "@ id / ok_button"
app: layout_constraintBottom_toBottomOf = "parent"

Chains

If several elements are to be evenly distributed in a row, they can be marked and then the Chains | Horizontal Chain item can be selected with the right mouse button.

Nesting layouts

Layouts can be nested. One or more other layouts can be located within the RelativeLayout, such as a linear layout here. ... The control elements are located within the LinearLayout, such as a button here.

Align buttons next to each other in the middle

A horizontal linear layout is created so that horizontal buttons use the space (halfway) evenly. A weight is used to determine which button must be wider than the others. If the weights are all the same, they align themselves evenly. ... What if the relativity theory failed?
  • How you made notes for UPSC CSE
  • Where are robots often used?
  • Are SRMU placements good
  • Is there a compulsory attendance at the DTU
  • How helpful are TED talks
  • Naked embarrassing moments