Skip to content

Instantly share code, notes, and snippets.

@Suleiman19
Last active October 11, 2022 14:10
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Suleiman19/b7cfd4d44e978d8b8fd4 to your computer and use it in GitHub Desktop.
Save Suleiman19/b7cfd4d44e978d8b8fd4 to your computer and use it in GitHub Desktop.
Theme Aware Material Design Spinner
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/AppTheme.PopupOverlay">
<Spinner
android:id="@+id/main_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
Spinner spinner = (Spinner) findViewById(R.id.main_spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(getSupportActionBar().getThemedContext(),
R.layout.spinner_list_style,
getResources().getStringArray(R.array.countries));
spinnerAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
<!--Used for Spinner's list item-->
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_dropdown"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="?attr/textColorAlertDialogListItem" />
<!--Used for the Spinner title indicator-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_list_item"
style="@style/TextAppearance.AppCompat.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:paddingEnd="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:singleLine="true"
android:textColor="@android:color/white" />
<!-- Base application theme (LIGHT). -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="spinnerStyle">@style/MySpinner</item>
</style>
<!-- Base application theme (DARK). -->
<style name="AppTheme.Dark" parent="Theme.AppCompat">
...
<item name="spinnerStyle">@style/MySpinner.Dark</item>
</style>
<style name="MySpinner" parent="Base.Widget.AppCompat.Spinner">
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
</style>
<style name="MySpinner.Dark" parent="MySpinner">
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Dark</item>
</style>
@Falchio
Copy link

Falchio commented Oct 11, 2022

Thanks a lot))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment