Reputation: 61
I have a dialog
containing a spinner
. When I run the app on the emulator everything works fine but when I run it on a device the spinner
text becomes white and the background of the clicked spinner
is dark. I can't understand why nor find out how to fix it. This is very annoying because the text can't be read anymore.
I already implemented spinners
in other places in my app and everything works fine. This issue only occurs when the spinner
is in a dialog
.
Here is the code of my dialog
:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
builder.setPositiveButton(R.string.ok, new MyOnClickListener(itemInfo));
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.setMessage(R.string.dialog_message).setTitle(R.string.dialog_title);
//Create spinner
ArrayList<String> listForSpinner = (ArrayList) currentUser.getListNames();
String[] spinnerList = listForSpinner.toArray(new String[listForSpinner.size()]);
final ArrayAdapter<String> adp = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, spinnerList);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = new Spinner(getApplicationContext());
spinner.setAdapter(adp);
spinner.setPadding(50, 50, 50, 0);
spinner.setPopupBackgroundResource(R.color.white); //test that wasn't successful
builder.setView(spinner);
AlertDialog dialog = builder.create();
dialog.show();
Thank you very much !
Upvotes: 2
Views: 2251
Reputation: 61
What I did to fix this issue is that I created custom layouts.
I changed 2 lines in my code :
final ArrayAdapter<String> adp = new ArrayAdapter<>(getApplicationContext(), R.layout.dialog_spinner_item, spinnerList);
adp.setDropDownViewResource(R.layout.dialog_spinner_dropdown_item);
And created 2 layout files :
dialog_spinner_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/black"
android:background="@color/white"
android:textSize="@dimen/text_size_title_text_view">
</TextView>
dialog_spinner_dropdown_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/black"
android:background="@color/white"
</CheckedTextView>
Upvotes: 3
Reputation: 99
You can change the background by defining your own style in styles.xml
<style name="SpinnerTheme" parent="Theme.AppCompat.Light">
<item name="colorAccent">@color/bb_inActiveBottomBarItemColor</item>
<item name="android:textColorPrimary">#ffffff</item>
<item name="colorPrimary">@color/active_tab_icon</item>
<item name="colorPrimaryDark">@color/active_tab_icon</item>
<item name="android:background">@color/mb_blue</item>
</style>
Then, in your layout.xml set the custom style to your spinner:
<Spinner
style="@style/SpinnerTheme"
android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/mySpinner"
/>
Upvotes: 1