package com.customspinnerdemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
public class CustomSpinnerDemoActivity extends Activity {
String[] strings = {"Motorola","Google",
"Microsoft", "Apple", "Yahoo","Samsung"};
String[] subs = {"Mobile world","Google gives you everything",
"Microsoft king of windows", "Apple you will love it", "Yahoo still strugling","Samsung good"};
int arr_images[] = { R.drawable.motorola,
R.drawable.google, R.drawable.microsoft,
R.drawable.apple, R.drawable.yahoo, R.drawable.samsung};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
mySpinner.setAdapter(new MyAdapter(CustomSpinnerDemoActivity.this, R.layout.row, strings));
}
public class MyAdapter extends ArrayAdapter{
public MyAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
TextView label=(TextView)row.findViewById(R.id.company);
label.setText(strings[position]);
TextView sub=(TextView)row.findViewById(R.id.sub);
sub.setText(subs[position]);
ImageView icon=(ImageView)row.findViewById(R.id.image);
icon.setImageResource(arr_images[position]);
return row;
}
}
}