4

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: enter image description here

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

Community
  • 1
  • 1
chuky
  • 1,037
  • 1
  • 12
  • 21

1 Answers1

1

Guess I found the solution, so if everyone else should ever come across this problem here's how I've done it:

It feels like a really crappy solution, but I just searched for the ID of a List-Element the moment it was clicked and apparently every List-Element can be styled with this ID.
The Code I used to get the ID was the following:

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) {
        // -------------- here I got the ID ----------- //
        System.out.println(((Dialog)dialog).getCurrentFocus().toString());
        // doSomething with clicked item
    }
})
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        DBs.getDBSettings().writeSetting("CURRENCY", currency);
    }
})
.setNegativeButton(android.R.string.cancel, null)
.create();
dialog.setOnShowListener(new DialogOnShowListener());
dialog.show();

The ID that was part of this Views toString-method is "android:id/select_dialog_listview".

Finally a screenshot showing my solution:

Greetings, chuky

chuky
  • 1,037
  • 1
  • 12
  • 21
  • can you please tell me how did you change the title color and the divider color? Thanks – Haifeng Zhang Sep 09 '14 at 04:56
  • It's actually written in the question. Read it again and give the last code block special attention. There you can see the code that I used to style the divider. The title can be styled exactly alike, you just need another ID... – chuky Sep 09 '14 at 12:53