0% found this document useful (0 votes)
30 views4 pages

pr-7 Mad

Uploaded by

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

pr-7 Mad

Uploaded by

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

ITM(SLS) BARODA UNIVERSITY

DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY


Diploma Semester – 5(A)
MAD

Practical-7
Aim:- Create spinner with strings taken from resource folder and on
changing spinner value, change the image.
Code1: -activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
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"
android:orientation="vertical"
android:padding="16dp">

<!-- Spinner for selecting an animal -->


<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:minHeight="48dp" />

<!-- ImageView to display the animal image -->


<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="Animal Image"
tools:ignore="HardcodedText" />

</LinearLayout>

Name:- Dhairya joshi Enrollment No:-22C11027 1


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Semester – 5(A)
MAD
Code2:-MainActivity.java

package com.example.dropdown;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private Spinner spinner;


private ImageView image;

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

// Initialize the Spinner and ImageView


spinner = findViewById(R.id.spinner);
image = findViewById(R.id.image);

// Define the string array for animals


String[] animals = {"Select one", "Tiger", "Lion", "Zebra", "Elephant"};

// Create an ArrayAdapter to hold the animal names


ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_dropdown_item, animals);

// Set the adapter for the spinner


spinner.setAdapter(adapter);

// Set listener for the spinner to handle selection events


spinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int

Name:- Dhairya joshi Enrollment No:-22C11027 2


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Semester – 5(A)
MAD
position, long id) {
// Switch case to change the image based on the selected item
switch (position) {
case 1:
image.setImageResource(R.drawable.tiger); // Tiger image
break;
case 2:
image.setImageResource(R.drawable.lion); // Lion image
break;
case 3:
image.setImageResource(R.drawable.zebra); // Zebra image
break;
case 4:
image.setImageResource(R.drawable.elephant); // Elephant
image
break;
default:
image.setImageResource(0); // No image if "Select one" is
chosen
break;
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// Handle the case where no selection is made (optional)
}
});
}
}

Name:- Dhairya joshi Enrollment No:-22C11027 3


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Semester – 5(A)
MAD
Output:-

Name:- Dhairya joshi Enrollment No:-22C11027 4

You might also like