Question

I have been able to apply the custom font to the title of the Alert dialog as below:

 AlertDialog.Builder builder = new AlertDialog.Builder(this);

 TextView Mytitle = new TextView(this);
 Mytitle.setText("My Custom title"); 
 Mytitle.setTextSize(20);
 Mytitle.setPadding(5, 15, 5, 5);
 Mytitle.setGravity(Gravity.CENTER);
 Mytitle.setTypeface(Typeface.createFromAsset(this.getAssets(), "myfont.ttf"));
 builder.setCustomTitle(Mytitle);

The alert Dialog displays a list of Multiselect Items populated by the line below.

 builder.setMultiChoiceItems(MyItems, MycheckedItems, MyDialogListener);

 //where MyItems is CharSequence[] Array, MycheckedItems => boolean[] array,
 //MyDialogListener => DialogInterface.OnMultiChoiceClickListener

i want to apply a font to these multiselect items also. How can i do this? is it possible?

Was it helpful?

Solution

AlertDialog.Builder uses AlertController.AlertParams to construct a dialog. I examined that AlertDialog.Builder#create() calls AlertController.AlertParams#apply() which creates ListView and assign adapter, if items are set (AlertParams#createListView()).

I created custom adapter based on createListView sources and modified android cell layout:

public static class TypefaceDialog extends DialogFragment {
        private static final CharSequence[] items = {
            "A", "B", "C", "D", "E", "F", "G"
        };
        private static final boolean[] checked = {
            true, false, false, true, true, false, false
        };

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Typeface fontTypeface = Typeface.createFromAsset(getActivity().getAssets(), "Arial Bold.ttf");
            ListAdapter adapter = new ArrayAdapter<CharSequence>(
                    getActivity(), 
                    android.R.layout.select_dialog_multichoice, 
                    android.R.id.text1,
                    items) {

                @Override
                public View getView(final int position, View convertView, ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);
                    CheckedTextView textView = (CheckedTextView)view.findViewById(android.R.id.text1);

                    textView.setChecked(checked[position]);
                    textView.setTypeface(fontTypeface);
                    textView.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            CheckedTextView view = (CheckedTextView)v;
                            view.setChecked(!view.isChecked());
                            checked[position] = view.isChecked();
                        }
                    });

                    return view;
                }
            };

            return new AlertDialog.Builder(getActivity())
            .setAdapter(adapter, null)
            .setPositiveButton("OK", null)
            .create();
        }

    }

OTHER TIPS

Try creating a custom adapter for the multiple select with a custom item layout.

Set the Typeface as static within your activity or application

public static Typeface mFont;
public void OnCreate(){
mFont =Typeface.createFromAsset(this.getAssets(), "myfont.ttf");
}

In your custom adapter, set the typeface of the textview from your convertView using the static typeface.

Instead of AlertDialog.Builder use Dialog like as follow .

Inside the R.layout.vv customize as like you want.

Dialog dailog=new Dialog(this);
dailog.setContentView(R.layout.vv);
dailog.show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top