Q5

Question 5

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

Browse Source Code

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>

Output

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)

Last updated