Android: Spinner Text Is White on White Background

On Android, if you have a Spinner widget inside a Fragment, the Spinner could have white text on a white (or on a Light theme) background. The problem is most often due to an incorrect reference to the Context object.

The layout and code fragment are below.

The problem is during the initialisation of the Adapter (in our case, it’s an ArrayAdapter) to be used with the Spinner. The context argument passed to the ArrayAdapter‘s constructor must be retrieved from getActivity() and not getActivity().getApplicationContext().

The Context from getActivity() will have the reference to the Resources.Theme object while the Context from getActivity().getApplicationContext() will not have any such reference.

The code in MainFragment.java should be changed as shown below.

Once the ArrayAdapter tied to the Spinner widget is initialised as shown above, the Spinner will inherit the correct background and foreground colors based on the selected theme.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react, vuejs); plays_on(xbox, ps4); linux_desktop_user(true); }