Skip to content Skip to sidebar Skip to footer

Spinner With Text And Icons

In my app, I have a Spinner, that can be filled with two Arrays of Strings, stored in my values/strings.xml resource. Depending on the state of two RadioButtons, the values from th

Solution 1:

You need to implement a custom Adapter and define a custom layout for the list items. Check this tutorial.

UPDATE

Try the following. I haven't tested the code but I think it should work like that.

public class MyCustomAdapter extends ArrayAdapter<String>{

    private String[] mIcons;

    public MyCustomAdapter(Context context, int textViewResourceId,
    String[] objects, String[] icons) {
        super(context, textViewResourceId, objects);
        mIcons = icons;
    }

    @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.spinnerrow, parent, false);
       TextView label=(TextView) findViewById(R.id.functie);
       label.setText(getItem(position);

       ImageView icon=(ImageView)row.findViewById(R.id.icon);

       String uri = "@drawable/a" + mIcons[position];
       int imageResource = getResources().getIdentifier(uri, null, getPackageName());
       icon.setImageResource(imageResource);

       return row;
    }
}

Post a Comment for "Spinner With Text And Icons"