Android Spinner with Different Layouts for "Drop Down State" and "Closed State"

Android Spinner with different layouts for drop down state and closed state?

You have to create a custom Adapter class for the Spinner and overwrite the two methods getView() for the normal closed view and getDropDownView() for the drop down list view. Both methods must return a View object for a single element.

Have a look at this tutorial it might help you getting started.

Spinner Layout Closed different From Layout of Each Row

Finnaly found a solution to this problem :)
Didn't know that getDropDownView was for the dropdown layout and the getView was for the single layout, at closed spinner...

Solution here: Android Spinner with different layouts for "drop down state" and "closed state"?

Android: how to give alternate colors to the spinner items

You can use

ArrayAdapter<CharSequence> adapter =
new ArrayAdapter(this, R.layout.simple_spinner_item, myList) {
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
if (position % 2 == 0) { // we're on an even row
view.setBackgroundColor(evenColor);
} else {
view.setBackgroundColor(oddColor);
}
return view;
}
}

Setting the drop down list of a spinner with an int[] array

use below code

  String[] array = {"1", "2","3", "4","5","6","7","8","9","10"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_item, array);

instead of

  ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
intArray, android.R.layout.simple_spinner_item);

Android - Text dropdown/selection of Spinner does not show

Ok, what's happening is that when you're calling setDropDownViewResource you're replacing the layout you previously specified in the constructor. In your case R.layout.profile_color. SimpleCursorAdapter extends ResourceCursorAdapter and in the constructor sets the two layouts equal to each other.

public ResourceCursorAdapter(Context context, int layout, 
Cursor c, boolean autoRequery) {

super(context, c, autoRequery);
mLayout = mDropDownLayout = layout;
mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

The issue arises when you call setDropDownViewResource and change the drop down layout. The SimpleCursorAdapter will continue to use the same resource id bindings that you specified in the constructor.

What you should do is only specify the layout in the SimpleCursorAdapter's constructor. I suggest changing your code to as follows:

String[] from = new String[] { ProfileDbAdapter.COL_PROFILE_TITLE };
int[] to = new int[] { android.R.id.text1 }; // from simple_spinner_dropdown_item

SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_dropdown_item, profilesCursor, from, to);

spinnerColor.setAdapter(profilesAdapter);

To get the results you want.

Basically don't use the setDropDownViewResource method. Or, if you do, and you change the resource id bindings, you'll have to call SimpleCursorAdapter#changeCursorAndColumns; however, that is probably overkill for the simple result you're trying to achieve.



Related Topics



Leave a reply



Submit