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
How to Keep Android Applications Always Be Logged in State
How to Capture Image from Custom Cameraview in Android
Android Textview: Setting the Background Color Dynamically Doesn't Work
How Do We Distinguish Never-Asked from Stop-Asking in Android M's Runtime Permissions
Android Accelerometer Not Working When Screen Is Turned Off
Android: How to Change the Actionbar "Home" Icon to Be Something Other Than the App Icon
Android Studio Unit Testing: Read Data (Input) File
How to Correctly Use Google Plus Sign in with Multiple Activities
Android Navigation Architecture Component - Get Current Visible Fragment
How to Draw a Circle with Animation in Android with Circle Size Based on a Value
How to Create Converter for My Class in Android Retrofit Library
How to Implement Export SQLite to Excel/CSV File in Android
Android Sdk Manager Not Installing Components
Migration from Gcm to Fcm Needed
What Is the Default Font Family in Android
Interstitial Admob Ads: "Illegalstateexception: Only Fullscreen Activities Can Request Orientation"