Android Development with Java: Activities and Fragments
Android app development with Java is built around key components known as activities and fragments. Activities represent screens or user interfaces, while fragments are smaller reusable components within those screens. In this article, we’ll explore how activities and fragments work in Android development with Java.
Activities: The Building Blocks of Android Apps
An activity is a fundamental building block of any Android app. It represents a single screen with a user interface that users can interact with. Activities are Java classes that extend the “AppCompatActivity” class and are associated with a layout that defines the user interface.
Creating an Activity
To create an activity in Android Studio, follow these steps:
- Open Android Studio and your Android project.
- Right-click on the “java” directory within the “app” module and choose “New” > “Java Class.”
- Enter a name for your activity, and make sure it extends “AppCompatActivity.”
- Android Studio will generate a Java class for your activity with a “onCreate” method, which is called when the activity is created.
Example: Creating a Simple Activity
Here’s an example of a simple Java activity class for Android:
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
In this example, the “MainActivity” class extends “AppCompatActivity,” and the “onCreate” method sets the activity’s content view to a layout defined in an XML file (activity_main.xml).
Fragments: Reusable UI Components
Fragments are like sub-activities that can be combined to create a complete user interface. They allow you to build modular and flexible UIs. Fragments are also Java classes and can be associated with their own layouts.
Creating a Fragment
To create a fragment in Android Studio, you can follow similar steps as creating an activity:
- Right-click on the “java” directory within the “app” module and choose “New” > “Java Class.”
- Enter a name for your fragment, and make sure it extends the “Fragment” class.
- Android Studio will generate a Java class for your fragment with an “onCreateView” method, where you can inflate the fragment’s layout.
Example: Creating a Simple Fragment
Here’s an example of a simple Java fragment class for Android:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my, container, false);
}
}
In this example, the “MyFragment” class extends “Fragment,” and the “onCreateView” method inflates the fragment’s layout from an XML file (fragment_my.xml).
Combining Activities and Fragments
Android apps typically use a combination of activities and fragments to create a flexible and responsive user interface. Activities can host multiple fragments, allowing you to reuse UI components and adapt to different screen sizes and orientations.
Using Fragments in Activities
To use a fragment within an activity, you need to add it to the activity’s layout using a element or programmatically within the activity’s Java code.
Example: Adding a Fragment to an Activity’s Layout
Here’s an example of adding a fragment to an activity’s layout using the element in an XML file (activity_with_fragment.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<fragment
android:id="@+id/myFragment"
android:name="com.example.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
In this example, the element references the “MyFragment” class by name.
Conclusion
Activities and fragments are essential components in Android development with Java. They allow you to create versatile and modular user interfaces, making your Android apps more adaptable and user-friendly.