main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<Spinner android:layout_width="match_parent" android:id="@+id/spinner1"
android:layout_height="wrap_content" android:entries="@array/img"></Spinner>
<ImageView android:src="@drawable/icon" android:id="@+id/imageView1"
android:layout_height="wrap_content" android:layout_weight="0.69"
android:layout_width="match_parent"></ImageView>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Tut_2_1Activity!</string>
<string name="app_name">Tut_2_1</string>
<string-array name="img">
<item>Image 1</item>
<item>Image 2</item>
<item>Image 3</item>
</string-array>
</resources>
Tut_2_1Activity.java
package com.tut2_1;
import android.app.Activity;
import android.os.Bundle;
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 Tut_2_1Activity extends Activity implements
OnItemSelectedListener {
/** Called when the activity is first created. */
Spinner sp;
ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sp=(Spinner)findViewById(R.id.spinner1);
sp.setOnItemSelectedListener(this);
img=(ImageView)findViewById(R.id.imageView1);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2,
long arg3) {
if(arg2==0)
{
img.setImageResource(R.drawable.img1);
}
if(arg2==1)
{
img.setImageResource(R.drawable.img2);
}
if(arg2==2)
{
img.setImageResource(R.drawable.img3);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
Output
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<Spinner android:layout_width="match_parent" android:id="@+id/spinner1"
android:layout_height="wrap_content" android:entries="@array/img"></Spinner>
<ImageView android:src="@drawable/icon" android:id="@+id/imageView1"
android:layout_height="wrap_content" android:layout_weight="0.69"
android:layout_width="match_parent"></ImageView>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Tut_2_1Activity!</string>
<string name="app_name">Tut_2_1</string>
<string-array name="img">
<item>Image 1</item>
<item>Image 2</item>
<item>Image 3</item>
</string-array>
</resources>
Tut_2_1Activity.java
package com.tut2_1;
import android.app.Activity;
import android.os.Bundle;
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 Tut_2_1Activity extends Activity implements
OnItemSelectedListener {
/** Called when the activity is first created. */
Spinner sp;
ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sp=(Spinner)findViewById(R.id.spinner1);
sp.setOnItemSelectedListener(this);
img=(ImageView)findViewById(R.id.imageView1);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2,
long arg3) {
if(arg2==0)
{
img.setImageResource(R.drawable.img1);
}
if(arg2==1)
{
img.setImageResource(R.drawable.img2);
}
if(arg2==2)
{
img.setImageResource(R.drawable.img3);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
Output
no
ReplyDelete