in the last days I read lots of questions, answers and solutions regarding customization of AlertDialogs in Android. Still, none of them helped me out completely.
So my question is:
What Layout-element is used when adding Items via setSingleChoiceItems
or setItems
to an AlertDialog
?
Note: I want to style it in code, not via theme/style-solutions!
To this point I used a combination of the following answers to achieve a customized title, title-content-divider and different button colors.
Android - AlertDialog styling
Changing the background drawable of the searchview widget
My customized Dialog looks like this:
As you can see I achieved to dye the background of the title and the buttons in white as well as coloring the title itself and the divider in red but not the color of those single-choice items.
For that, I took a look into the android-sources, specifically "alert_dialog_holo.xml". I found the AlertDialog
is built of multiple LinearLayouts
nested into each other (I took this approach from the second Link). After determining the elements I wanted to style I used the approach of Link 1 (defining an onShowListener
to the AlertDialog
) to change their color. This worked pretty well for every element, whether it was the title, the divider, the buttons or even the content (standard text-content as well as custom content such as a DatePicker
).
Now I'm stuck on the last part (see question above) as it seems that those items are not being loaded into the ScrollView
of a "normal" content, neither in the FrameLayout
of a custom content (which I managed to style with white background).
So can someone please point me to the layout-elements that are used when the methods setSingleChoiceItems
or setItems
are used?
For completion I add the code how I created the shown AlertDialog, and how I styled it:
Creation of the Dialog:
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(getString(R.string.currency_choose))
.setCancelable(false)
.setSingleChoiceItems(R.array.currencies, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
currency = getResources().getStringArray(R.array.currencies)[which].substring(0, 1);
}
})
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// doSomething with clicked item
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
dialog.setOnShowListener(new DialogOnShowListener()); // here happens the styling
dialog.show();
Styling of the Dialog (excerpt, but should be enough to get the idea):
@Override
public void onShow(DialogInterface dialog) {
Dialog d = ((Dialog) dialog);
int divierId = d.getContext().getResources()
.getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(divierId);
divider.setBackgroundColor(red); // earlier defined color-int
}
Thanks in advance, chuky