0

is it possible to set a relativelayout array as items for spinner?

RelativeLayout[] items = new RelativeLayout[]{dep1.createObj(getContext()), dep2.createObj(getContext())};
ArrayAdapter<RelativeLayout> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, items);
_deposit.setAdapter(adapter);

i'm getting this as an output:

I would like to use the relativelayout because the object i want to display contains two strings to be displayed with left and right alignment

Onik
  • 19,396
  • 14
  • 68
  • 91
  • You could use a custom spinner adapter and inflate views on it . I don't think passing relative layouts as parameters will work . Take a look https://stackoverflow.com/questions/35983176/how-to-create-spinner-list-using-customadapter-in-android – Royce Raju Beena Nov 29 '18 at 12:54
  • The purpose of the third parameter in the `ArrayAdapter` is because you could you use your own POJO classes or user defined classes to supply the data and use it. Just because it accepts T[] Objects doesn't mean you can pass anything that extends Objects. Use Arrays of Strings or something else which has relevant data and is not an Android component. – oldcode Nov 29 '18 at 13:12

1 Answers1

1

No, you can only populate String in unmodified ArrayAdapter with default layout(which is pretty boring).

If you want to change the default behavior, like change the layout like you mentioned above, you have to extend an adapter like ArrayAdapter.

Here is an example of custom adapter

touhid udoy
  • 4,005
  • 2
  • 18
  • 31