0% found this document useful (0 votes)
20 views5 pages

Explicit Intent

Uploaded by

anushkasaundane5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Explicit Intent

Uploaded by

anushkasaundane5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

This example demonstrates the use of explicit intents in Android to navigate from one activity to

another while passing data between them. Here’s a detailed explanation of the entire process:

### **Main Concepts**


1. **Explicit Intents**: Used to start another activity within the same application.
2. **Passing Data**: The `Intent` object is used to send data from one activity to another.

### **Step 1: First Activity (MainActivity.java)**

#### **XML Layout (activity_main.xml)**

```xml
<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">

<EditText
android:id="@+id/editTextIndex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Write Image Index"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Image Index"
android:onClick="onIndexSendButtonClicked"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextIndex"
app:layout_constraintVertical_bias="0.318" />
</androidx.constraintlayout.widget.ConstraintLayout>
```
- **EditText** (`@+id/editTextIndex`): Allows the user to input the index of the image they want to
view in the next activity.
- **Button** (`@+id/button`): When clicked, this triggers the `onIndexSendButtonClicked`
method.

#### **Java Code (MainActivity.java)**

```java
package com.example.vahexplicitintent;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

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

public void onIndexSendButtonClicked(View v) {


Intent intent = new Intent(this, SecondActivity.class); // Create an Intent to start
SecondActivity
EditText editText = (EditText) findViewById(R.id.editTextIndex); // Get the EditText view
String index = editText.getText().toString(); // Retrieve the text entered by the user
intent.putExtra("imageIndex", index); // Put the image index as an extra in the intent
startActivity(intent); // Start SecondActivity
}
}
```
- **`onIndexSendButtonClicked(View v)`**: This method is called when the button is clicked. It
creates an `Intent` to start `SecondActivity` and passes the entered image index to it using
`putExtra`.

### **Step 2: Create Second Activity**

- **Create a new activity**: `SecondActivity.java`


- **Add images**: Copy the images into the `drawable` folder (e.g., `thalepith`, `shira1`,
`upama1`, `dhapate`).

### **Step 3: Second Activity Layout (activity_second.xml)**

```xml
<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=".SecondActivity">

<ImageView
android:id="@+id/imageViewFood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.054"
app:srcCompat="@drawable/thalepith" />
</androidx.constraintlayout.widget.ConstraintLayout>
```
- **ImageView** (`@+id/imageViewFood`): Displays the selected food image based on the index
passed from `MainActivity`.

### **Java Code (SecondActivity.java)**

```java
package com.example.vahexplicitintent;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class SecondActivity extends AppCompatActivity {

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

Bundle bundle = getIntent().getExtras(); // Retrieve the bundle of extras from the Intent
if(bundle != null){
String imgIndex = bundle.getString("imageIndex"); // Get the image index from the
bundle
setImage(imgIndex); // Call the method to set the image based on the index
}
}

public void setImage(String index){


ImageView imgView = (ImageView) findViewById(R.id.imageViewFood); // Get the
ImageView

switch (index) {
case "1":
imgView.setImageResource(R.drawable.thalepith); // Set image for index 1
break;
case "2":
imgView.setImageResource(R.drawable.shira1); // Set image for index 2
break;
case "3":
imgView.setImageResource(R.drawable.upama1); // Set image for index 3
break;
case "4":
imgView.setImageResource(R.drawable.dhapate); // Set image for index 4
break;
default:
imgView.setImageResource(R.drawable.upama1); // Default image if no valid index is
provided
}
}
}
```
- **`onCreate(Bundle savedInstanceState)`**: Retrieves the image index from the intent extras
and calls `setImage` to display the appropriate image.
- **`setImage(String index)`**: A method that sets the `ImageView` resource based on the index
value passed from `MainActivity`.

### **Summary:**
1. **MainActivity**: Takes user input for an image index and starts `SecondActivity` with that
index passed via an `Intent`.
2. **SecondActivity**: Receives the image index and displays the corresponding image in an
`ImageView` using a switch-case statement.
This is a basic demonstration of explicit intents and data passing between activities in Android,
making it easier to create interactive apps.

You might also like