3

I am trying to get a selected view of my spinner but it is returning null as all the time.

spinner.getSelectedView()

Any idea?

3
  • That should work. Does the spinner have a selected item?
    – matiash
    Dec 16, 2014 at 3:28
  • Can you provide more code? A short, self contained snippet that demonstrates the problem.
    – Alex K
    Dec 16, 2014 at 4:09
  • 1
    Figured was trying to access getSeletedView() before setting selection duh :( – Dec 18, 2014 at 23:02

3 Answers 3

1

Even after setting the view spinner.getSelectedView() was returning null for me.

As a workaround, I used the following code which worked for me and returned the correct view for some reason.

new Handler().post(() -> ((TextView)mVehicleView.getSelectedView()).setTextColor(context.getColor(R.color.gray2)));

If someone can explain why this happened, it would be great.

Posting this incase it happens to someone else.

0

You don't need the view, do you? I think this is what you want:

Spinner s = (Spinner) findViewById(R.id.spinner);
String text = s.getSelectedItem().toString();
4
  • 1
    No I need a view specifically, so that I can modify it. Dec 16, 2014 at 2:21
  • Ah. I'm a bit confused then. You want the view that the spinner has selected? Because your code should work in that case.
    – Alex K
    Dec 16, 2014 at 4:08
  • Figured was trying to access getSeletedView() before setting selection duh :( Dec 18, 2014 at 23:02
  • @VendettaDroid Great! Glad you figured it out
    – Alex K
    Dec 18, 2014 at 23:06
0

Yes, in my case it was timing issue as the getSelectedView() was called before setting selection. That explains why I was getting the view as null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.