Erlassen Erlassen - 2 months ago 24
Android Question

How can I set a spinner prompt as an image

I have a spinner with a prompt. Is it possible to have the prompt as an image? Do I need a

CustomArrayAdapter
?

This is currently my
ArrayAdapter
:

Spinner legalSpinner = (Spinner) findViewById(R.id.legal);

legal = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,android.R.id.text1);
legalSpinner.setPrompt("Places");
legal.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
legalSpinner.setAdapter(legal);


Any help would be appreciated!

Answer

Okay, I managed to fix the problem on my own after a few days of work. If you want to set a spinner prompt as a image you must use a ImageGetter.

legalSpinner.setPrompt(Html.fromHtml("<img src=\"image\"", new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(String s) {
            Drawable drawFromPath;
            int path =
                    MyClass.this.getResources().getIdentifier(s, "drawable",
                            getBaseContext().getPackageName());
            drawFromPath = (Drawable) AddWashroom.this.getResources().getDrawable(path);
            drawFromPath.setBounds(0, 0, drawFromPath.getIntrinsicWidth(),
                    drawFromPath.getIntrinsicHeight());
            return drawFromPath;
        }
    }