Create spinner with strings taken from resource folder (res >> value folder). On changing spinner value, change background of screen.

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"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:entries="@array/S"
        />
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginTop="32dp"
        android:layout_toRightOf="@+id/imageView1" />
</RelativeLayout>

MainActivity.java

package com.example.prac12;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;


public class MainActivity extends Activity implements OnItemSelectedListener {

            Integer[] aIDS={
                                    R.drawable.one,
                                    R.drawable.two,
                                    R.drawable.three,
                                    R.drawable.four};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        Spinner sp = (Spinner) findViewById(R.id.spinner1);
        sp.setOnItemSelectedListener(this);
    }

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

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                        Toast.makeText(this,"SELECTED ITEM IS : "+arg0.getSelectedItem().toString(),Toast.LENGTH_SHORT).show();
                        ImageView ie = (ImageView) findViewById(R.id.imageView1);
                        ie.setImageResource(aIDS[arg2]);
            }


}



Post a Comment

0 Comments