Java Android App Development Course, Arranged in Hierarchical Structure – ConstraintLayout

There are several ways to structure a UI in Android app development, one of which is ConstraintLayout. ConstraintLayout is a powerful tool that helps to easily create complex UI layouts. In this tutorial, we will explain why to use ConstraintLayout, its basic concepts, and how to use it in practice through examples.

1. Introduction to ConstraintLayout

ConstraintLayout is a layout that allows for easy arrangement of various Views. In traditional layout methods, LinearLayout or RelativeLayout were used, but ConstraintLayout has structural advantages that overcome the limitations of these methods.

  • Flexibility: Automatically adjusts according to the size of the device using a percent-based layout.
  • Performance: Simplifies the View Hierarchy, reducing memory usage and improving performance.
  • Integration with design tools: The Layout Editor in Android Studio allows for easy placement of visual elements using ConstraintLayout.

2. Basic Concepts of ConstraintLayout

ConstraintLayout arranges views by defining constraints between them. You can set the position of your desired view relative to other views or the parent view. Thanks to this characteristic, developers can set views to reference each other for proper placement.

The most important concept in ConstraintLayout is Constraints. Constraints provide the necessary information to determine the position and size of a view. Generally, the following constraints can be set:

  • Top constraint: Fixes the top of the view to the top of the parent or another view.
  • Bottom constraint: Fixes the bottom of the view to the bottom of the parent or another view.
  • Start constraint: Fixes the start (left) of the view to the start of the parent or another view.
  • End constraint: Fixes the end (right) of the view to the end of the parent or another view.
  • Width and Height constraint: Sets constraints for the width and height of the view.

3. Using ConstraintLayout

Now let’s look at how to actually use ConstraintLayout through the example below. In this example, we will create a basic login screen.

3.1 Creating an XML Layout File

The following is the XML code to implement the login screen. This code will be saved as ‘activity_login.xml’ in the res/layout directory.

<?xml version="1.0" encoding="utf-8"?>
<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=".LoginActivity">

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/editTextUsername"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <EditText
        android:id="@+id/editTextUsername"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Username"
        app:layout_constraintBottom_toTopOf="@+id/editTextPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintWidth_percent="0.5"/>

    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Password"
        app:layout_constraintBottom_toTopOf="@+id/buttonLogin"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintWidth_percent="0.5"/>

    <Button
        android:id="@+id/buttonLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

3.2 Writing the MainActivity.java File

After creating the XML file, you now need to write the Activity that will use this layout. Create a ‘MainActivity.java’ file under the directory ‘src/main/java/com/example/app’.

package com.example.app;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText editTextUsername;
    private EditText editTextPassword;
    private Button buttonLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        editTextUsername = findViewById(R.id.editTextUsername);
        editTextPassword = findViewById(R.id.editTextPassword);
        buttonLogin = findViewById(R.id.buttonLogin);

        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = editTextUsername.getText().toString();
                String password = editTextPassword.getText().toString();

                if (username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(MainActivity.this, "Please fill in all fields.", Toast.LENGTH_SHORT).show();
                } else {
                    // Logic to handle login
                    Toast.makeText(MainActivity.this, "Login successful!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

4. Advanced Features of ConstraintLayout

In addition to basic constraints, ConstraintLayout supports various advanced features. Here we will introduce a few key features.

4.1 Guideline

A guideline provides a visual aid that helps align views. You can create horizontal or vertical guidelines to assist with view placement and size adjustments.

<androidx.constraintlayout.widget.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:guidePercent="0.5"/>

4.2 Barrier

A barrier allows views to be dependent on each other, dynamically adjusting the position of other views based on the size of the primary view. For example, if one view is hidden, another view can take its place.

4.3 Group

Using a group allows you to bundle multiple views together to set constraints or visibility collectively.

<androidx.constraintlayout.widget.Group
    android:id="@+id/group"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"/>

5. Conclusion

ConstraintLayout is a very useful tool in Android UI development, allowing you to easily set relationships between views. In this tutorial, we explored the basic concepts and usage of ConstraintLayout, as well as advanced features for creating complex layouts. When developing actual apps, utilize this information to implement efficient and responsive UIs.

Now you are ready to create your own unique UI using ConstraintLayout. Enjoy the pleasure of combining various elements to create amazing apps!