Create spinner with strings taken from resource folder and on changing the spinner value, image will change.

/*Create spinner with strings taken from resource folder and on changing the spinner value, image will change.*/

File Name: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.progfive" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:largeHeap="true"
        android:hardwareAccelerated="false">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>                

File Name: activity_main.xml

<RelativeLayout
    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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sp"
        android:background="#F0F0"
        />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/background"
        android:scaleType="fitXY"
        android:layout_below="@+id/sp"
        />

</RelativeLayout>    

File Name: MainActivity.java

package com.ducskecode.lifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity implements OnItemSelectedListener {

    Spinner spin;
    ImageView layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spin=(Spinner)findViewById(R.id.sp);
        String[] objects = { "Image One", "Image Two", "Image Three" };
        ArrayAdapter adapter = new ArrayAdapter(
                getApplicationContext(),android.R.layout.simple_list_item_1 ,objects);

        spin.setAdapter(adapter);
        spin.setOnItemSelectedListener(this);

            layout = (ImageView) findViewById(R.id.background);

    }

    public void onItemSelected(AdapterView parent, View view, int pos, long id) {
        Toast.makeText(getApplicationContext(),spin.getItemAtPosition(pos).toString(),                          Toast.LENGTH_LONG).show();
        int position= spin.getSelectedItemPosition();
        switch(position){
            case 0:
                layout.setImageResource(R.drawable.photo);
                break;

            case 1:
                layout.setImageResource(R.drawable.img1);
                break;

            case 2:
                layout.setImageResource(R.drawable.img2);
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView arg0) {

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}        

output: 





No comments:

Post a Comment

Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...