📝
Android Programming
  • Introduction
  • Contents
  • Android
    • Q1
    • Q2
    • Q3
    • Q4
    • Q5
    • Q6
    • Q7
    • Q8
    • Q9
    • Q10
    • Q11
    • Q12
    • End
Powered by GitBook
On this page
  • Create a spinner with strings taken from resource folder (res >> value folder) and on changing the spinner value, image will change.
  • Code
  • Output
  1. Android

Q5

Question 5

PreviousQ4NextQ6

Last updated 4 years ago

Create a spinner with strings taken from resource folder (res >> value folder) and on changing the spinner value, image will change.

App Insights:

Added main activity, used drawable folder to store image files and displayed using ImageView and used Spinner for dropdown menu options

Code

<?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"
    android:background="#673AB7"
    tools:context="MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="320dp"
        android:layout_height="54dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="138dp"
        android:gravity="bottom"
        android:text="@string/select_any_country_from_list"
        android:textColor="#FFEB3B"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/imV"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/textView"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="55dp"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="74dp"
        android:background="#FFEB3B"
        android:textAlignment="center"
        android:entries="@array/List_Of_Countries" />

    <ImageView
        android:id="@+id/imV"
        android:layout_width="280dp"
        android:layout_height="180dp"
        android:layout_below="@id/spinner1"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="100dp"
        android:layout_marginEnd="60dp"
        android:layout_marginRight="60dp"
        android:background="#fb4"
        android:foregroundGravity="bottom"
        app:srcCompat="@drawable/france" />
</RelativeLayout>
<resources>
    <string name="app_name">Q5_Spinner</string>
    <string name="country">
        pick any country from list
</string>
    <string 
    name="select_any_country_from_list">
    Select Any Country from List
    </string>
    <string-array name="List_Of_Countries">
        <item>United Kingdom</item>
        <item>Germany</item>
        <item>France</item>
        <item>India</item>
    </string-array>
</resources>
package com.example.q5_spinner;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements
        AdapterView.OnItemSelectedListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner spin=(Spinner)findViewById(R.id.spinner1);
        spin.setOnItemSelectedListener(this);
    }
    public void onItemSelected(AdapterView<?> parent,
     View view, int pos, long id)
    {
        ImageView imagev=(ImageView)findViewById(R.id.imV);
        if(((TextView)view).getText().equals("United Kingdom"))
            imagev.setImageResource(R.drawable.uk);
        if(((TextView)view).getText().equals("France"))
            imagev.setImageResource(R.drawable.france);
        if(((TextView)view).getText().equals("Germany"))
            imagev.setImageResource(R.drawable.germany);
        if(((TextView)view).getText().equals("India"))
            imagev.setImageResource(R.drawable.india);

    }
    public void onNothingSelected(AdapterView<?> parent)
    {
        //another interface callback
    }
}

Output

Download to Run and Test Locally

Q5_Spinner.apk
Browse Source Code
Studio Screenshot (1/9)
App Screenshot (2/9)
Studio Screenshot Choosing Display Value (3/9)
Studio Screenshot Displaying The Chosen Resource File (4/9)
App Screenshot Displaying The Chosen Resource File (5/9)
Studio Screenshot Displaying The Chosen Resource File (6/9)
App Screenshot Displaying The Chosen Resource File (7/9)
Studio Screenshot Displaying The Chosen Resource File (8/9)
App Screenshot Displaying The Chosen Resource File (9/9)