Activities and Intents
Activities are the building blocks of Android apps. Each screen in an app is typically implemented as an activity, which is a Java class that extends the Activity or AppCompatActivity class.
Intents are messages that allow different components of an app to communicate with each other. They can be used to start activities, pass data between activities, and trigger other app functions.
Here is an example of how to work with activities and intents:
- Open Android Studio and create a new project. Choose the "Empty Activity" template.
- Open the activity_main.xml layout file, and replace the default code with the following code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Second Activity"
android:layout_centerInParent="true" />
</RelativeLayout>
This code defines a RelativeLayout that contains a Button. When the button is clicked, it will start a new activity.
- Create a new Java class called "SecondActivity". Add the following code to the class:
package com.example.myfirstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.textView);
textView.setText("This is the second activity");
}
}
This code defines the SecondActivity class, which extends the AppCompatActivity class. The onCreate() method sets the layout for the activity, and finds the TextView element by its ID. It then sets the text of the TextView to a message.
- Create a new layout file called "activity_second.xml". Add the following code to the file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_centerInParent="true" />
</RelativeLayout>
This code defines a RelativeLayout that contains a TextView.
- In the MainActivity.java file, add the following code to start the SecondActivity when the button is clicked:
package com.example.myfirstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
This code finds the Button element by its ID, and sets an OnClickListener that creates a new Intent object. The Intent specifies the context (MainActivity) and the class of the activity to start (SecondActivity), and starts the activity by calling the startActivity() method.
- Run the app on an emulator or a physical device, and click the "Go to Second Activity" button. The SecondActivity should be displayed, showing the message "This is the second activity".
This example demonstrates how to use activities and intents to create a simple app with two screens. The MainActivity contains a button that starts the SecondActivity when clicked. The SecondActivity displays a message on the screen.
Using activities and intents, you can create more complex apps with multiple screens and functionalities. By passing data between activities, you can create apps that are more interactive and personalized.
Leave a Comment