SingleWave Games
SingleWave Games

Reputation: 2648

Android Custom Spinner with Image selection

I am working on a custom spinner, I already understand how to use an ArrayAdapter in order to customise the layout of the custom popup.

The issue I am having however is that I do not want to show the default spinner on my UI instead I want to show an image, then this image will changed based on the item that has been selected, the image would look something like below:

enter image description here

Further to this, is there a way that I can use wrap_content, but to wrap it based on this image size rather than the default spinner size?

Upvotes: 0

Views: 1008

Answers (2)

KamleshNishad
KamleshNishad

Reputation: 24

Well its very easy to implement either you use switch case or if else both will work fine. I have implement this one using if else condition by attaching image source to each argument that I have declared.

Here is example of code.

 spinner = (Spinner) findViewById(R.id.spinner1);
    imageview = (ImageView) findViewById(R.id.imageView1);


    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        //spinner.
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {
            if(arg2==0)
            {

                imageview.setImageResource(R.drawable.apple);
            }
            else if(arg2==1)
            {
                imageview.setImageResource(R.drawable.microsoft);
            }
            else
            {
                imageview.setImageResource(R.drawable.google);
            }

Source - http://kamleshnishad.com/android-studio-spinner-example-onclick-change-image/

Upvotes: 0

matiash
matiash

Reputation: 55360

If I understood correctly, you want one layout for the value shown in the Spinner proper (layoutA), and a different one (layoutB) for the spinner option views in the popup window?

If that's the case, then it's very simple: in the adapter's constructor, pass layoutA. And then call setDropDownViewResource() with layoutB.

Alternatively, if you have a custom adapter class, you can achieve the same result by overriding getView() and getDropDownView() respectively.

Setting the spinner's height to WRAP_CONTENT should work in this scenario (although I haven't tested that particular bit).

Upvotes: 1

Related Questions