Java Android App Development Course, Creating Screens using Jetpack

Android app development is receiving more attention than ever. In particular, Google’s Jetpack library is a powerful tool that makes the development of Android applications easier and more efficient. In this course, we will take a closer look at how to create screens based on Jetpack using Java.

1. Understanding Jetpack

Jetpack is a set of numerous libraries and architecture components for Android development that helps make app development, testing, and maintenance easier and faster. Jetpack is composed of the following key components.

  • Architecture Components: UI-related libraries such as Lifecycle, LiveData, and ViewModel
  • UI Components: Modern UI tools like Jetpack Compose
  • Data Management: Data storage solutions like Room and DataStore
  • Behavior: Manages app behavior with Navigation and WorkManager

2. Setting Up the Environment

Let’s learn how to set up a project using Jetpack with Android Studio. Please follow the steps below.

  1. Launch Android Studio and create a new project. Select “Empty Activity” and enter the project name.
  2. Gradle File (Build.gradle) Add Jetpack library dependencies. The necessary libraries are as follows:

    dependencies {
        implementation 'androidx.appcompat:appcompat:1.3.0'
        implementation 'androidx.activity:activity-ktx:1.2.3'
        implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
        implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
    }
  3. Sync the Gradle file.

3. Creating the Basic Screen

Now let’s write the XML layout and Java code to create the basic screen.

3.1 Creating the XML Layout File

Open the project’s res/layout/activity_main.xml file and modify it as follows.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, Jetpack!"
        android:textSize="24sp"
        android:layout_centerInParent="true" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:layout_below="@id/text_view"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" />

</RelativeLayout>

3.2 Writing MainActivity.java

Open the MainActivity.java file and write the code as follows. This code implements the functionality to change the text upon button click.

package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private Button button;

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

        textView = findViewById(R.id.text_view);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("Button Clicked!");
            }
        });
    }
}

4. Utilizing ViewModel and LiveData

Using Jetpack’s ViewModel and LiveData allows for efficient management of UI data. ViewModel retains UI-related data, and LiveData automatically updates the UI upon data changes.

4.1 Creating the ViewModel Class

Create a new class to implement ViewModel. Create a MyViewModel.java file and enter the following code.

package com.example.myapplication;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class MyViewModel extends ViewModel {
    private final MutableLiveData text = new MutableLiveData<>();

    public MyViewModel() {
        text.setValue("Hello, Jetpack with ViewModel!");
    }

    public LiveData getText() {
        return text;
    }

    public void updateText(String newText) {
        text.setValue(newText);
    }
}

4.2 Using ViewModel in MainActivity

Now let’s use the ViewModel in MainActivity to update the text. Modify the code as follows.

package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

public class MainActivity extends AppCompatActivity {
    private MyViewModel myViewModel;

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

        myViewModel = new ViewModelProvider(this).get(MyViewModel.class);
        TextView textView = findViewById(R.id.text_view);
        Button button = findViewById(R.id.button);

        myViewModel.getText().observe(this, new Observer() {
            @Override
            public void onChanged(String s) {
                textView.setText(s);
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myViewModel.updateText("Button Clicked!");
            }
        });
    }
}

5. Screen Transitions via Navigation

The Jetpack Navigation component allows for easy transitions between various screens of the app. Let’s learn how to switch screens using Navigation components.

5.1 Creating a Navigation Graph

Create a new Navigation graph file. Create a res/navigation/nav_graph.xml file and set it up as follows.

<?xml version="1.0" encoding="utf-8"?>
<navigation 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"
    app:startDestination="@id/firstFragment">

    <fragment
        android:id="@+id/firstFragment"
        android:name="com.example.myapplication.FirstFragment"
        android:label="First Fragment"
        tools:layout="@layout/fragment_first">
    </fragment>

    <fragment
        android:id="@+id/secondFragment"
        android:name="com.example.myapplication.SecondFragment"
        android:label="Second Fragment"
        tools:layout="@layout/fragment_second">
    </fragment>

</navigation>

5.2 Creating Fragment Classes

To implement navigation, we will add two Fragment classes. First, create FirstFragment.java.

package com.example.myapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class FirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_first, container, false);
    }
}

Next, create the SecondFragment.java file.

package com.example.myapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class SecondFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_second, container, false);
    }
}

6. Conclusion

Developing Android apps utilizing Jetpack allows for efficient use of various features such as UI components, data management, and fragment navigation. In this course, we configured a basic screen using Java and implemented state management using ViewModel and LiveData. We also learned how to facilitate screen transitions easily using navigation components.

To develop more complex apps, you can leverage additional components and libraries, and enhance code reusability and maintainability through the various features of Jetpack. Keep practicing and try applying it to actual projects to gain more experience.

References