Explicit Intent
Explicit Intent
another while passing data between them. Here’s a detailed explanation of the entire process:
```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
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
```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
package com.example.vahexplicitintent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
@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
}
}
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.