Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing font size of AlertDialog.Builder's components

I created an AlertDialogue using the following code :

 int selectedModeId=0;
 public void sortTypeModeSelection(){

    AlertDialog.Builder alertBuilder=new AlertDialog.Builder(WatchListDetailActivity.this);

    alertBuilder.setSingleChoiceItems(R.array.watchlist_sorting_modes,selectedModeId, new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
                case 0:
                    selectedModeId=0;
                    break;
                case 1:
                    selectedModeId=1;
                    break;
                case 2:
                    selectedModeId=2;
                    break;
                case 3:
                    selectedModeId=3;
                    break;
                case 4:
                    selectedModeId=4;
                    break;
                case 5:
                    selectedModeId=5;
                    break;
                case 6:
                    selectedModeId=6;
                    break;
                case 7:
                    selectedModeId=7;
                    break;
            }
            dialog.cancel();
        }
    });
    alertBuilder.show();
}

enter image description here

I made the alert, but I want to reduce the font size of the list items of the dialog. How can I do this?

Note: Don't recommend inflating a custom layout to accomplish this, I wish to know if there is another approach.

like image 999
edwin Avatar asked Feb 15 '13 05:02

edwin


People also ask

How do I change the font in AlertDialog?

If you only want to change text format, you can just override alertDialogTheme attribute to change the theme for the AlertDialog . <style name="MyTheme" parent="Theme. AppCompat.

How do I set the width and height of an AlertDialog?

You just have to give android:theme="@android:style/Theme. Dialog" in the android manifest. xml for your activity and can write the whole layout as per your requirement. you can set the height and width of your custom dialog from the Android Resource XML.

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

How do I use AlertDialog builder?

Alert Dialog code has three methods:setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is used to set the icon on the Alert dialog box.


1 Answers

I was able to achieve this through styles. I added this style to my styles.xml file in the values directory:

<style name="AlertDialogTheme" parent="android:Theme.Dialog">
    <item name="android:textSize">14sp</item>
</style>

Then when creating the AlertDialog, I wrapped the activity context in a ContextThemeWrapper and passed that into the Builder constructor:

ContextThemeWrapper cw = new ContextThemeWrapper( this, R.style.AlertDialogTheme );
AlertDialog.Builder b = new AlertDialog.Builder( cw );

This produced smaller text size for the list items in the dialog.

enter image description here

like image 165
Jason Robinson Avatar answered Sep 23 '22 01:09

Jason Robinson