postheadericon spinner in android example


6) Understand resource folders :
a. Create spinner with strings taken from resource folder(res >> value folder).
b. On changing spinner value, change image.


File Name : E6Activity.java


package bsr.exa;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class E6Activity extends Activity {
/** @author Bipin S Rupadiya , www.BipinRupadiya.blogspot.com
     *
     *   6) Understand resource folders :
* a. Create spinner with strings taken from resource folder(res >> value folder).
* b. On changing spinner value, change image.
     **/

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Spinner cmbSpinner = (Spinner)findViewById(R.id.cmbImg);
     
        cmbSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
       
        public void onItemSelected(AdapterView<?> arg0, View arg1,
    int arg2, long arg3) {
    ImageView img=(ImageView)findViewById(R.id.img1);
    String str=String.valueOf(cmbSpinner.getSelectedItem());
if(str.equals("Image-1"))
{
img.setBackgroundResource(R.drawable.bac);
       img.setImageResource(R.drawable.icon);
}
else if(str.equals("Image-2"))
{
img.setBackgroundResource(R.drawable.bac);
   img.setImageResource(R.drawable.android2);
}
else if(str.equals("Image-3"))
{
img.setBackgroundResource(R.drawable.bac);
   img.setImageResource(R.drawable.android3);
}
else if(str.equals("Image-4"))
{
img.setBackgroundResource(R.drawable.bac);
   img.setImageResource(R.drawable.android4);
}
else if(str.equals("Image-5"))
{
img.setBackgroundResource(R.drawable.bac);
   img.setImageResource(R.drawable.android5);
}
else
{
img.setBackgroundResource(R.drawable.bac);
   img.setImageResource(R.drawable.android1);
}
    }

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

});
           
        }
}

String.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">www.BipinRupadiya.blogspot.com</string>
    <string name="app_name">SL-MC,  Exercise 6</string>
    <string-array name="Android">
        <item>Image-1</item>
        <item>Image-2</item>
        <item>Image-3</item>
        <item>Image-4</item>
        <item>Image-5</item>
        <item>Image-6</item>
    </string-array>
</resources>



Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.